Question: C Programming!! Im getting a segmentation fault when I hand this code into my school webgrader and I can't find the problem, please help! Code:
C Programming!! Im getting a segmentation fault when I hand this code into my school webgrader and I can't find the problem, please help!
Code:
/* Write a menu driven program that simulates a virtual grade book. The program will * print a menu of several options. These options are: enter grade, find average, find * number of certain letter grade, and print the number of total grades entered. */
#include
//Function Prototypes void add(int *array,int size); double average(int *array,int size); int sum(int *array,int size); int getSize(int *array,int size);
int main(void) {
//Declare variables int size=100; int array[size]; int count=0; int choice;
do {
//Display menu printf(" Main Menu"); printf(" 1. Add Grade"); printf(" 2. Average Grade"); printf(" 3. Number of Certain Letter Grade"); printf(" 4. Number of Grades Entered"); printf(" 5. Exit"); printf(" Enter Choice: "); scanf("%d", &choice);
//Display error message for invalid choice if(choice < 1 || choice > 5) { printf("Invalid Selection. "); }
//Switch Function for different choices from menu switch(choice) { case 1:{ add(array, count); count++; break; } case 2:{ double avg = average(array, count); printf("The Average is :%.2f ",avg); break; } case 3:{ int cnt = sum(array, size); printf("Count = %d ",cnt); break; } case 4:{ int length = getSize(array, size); printf("No of total grades = %d ", length); break; } case 5:{ printf("Exited Program."); break; } default:{ break; } } } while(choice != 5); return 0; }
//Function Definition void add(int *array, int size) {
int i = 0, count = 0, grade;
for(i = 0; i < size; i++) { if(array[i] != '\0') { count++; } }
while(1) { //Prompt user to enter grade printf(" Enter grade:"); scanf("%d",&grade);
//Display error message if grade > 100 or < 0 if(grade < 0 || grade > 100) { printf(" Integer must be between 0 and 100."); continue; } else { array[count-1] = grade; break; } } }
double average(int *array, int size) {
double tot = 0; int i = 0, count = 0; double avg;
while(array[i] != '\0') { tot += array[i]; i++; } return tot/i; }
int sum(int *array, int size) {
char totalCountInput;
printf(" Enter a letter grade to find its total count:"); scanf(" %c", &totalCountInput);
int i, count = 0, cA = 0, cB = 0, cC = 0, cD = 0, cF = 0;
for(i = 0; i < size; i++) { if (array[i] >= 90) { cA++; } else if (array[i] >= 80 && array[i] < 90) { cB++; } else if (array[i] >= 70 && array[i] < 80) { cC++; } else if (array[i] >= 60 && array[i] < 70) { cD++; } else if (array[i] < 60) { cF++; } }
//Create statement that will include uppercase and lowercase inputs for total count if(totalCountInput == 'a' || totalCountInput == 'A') { return cA; } else if(totalCountInput == 'b' || totalCountInput == 'B') { return cB; } else if(totalCountInput == 'c' || totalCountInput == 'C') { return cC; } else if(totalCountInput == 'd' || totalCountInput == 'D') { return cD; } else if(totalCountInput == 'f' || totalCountInput == 'F') { return cF; } }
int getSize(int *array, int size) {
int i = 0, count = 0;
for(i = 0; i < size; i++) { if(array[i] != '\0') { count++; } } return count; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
