Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming!! Im getting a segmentation fault when I hand this code into my sc

ID: 3596537 • Letter: C

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:

/* 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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

//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] != '') {
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] != '') {
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] != '') {
count++;
}
}
return count;
}

Explanation / Answer

Segmentation fault rises when the program tries to access an area of memory that it is not allowed to access or your input is not able to be stored in your variables. These will be the most cases. For this problem follow below steps.

1. First, increase the size variable from 100 to 1000 or 10000 i.e

int size=100; to int size=1000; or int size=10000;

Now try running your code in your school webgrader. If you are still getting the error then follow step two

2. Change all int to long int and increase size to long int size = 100000 and run. I hope this solves your problem

Try first step, run and if the first step does not work then go for step 2

**Comment for any further queries.