# Thread: Student GPA system problems

1. ## Student GPA system problems

Hello Everyone, I have been asked to write a GPA system for my college assignment and i am facing some problems. The question stated that i must use Two Dimensional Array to store the grades of all students in a class. The subjects are a fixed amount of columns which are 5. The rows however are unlimited. How do i declare/ initialize this? I am also unsure on how to store these data's.

This is all i have right now. Can anyone tell me what i am doing wrong? I am still a newbie to C.

Code:
```#include <stdio.h>
#define MAX_COLLUMN 5
#define MAX_ROWS []

int main()
{
// Variable Declarations //
int subjects_taken,Array[MAX_ROWS][MAX_COLLUMN],i,student_no = 1;
double totalmarks,gpa;

// Input //
printf("Enter Number of students to process : ");
scanf("%d", MAX_ROWS);
fflush(stdin);
for(i=0;i<MAX_ROWS;i++)
{
printf("Student #%d\n",student_no);
printf("-----------\n");
student_no++;
for(i=1;i<6;i++)
{
printf("Enter Student #%d's Grade for subject #%d",student_no,i);
Array[MAX_ROWS][i] = 4.0;
Array[MAX_ROWS][i] = 3.0;
Array[MAX_ROWS][i] = 2.0;
Array[MAX_ROWS][i] = 1.0;
Array[MAX_ROWS][i] = 0.0;
}
}
// Process //
totalmarks = Array[MAX_ROWS][0] + Array[MAX_ROWS][1] + Array[MAX_ROWS][2] + Array[MAX_ROWS][3] + Array[MAX_ROWS][4];
gpa = totalmarks / subjects_taken;

// Output //
printf("                                Examination Report                              ");
printf("Student No.                           Subjects                                  ");
printf("            English     Maths       Science     History     Social Studies      ");
printf("    %d                                                                          ");

return 0;
}```

2. You can't put a stop condition on a for loop, that may need to go on indefinitely. You could just leave the semi-colon:

Code:
```for(i=0;;i++) {
for(j=0;j<5;j++) {
}
}```
(You will need a break condition to terminate input from the outer for loop, and I wouldn't do this.)

If you're going to program in C, start counting from zero, not 1, in your loops. It's easier once you do it, and you'll have fewer bugs, also.

Why? It's just the way it is.

What size should your 2D array be? Arrays can be realloc()'d to a larger size, but I suspect you haven't got that far just yet. For now, pick a size 40 students larger than the largest class in your school. Be sure to show ONLY the array that has valid data (count it as it's entered, so you know just how many students were entered).

#define MAX 200

Should be enough but it's not infinite, by any means. If you can write the data out to a file, as it's entered, then you can use code like I showed you, above, and process as many students as your HD and memory, can hold.

AND --> Welcome to the Forum ShiroiShu!

3. 1. Include
Code:
`#include <stdlib.h>`
2. Remove
Code:
`#define MAX_ROWS []`
Code:
`typedef double row_t[MAX_COLLUMN];`
4. Redefine Array as
Code:
`row_t *Array = NULL;`
5. Define
Code:
`int row_count = 0;`
6. Some corrections to your code:
Code:
```printf("Enter Number of students to process : ");
scanf("%d", &row_count);
fflush(stdin);
if (row_count > 0)
{
Array = malloc(row_count * sizeof(row_t));
}
for (i = 0; i < row_count; i++)
{
// instructions
}
// instructions
free(Array);
return 0;```

4. Oh god.. Its still not working !! I have no idea whats wrong!
I've added some of the the things is this how it should look like?
Code:
```#include <stdio.h>
#include <stdlib.h>
#define MAX_COLLUMN 5

int main()
{
// Variable Declarations //
int subjects_taken,row_count = 0 ,Array[200][5], i , j ,student_no = 1,row_t *Array = NULL;;
double totalmarks = 0.0 ,gpa = 0.0;
typedef double row_t[MAX_COLLUMN];

// Input //

printf("Enter Number of students to process : ");
scanf("%d", &row_count);
fflush(stdin);
if (row_count > 0)
{
Array = malloc(row_count * sizeof(row_t));
}
for (i = 0; i < row_count; i++)
{
printf("Student #%d\n",student_no);
printf("-----------\n");
student_no++;
for(i=0;i<5;i++)
{
printf("Enter Student #%d's Grade for subject #%d",student_no,i);
Array[MAX_ROWS][i] = 4.0;
Array[MAX_ROWS][i] = 3.0;
Array[MAX_ROWS][i] = 2.0;
Array[MAX_ROWS][i] = 1.0;
Array[MAX_ROWS][i] = 0.0;
}
}
// Process //
totalmarks = Array[MAX_ROWS][0] + Array[MAX_ROWS][1] + Array[MAX_ROWS][2] + Array[MAX_ROWS][3] + Array[MAX_ROWS][4];
gpa = totalmarks / subjects_taken;

// Output //
printf("                                Examination Report                              ");
printf("Student No.                           Subjects                                  ");
printf("            English     Maths       Science     History     Social Studies      ");
printf("    %d                                                                          ");

return 0;
}```

5. Originally Posted by ShiroiShu
Oh god.. Its still not working !! I have no idea whats wrong!
Ok... "still not working" is of no help to *anyone*.
We need to know what it's doing wrong...
Also post compiler errors and warnings.

Speaking of compiler errors you should be getting lots and lots of them... MAX_ROWS used at least 10 times is undefined.

When your compiler prints a warning or an error, it will give you the line number and a description of the problem...
Listen to what it's telling you! Start at the top of the list and resolve each error in turn, don't just come running here whining that its "still not working". Thank ahead... you're out of school, looking for that first big job... who you gonna run to when things go wrong?

6. Alright you're right.. I am at fault here im on my first year and 2nd semester of college and things are still quite new for me. Some of the problems that the compiler tells me i have no idea how to resolve.
Here are the build Errors:
C:\Users\Win7\wddw\Untitled1.c||In function 'main':|
C:\Users\Win7\wddw\Untitled1.c|10|error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token|
C:\Users\Win7\wddw\Untitled1.c|21|error: incompatible types when assigning to type 'int[200][5]' from type 'void *'|
C:\Users\Win7\wddw\Untitled1.c|45|error: 'MAX_ROWS' undeclared (first use in this function)|
C:\Users\Win7\wddw\Untitled1.c|45|error: (Each undeclared identifier is reported only once|
C:\Users\Win7\wddw\Untitled1.c|45|error: for each function it appears in.)|
||=== Build finished: 5 errors, 0 warnings ===|

Im also not sure where to put this
Code:
` row_t *Array = NULL;`
I haven't even learned some of the functions you posted such as "malloc" and "sizeof". I'm not even sure if the main i'm using is correct.(Could someone explain what are the situations that i must use a void or int main??) I'm sorry if i've made anyone mad.. But i really don't understand 2-dimensional arrays and how to use them. I've read some tutorials on this site but they didnt really help my problem because i only need a maximum of 5 columns and an "N" number of rows. N in which representing undefined. And i dont know how to declare that. Could someone please explain how to declare this type of 2-Dimensional Array? And how to store data in them?
Is this method valid?
Code:
```printf("Enter Student #%d's Grade for subject #%d",student_no,i);
Array[i][i] = 4.0;
Array[i][i] = 3.0;
Array[i][i] = 2.0;
Array[i][i] = 1.0;
Array[i][i] = 0.0;```

Code:
`int subjects_taken,row_count = 0 ,Array[200][5], i , j ,student_no = 1,row_t *Array = NULL;;`
Here you create an array named 'Array', and then try to create a pointer to row_t named 'Array'. Eliminate the Array[200][5] or throw out the malloc business altogether. If you stay with malloc, you need to move
Code:
`row_t *Array ;`
to its own line, it's not an int pointer, its a row_t pointer.

Next,
Code:
`if (grade = 'A')`
You are using the assignment operator and not the equality operator == .

And,
Code:
`Array[MAX_ROWS][i] = 4.0;`
You threw out MAX_ROWS, so it's no longer a declared/defined value. This is also not how you go about addressing a 2D array. If you're just trying to get 1 entry to work, fine, but then hardcode it temporarily to Array[0][i] or something.

And to the last point,
Code:
```for (i = 0; i < row_count; i++)
{
printf("Student #%d\n",student_no);
printf("-----------\n");
student_no++;
for(i=0;i<5;i++)
{```
You are improperly using the same counter variable to control both inner and outer loops.

8. Your first line of code with errors is:
Code:
`int subjects_taken,row_count = 0 ,Array[200][5], i , j ,student_no = 1,row_t *Array = NULL;;`
row_t is an int, but has no comma after it.

*Array is the same name as the Array[200][5] has, so that's no good.

You have two ;; semi-colons on the end of the line, instead of one.

Next is:
Code:
`typedef double row_t[MAX_COLLUMN];`
row_t is already declared as an int. For now, avoid typedef's. I deleted this line, entirely.

Next is:
Code:
```    if (row_count > 0)
{
Array = malloc(row_count * sizeof(row_t));
}```
Array is declared with a fixed size. Delete this line of code. You can't declare array sizes, and then try and malloc them as well.

Next is:
Code:
`if (grade = 'A')`
For a comparison to be made, you need two ==. One = is for assignment only. You have several lines of this, in your code.

Next is in this same loop:
Code:
```    for(i=0;i<5;i++)
{
printf("Enter Student #%d's Grade for subject #%d",student_no,i);
Array[MAX_ROWS][i] = 4.0;
Array[MAX_ROWS][i] = 3.0;
Array[MAX_ROWS][i] = 2.0;
Array[MAX_ROWS][i] = 1.0;
Array[MAX_ROWS][i] = 0.0;
}```
You need to #define MAX_ROWS. Also, you can't use Array[MAX_ROWS][i], in this way, for two reasons:

1) MAX_ROWS is not a valid index number. Highest valid index for Array is Array[MAX_ROWS - 1], because indices in C arrays, begin at 0, not 1.

2) You need a variable here, not a constant value, otherwise you'll assign every GPA to the very last index, only.

I'll show how to do that, in a follow up post. For now, I'm adding
#define MAXROWS 20

Up above int main().

And yes, it's always int main(), never main(), and never void main(). Teachers and some books have used that for years (because it's easier, and a bit complicated), but int main() is what the operating system will use to determine (report) whether your program ran normally (not accurately!) just ran normally, or not. So:

Code:
```int main(void) {

return 0; //return other than zero, by convention, would indicate an error
}```

9. This doesn't do all that you need, but it's a start.

It has some marks assigned to marks[], already -- very handy for testing and when you want to adjust the output.

Code:
```#include <stdio.h>
#include <stdlib.h>
#define MAX_COLUMNS 5
#define MAX_ROWS 2

int main()
{
// Variable Declarations //
int subjects_taken,row_count = 0;
int r,c; //r for row iterator, c for column iterator
double totalmarks = 0.0 ,gpa = 0.0;
int marks[MAX_ROWS][MAX_COLUMNS]={{4,3,4,2,3},{0,1,0,1,0}};

/*

// Input //

printf("Enter Number of students to process : ");
scanf("%d", &row_count);
getchar();
//    fflush(stdin); << delete this, it's undefined
for (r = 0; r < row_count; r++)
{
printf("Student #%d\n",r+1);
printf("-----------\n");

for(c=0,totalmarks=0;c<5;c++)
{
printf("Enter Student #%d's Grade for subject #%d: ",r+1,c+1);
getchar();
marks[r][c] = 4;
marks[r][c] = 3;
marks[r][c] = 2;
marks[r][c] = 1;
marks[r][c] = 0;

totalmarks += marks[r][c];
}

// Process //
gpa = totalmarks / subjects_taken;
}
*/

// Output //
printf("                                Examination Report                            ");
printf("                                   Subjects                                 \n");
printf("Student No.       English      Maths   Science   History  Social Studies     \n");
row_count = MAX_ROWS;  // temporary
for(r=0;r<row_count;r++) {
printf("%8d     ",r+1);
for(c=0;c<MAX_COLUMNS;c++) {
printf("%10d",marks[r][c]);
}
printf("\n");
}

return 0;
}```
You'll have to decide what you want to do with the gpa - it's not being printed out or stored, currently. Every student overwrites the gpa value.

10. Thanks Adak! Now i can understand 2D arrays alot better! Thank you very much for your explanations. This is what i was trying to make. Adak pretty much did all the work but at least i can understand it better now.
Code:
```#include <stdio.h>
#include <stdlib.h>
#define MAX_COLUMNS 5
#define MAX_ROWS 10

int main()
{
// Variable Declarations //
int subjects_taken = 5,row_count = 0;
int r,c; //r for row iterator, c for column iterator
double totalmarks = 0.0 ,gpa = 0.0;
int marks[MAX_ROWS][MAX_COLUMNS]={{0,0,0,0,0},{0,0,0,0,0}};

// Input //

printf("Enter Number of students to process : ");
scanf("%d", &row_count);
getchar();
for (r = 0; r < row_count; r++)
{
printf("Student #%d\n",r+1);
printf("-----------\n");

for(c=0,totalmarks=0;c<5;c++)
{
printf("Enter Student #%d's Grade for subject #%d: ",r+1,c+1);
getchar();
marks[r][c] = 4;
marks[r][c] = 3;
marks[r][c] = 2;
marks[r][c] = 1;
marks[r][c] = 0;

totalmarks += marks[r][c];
}

// Process //
gpa = totalmarks / subjects_taken;
}

// Output //
printf("                              Examination Report                             \n");
printf("                                   Subjects                                  \n");
printf("Student No.         English    Maths    Science   History    Moral    GPA    \n");
for(r=0;r<row_count;r++)
{
printf("%8d      ",r+1);//student no.
for(c=0;c<MAX_COLUMNS;c++)
{
if(marks[r][c] == 4)
printf("         A");
else if (marks[r][c] == 3)
printf("         B");
else if (marks[r][c] == 2)
printf("         C");
else if (marks[r][c] == 1)
printf("         D");
else if (marks[r][c] == 0)
printf("         F");

}
printf("%9.1lf",gpa);
printf("\n");
}

return 0;
}```

11. these 2d arrays appear some how like the cartesian plane or a matrix plane it confuses me i'll check the notes of adak to clarify my doubts