uhm... i really need your help about this... below is the problem but i have my own turbo C code. and the problem in my code is that i cannot total and average the scores that i restore in my arrays...

write a program that will process the exam scores of students in a exam.here are the requirements:
-the program should ask for a student name followed by the 4 exam scores(percentage) of the student. do this for 15 students.
-store the student names in a two-dimensional array. another two-dimensional array must also be used to store exam scores.
-calculate the final score of each student computed as:
...exam 1 is 20%
...exam 2 is 20%
...exam 3 is 30%
...exam 4 is 30%
-calculate the average of the entire class (average of the individual student final score)
-for each student,display name,exam scores,final score with it's letter grade equivalent.

letter grade Final score
A 91-100
B 81-90
C 71-80
D 61-70
F 60 below

here is my code...i cannot total and average the scores that i restore in my arrays...hope that you can help me..

#include<stdio.h>
#define MAXSTUDENTS 15
#define EXAMS 4

main(){
char name[20];
int x[MAXSTUDENTS][EXAMS],grade,total,row,col;
float ave;
clrscr();

for(row=0;row<MAXSTUDENTS;row++){

printf("\nEnter First name : ");
scanf("%s",name);

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade);

total+=grade[row][col];

}
ave=total/EXAMS;



}
if (ave>91) printf("A");
if (ave>=81 && ave<=90) printf("B");
if (ave>=71 && ave<=80) printf("C");
if (ave>=61 && ave<=70) printf("D");
if (ave<=60) printf("F");
getch();}

i hope that you can help me until tomorrow 11.59(jan. 22)... i do really appreciate your helps...sorry for the urgent demand...:X

Recommended Answers

All 20 Replies

after first for loop u need to initialize total.

after first for loop u need to initialize total.

like this:
for(row=0;row<MAXSTUDENTS;row++){
total=0;

if its wrong...can you show me?

When posting c code, please use c code tags
[code=c] /* Your code here */

[/code]

Yes, that's where the total=0 goes.

You could simplify the following code by using else:

if (ave>91) printf("A");
if (ave>=81 && ave<=90) printf("B");
if (ave>=71 && ave<=80) printf("C");
if (ave>=61 && ave<=70) printf("D");
if (ave<=60) printf("F");
/* Could be replaced with this code */
if (ave>=91) printf("A");
else if (ave>=81) printf("B");
else if (ave>=71) printf("C");
else if (ave>=61) printf("D");
else printf("F");

Your code (as posted) does not presently meet this requirement:
-store the student names in a two-dimensional array.

You're not 'weighting' the exam scores as directed:
-calculate the final score of each student computed as:
...exam 1 is 20%
...exam 2 is 20%
...exam 3 is 30%
...exam 4 is 30%

and you presently have no code to support calculating a class average.

If I'm reading the assignment right, I suspect what they want you to do is first read all of the data then make a 'report' from the data you read.

You can perform the calculations as you read the data, but then you need to save the results until you're ready to do the report.

your corrected code id

#include<stdio.h>
#include<conio.h>
#define MAXSTUDENTS 15
#define EXAMS 4

int main(void){
char name[20];
int grade[MAXSTUDENTS][EXAMS],total=0,row,col;
float ave;
clrscr();

for(row=0;row<MAXSTUDENTS;row++){

printf("\nEnter First name : ");
scanf("%s",name);

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade);

total+=grade[row][col];

}
ave=total/EXAMS;




}
if (ave>91) printf("A");
if (ave>=81 && ave<=90) printf("B");
if (ave>=71 && ave<=80) printf("C");
if (ave>=61 && ave<=70) printf("D");
if (ave<=60) printf("F");
return(0);}

I didn't notice it before, but

printf("\nExam score : ");
scanf("%d",&grade);

isn't correct.

You might be able to scanf("%d",&grade[row][col]);

I have stated it already in my code

I have stated it already in my code

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade);

That was from your code.

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade);

That was from your code.

I have made some corrections in the beginning

int grade[MAXSTUDENTS][EXAMS],total=0,row,col;

Yes, grade is now a two dimensional array.

Do you always want to put the score in grade[0][0]?

for(row=0;row<MAXSTUDENTS;row++){
printf("\nEnter First name : ");
scanf("%s",name);

As this works in single dimension similarly the below code will work in the two dimension.for(row=0;row<MAXSTUDENTS;row++){

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade);

name was declared as char name[20]; which is enough space for ONE 20 character name and '%s' is DESIGNED to work with character arrays.

grade was declared as int grade[MAXSTUDENTS][EXAMS] which is enough space for 15 students to have 4 exam scores each and "%d" is not designed to work with arrays, it only works with a single integer.

Feel free to run the code...input 4 grades for all 15 students and then try to print back out the data you entered, it won't be there.

Just one mistake i forgot to correct was there after removal of the code is

#include<stdio.h>
#include<conio.h>
#define MAXSTUDENTS 15
#define EXAMS 4

int main(void){
char name[20];
int grade[MAXSTUDENTS][EXAMS],total=0,row,col;
float ave;
clrscr();

for(row=0;row<MAXSTUDENTS;row++){

printf("\nEnter First name : ");
scanf("%s",name);

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade);

total+=grade[row][col];

}
ave=total/EXAMS;





if (ave>91) printf("A");
if (ave>=81 && ave<=90) printf("B");
if (ave>=71 && ave<=80) printf("C");
if (ave>=61 && ave<=70) printf("D");
if (ave<=60) printf("F");
}
return(0);}

The above code was executed succesfully.

Define successfully.
How many grades did it print out?
Were there 15 grades?
Did you attempt to add additional printf's to confirm that the grade array actually got any data in it?

Define successfully.
How many grades did it print out?
Were there 15 grades?
Did you attempt to add additional printf's to confirm that the grade array actually got any data in it?

The later code posted had some changes to that posted before.The output had 15 grades as required.
Please feel free to check the code.

First, this isn't your thread. You aren't supposed to SOLVE the problem for them, THEY are supposed to do most of the work. We only want to help them through the code they don't understand.

The assignment (which I know wasn't yours) was to take 15 students and their exam scores and after all of the input was complete to then produce a report similar to the following:

Name                  E1  E2  E3  E4  Final  Grade
Alice                 90  95  93  92   93      A
...
(repeat for all 15 students)
...
Class average                          75

or something like it.

The names need to be stored (and your sample code does NOT do that). The grades also need to be stored (your code has space for that, but doesn't do it.) The final score is supposed to weight the grades. The program is supposed to calculate the class average final score.

I think i have only corrected the code which was posted .I didn't post the code required which includes the storing the names and numbers of the students so you need not worry about that.

I built and compiled your code.

I added one debug line:

for(row=0;row<MAXSTUDENTS;row++){

printf("\nEnter First name : ");
scanf("%s",name);

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade);

total+=grade[row][col];

printf("grade[row][col]=%d, total=%d", grade[row][col], total);

}

This is the output:

Enter First name : Alice

Exam score : 20
grade[row][col]=20, total=20
Exam score : 20
grade[row][col]=-858993460, total=-858993440
Exam score : 20
grade[row][col]=-858993460, total=-1717986900
Exam score : 20
grade[row][col]=-858993460, total=1717986936A

i also had the same problem
now i was wrong ok. Code should include

for(col=0;col<EXAMS;col++){
printf("\nExam score : ");
scanf("%d",&grade[row][col]);

Thanks Murtan for correcting me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.