Read a series of integers from stdin until EOF. The integers represent a series
ID: 3554496 • Letter: R
Question
Read a series of integers from stdin until EOF. The integers represent a series of test scores ranging from 0-100 (inclusive). Report the min, max, mean, and mode (the most frequent value or values) of the test scores. Print out the frequency of the mode, in brackets, followed by a series of one or more test scores, separated by spaces, having the given frequency. See the test cases for examples. Note the number of input integers may be very large. Don't try to store the inputs into an array -- you'll fail test cases. However, you will want to use an array to create a histogram of the inputs.
#include <stdio.h>
#define MAX_SCORE 100
int main() {
/* Read a series of integers from stdin until EOF. The integers
* represent a series of test scores ranging from 0-100 (inclusive).
* Report the min, max, and mean of the test scores, within the specified
* complexity. The mean should be calculated with floating-point precision
* to two decimal places, and the stats should only be reported if there
* is at least 1 score.
*/
int min = MAX_SCORE, max = 0, tmp;
double avg = 0;
int count = 0;
while(scanf("%d", &tmp) == 1) {
count++;
avg += (double)tmp;
if(tmp < min) {
min = tmp;
}
if(tmp > max)
{
max = tmp;
}
}
avg /=count;
printf("Max: %d ",max);
printf("Min %d ",min);
printf("Mean: %.2f ",avg);
return 0;
}
Test 1
Results Summary: Not run yet
Test 2
Results Summary: Not run yet
Test 3
Results Summary: Not run yet
Test 4
Results Summary: Not run yet
Input Output0 0 25 50 50 75 100 100
Min: 0 Max: 100 Mean: 50.00 Mode: [2] 0 50 100
Explanation / Answer
#include <stdio.h>
#define MAX_SCORE 100
int main() {
/* Read a series of integers from stdin until EOF. The integers
* represent a series of test scores ranging from 0-100 (inclusive).
* Report the min, max, and mean of the test scores, within the specified
* complexity. The mean should be calculated with floating-point precision
* to two decimal places, and the stats should only be reported if there
* is at least 1 score.
*/
int min = MAX_SCORE, max = 0, tmp;
double avg = 0;
int count = 0;
int arr[100] = {0} ;
int mode = 0;
while(scanf("%d", &tmp) == 1) {
count++;
avg += (double)tmp;
arr[tmp]++ ;
if( arr[tmp] > mode )
{
mode = arr[tmp];
;
}
if(tmp < min) {
min = tmp;
}
if(tmp > max)
{
max = tmp;
}
}
avg /=count;
int i = 0 ;
if( count != 0 )
{
printf("Max: %d ",max);
printf("Min %d ",min);
printf("Mean: %.2f ",avg);
printf("Mode: [%d] ", mode) ;
if( count != 0 )
for(i = 0 ; i <= 100 ; i++ )
if( arr[i] == mode )
printf("%d ", i ) ;
printf(" ");
}
//char c ;
//scanf("%c",&c);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.