I am having trouble with the max, min, median, and sorting ascending. PROGRAMMIN
ID: 3638067 • Letter: I
Question
I am having trouble with the max, min, median, and sorting ascending.PROGRAMMING ASSIGNMENT #1
Write a C program that determines the minimum grade, maximum grade, median grade and class average of an exam. Your program should first prompt for how many grades, then each of the grades. Your program should test that each value entered is valid (i.e., between 0 and 100). If the grade entered is not valid, the program should issue an error message as shown below, and re-prompt for a correct grade. Once all grades are entered, your program should calculate the minimum grade, maximum grade, and class average, and median for that exam. It will then print the grades listed in Ascending order (sorted). The program without the sort will be worth 90 points.
Your program should store all the grades entered in integer array: grades[100].
The dialog with the user must look exactly like:
Welcome to the Grade Calculator
Enter the number of grades to process (0 - 100): 10
Enter grade for student #1: 80
Enter grade for student #2: 90
Enter grade for student #3: 100
Enter grade for student #4: 85
Enter grade for student #5: 172
*** Invalid grade entered. Please enter 0 - 100. ***
Enter grade for student #5: 72
Enter grade for student #6: 65
Enter grade for student #7: 96
Enter grade for student #8: 70
Enter grade for student #9: 73
Enter grade for student #10: 100
The minimum grade is 65
The maximum grade is 100
The class average is 83
The median grade is 82.5
The 10 Grades entered were:
65, 70, 72, 73, 80, 85, 90, 96, 100, 100
Note: The coral text represents the "output" from your program and is shown for clarity only here. It is not required that your output be in this color. (Do not even attempt to try!). Also note that what the user types in is indicated by the blue area above. Again, for clarity only. Once the user enters a valid number of grades to process, the program should run as described above, but prompt for ONLY the number of grades entered by the user. If the user chooses to enter 0 (or fewer) grades, the program should simply end (appropriate exit message of your choice). Do not use the qsort() function from the Standard C Library.
*** Sample Algorithm for Assignment #1 ***
Note: This assignment can be solved many different ways. This algorithm simply illustrates one possible solution. Please feel free to ignore this algorithm and use your own design to solve this problem if desired.
1. Trap loop to prompt for number of grades(min 0 max 100)
2. Initialize stuff:
2.1 Set “minimum” equal to zero.
2.2 Set “maximum” equal to zero.
2.3 Set “sum” equal to zero.
3. Trap LOOP for each grade (grades[0] – grades[n]) with edits.
{
Keep running total of “sum” of grades:
Add each (good) grade to “sum” of grades.
}
4. Calculate average as:
“sum” of grades divided by number of grades.
5. Sort the stored array in ascending order with a bubble sort.
6. Display results:
6.1 Display minimum grade.
6.2 Display maximum grade.
6.3 Display class average.
7. Calculate the median.
7.1 If odd number entered grade[(n+1)/2]
7.2 if even number entered, (grade[n/2] + grade[n/2+1])/2
7.3 Display median
8. Print loop to print the sorted array.
#include <stdio.h>
#define NUM_GRADES 10
main()
{
int grades[NUM_GRADES], num, max = 0, min = 0, med, average, sum = 0, x, s, temp;
char changed = 'T';
printf("Welcome to the Grade Calculator ");
printf("Enter the number of grades to process (0 - 100): ");
scanf("%i", &num);
fflush(stdin);
if(num > NUM_GRADES)
{
printf(" *** Error: Number of grades is invalid *** ");
printf("Enter the number of grades to process (0 - 100): ");
scanf("%i", &grades);
fflush(stdin);
}
printf(" ");
for(x = 0; x < num; ++x)
{
do
{
printf("Enter grade for student #%i: ", x + 1);
scanf("%i", &grades[x]);
fflush(stdin);
if(grades[x] > max)
max = grades[x];
if(grades[x] < min || grades[x] < max)
min = grades[x];
if(grades[x] < 0 || grades[x] > 100)
printf(" *** Invalid grade entered. Please enter 0 - 100. *** ");
else
sum = sum + grades[x];
}while(grades[x] < 0 || grades[x] > 100);
}
printf(" ");
average = (float) sum / NUM_GRADES;
printf("The minimum grade is ", min);
printf("The maximum grade is ", max);
printf("The class average is ", average);
if(NUM_GRADES % 2 == 0)
med = (grades[x/2] + grades[x/2+1])/2;
else
med = grades[(x + 1)/ 2];
printf("The median grade is %i ", med);
printf("The 10 Grades entered were: ");
for(x = 0; x < NUM_GRADES; x++)
printf("%i", grades[x]);
printf(" ");
return 0;
}
Explanation / Answer
please rate - thanks
in your sample the average is wrong. it's showing as an int, when the average is a real number
message me before rating if a problem (I will get back to you in the morning)
#include <stdio.h>
#include <conio.h>
#define NUM_GRADES 10
main()
{
int grades[NUM_GRADES], num, max = 0, min = 105, sum = 0, x, s, temp;
float average,med;
printf("Welcome to the Grade Calculator ");
printf("Enter the number of grades to process (0 - 100): ");
scanf("%i", &num);
fflush(stdin);
if(num > NUM_GRADES)
{
printf(" *** Error: Number of grades is invalid *** ");
printf("Enter the number of grades to process (0 - 100): ");
scanf("%i", &grades);
fflush(stdin);
}
printf(" ");
for(x = 0; x < num; x++)
{
do
{
printf("Enter grade for student #%i: ", x + 1);
scanf("%i", &grades[x]);
fflush(stdin);
if(grades[x] < 0 || grades[x] > 100)
printf(" *** Invalid grade entered. Please enter 0 - 100. *** ");
}while(grades[x] < 0 || grades[x] > 100);
sum = sum + grades[x];
if(grades[x] > max)
max = grades[x];
if(grades[x] < min )
min = grades[x];
}
printf(" ");
average = (float) sum / num;
printf("The minimum grade is %d ", min);
printf("The maximum grade is %d ", max);
printf("The class average is %.1f ", average);
for(x=0;x<num-1;x++)
for(s=x+1;s<num;s++)
if(grades[x]>grades[s])
{temp=grades[s];
grades[s]=grades[x];
grades[x]=temp;
}
if(NUM_GRADES % 2 == 0)
med = (grades[x/2] + grades[x/2+1])/2.;
else
med = grades[(x + 1)/ 2];
printf("The median grade is %.2f ", med);
printf("The %d Grades entered were: ",num);
for(x = 0; x < num; x++)
printf("%i ", grades[x]);
printf(" ");
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.