Intro to C Programming Lab Zhantong Liang Fall 2017 3. Grade Zed is an employee
ID: 3607221 • Letter: I
Question
Intro to C Programming Lab Zhantong Liang Fall 2017 3. Grade Zed is an employee of Always Cook Mushroom. His boss Kevin gives him a pack of mushrooms and asks him to grade each mushroom according to its weight. Suppose the weight of a mushroom is w, tben its grade s is: s = 10000-(100-w)2 What's more, Zed also has to report the mode of the grade of these mushrooms. The mode is the value that appears most often. Mode may not be unique. If there are different grades but all of them have the same frequency, there is no mode Input The first line of the input contains an integer T, denoting the number of test cases. Then T test cases follow The first line of each test cases contains one integers N (1Explanation / Answer
#include <stdio.h>
void sort(int m, int x[ ])
{
int i, j, t;
for(i = 1; i <= m-1; i++){
for(j = 1; j <= m-i; j++){
if(x[j-1] >= x[j]){
t = x[j-1];
x[j-1] = x[j];
x[j] = t;
}
}
}
}
int main(){
int t;
scanf("%d",&t);
int n,w;
int caseno=0;
while(t--){
caseno++;
scanf("%d",&n);
int grades[n];
// Counter to store number of times each grade occured
int counter[10001] = {0};
int c,cnt;
// Find grades and calculate their frequencies
for(c=0;c<n;c++){
scanf("%d",&w);
grades[c] = 10000 - ( (100-w)*(100-w) );
counter[grades[c]]++;
}
int gradesnum=0;
// Find out total number of grades given
for(cnt=199;cnt<10001;cnt++){
if(counter[cnt] > 0){
gradesnum++;
}
}
int maxv=counter[199];
// Find out maximum frequency of grades
for(cnt=200;cnt<10001;cnt++){
if(counter[cnt] > maxv){
maxv = counter[cnt];
}
}
int mode[10000],i=0;
// Find out grades with maximum frequency
for(cnt=199;cnt<10001;cnt++){
if(counter[cnt] == maxv){
mode[i] = cnt;
i++;
}
}
printf("Case #%d ",caseno);
// Check if number of all grades have maximum frequency
if(i == gradesnum){
printf("Bad Mushroom ");
}else{
sort( i,mode );
// Sort grades in increasing order
for(c=0;c<i;c++){
printf("%d ",mode[c]);
}
printf(" ");
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.