Write a program using C language Compile and run the program found in countgrade
ID: 647699 • Letter: W
Question
Write a program using C language
Compile and run the program found in countgrades.c. This program is supposed to read a series of input characters representing grades (e.g., an 'a' or 'A' represents an A, a 'b' or 'B' represents a 'B', and so on) and print counts of each grade. Here is some sample input and the output it is supposed to produce:
None of the counts will seem to be correct. Don't try to fix it in one shot. Before making any changes, put a print statement at the beginning of the loop (before the switch) that prints each character that's read. Also, add one statement for each case in the switch (as well as the default) that prints the value of the counter after it is updated. Again, you will note this program needs documentation. You should comment the code in your file to show the algorithm. This will also help you understand the code, so you can debug it. This output should help explain at least partially why the counters have the wrong values. After you make these fixes, re-run the program to see if it is fixed, and if it is not, try to add more printf's to help you see what's going on. Your task is to fix this program to produce output like the above.
Explanation / Answer
/* A program to count the number of grades occuring in the input */
#include <stdio.h>
int main()
{
int a;
int b;
int c;
int d;
int f;
int others;
a = b = c = d = f = 0;
others = 0;
while ((c = getchar()) != EOF)
{
printf("%c",c);
switch(c)
{
case 'a': a++;
printf("%d",a);
case 'b': b++;
printf("%d",b);
case 'c': c++;
printf("%d",c);
case 'd': d++;
printf("%d",d);
case 'f': f++;
printf("%d",f);
default: others++;
printf("%d",others);
}
}
printf("Grade counts: ");
printf(" A's: %d ", a);
printf(" B's: %d ", b);
printf(" C's: %d ", c);
printf(" D's: %d ", d);
printf(" F's: %d ", f);
printf(" Other grades: %d ", others);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.