Hey,
I have to write a program for class that takes in a last name, first name, and exam score and stores them into seperate arrays. No structs in this however. Then I have to print the high score, low score, and avg score. Thats it. I thought that I had it right but it doesnt work correctly when I run it, even though it compiles fine.
Heres my program...
This is what Im getting when I enter some information...Code:#include <stdio.h> #define CLASS_SIZE 10 #define NAME_SIZE 20 int getInfo(char[][20], double[], char[][20], int); void printResults(char[][20], double[], char[][20], int); void doCalcs(double[], int); int main() { int count = 0; char first[10][20]; char last[10][20]; double score[10]; count = getInfo(first, score, last, count); doCalcs(score, count); printResults(first, score, last, count); return 0; } int getInfo(char first[][20], double score[], char last[][20], int count) { char again='y'; while(again=='y') { printf("Please enter the student's first name -- "); scanf("%s", first[count]); printf("Please enter the student's last name -- "); scanf("%s", last[count]); printf("Please enter the exam score -- "); scanf("%lf", &score[count]); count++; printf("Would you like to enter another record? (y/n)"); //scanf("%c", &again); scanf("%s", &again); } return count; } void doCalcs(double score[], int count) { int i; double high; double low = 105; double avg; double total; for(i=0; i<count; i++) { if(score[i] > high) { high = score[i]; } if(score[i] < low) { low = score[i]; } } for(i=0; i<count; i++) { total = total + score[i]; } avg = total/count; printf("Total number of students = %d\n", count); printf("Highest score = %lf\n", high); printf("Lowest score = %lf\n", low); printf("Average score = %lf\n", avg); } void printResults(char first[][20], double score[], char last[][20], int count) { int i; for(i=0; i<count; i++) { printf("%s, %s %4.2lf\n", last[i], first[i], score[i]); } }
Please enter the student's first name -- Joe
Please enter the student's last name -- Smith
Please enter the exam score -- 30
Would you like to enter another record? (y/n)y
Please enter the student's first name -- Bob
Please enter the student's last name -- White
Please enter the exam score -- 80
Would you like to enter another record? (y/n)n
Total number of students = 2
Highest score = 0.000000
Lowest score = 0.000000
Average score = 37270968922511555218815399770931566435968430750338 28121034433494206368125488490594734490445484833985 42932704708245693379119842581045603106586418751351 90047137854599889866604204928347406994246874588887 4194112758707602325504.000000
7N, 0.00
, 0.00
Segmentation fault
Any help would be greatly appreciated!!!! Thank you!![]()



LinkBack URL
About LinkBacks



