# Thread: need help with these

1. ## need help with these

im a student, and im currently working on a program to calculate GPA, but it seems doesn't run...
can anyone help me?

Code:
```#include <stdio.h>
#include <string.h>
#define CLASS_SIZE 3

void main()
{
int mark[CLASS_SIZE][2];
float gp[CLASS_SIZE][2], gpa[CLASS_SIZE];
int student, unit;

for (student=1;student<=CLASS_SIZE;++student){
for (unit=1;unit<=2;++unit){
printf("Please enter the mark for subject no.%d for student no.%d:", unit, student);
scanf("%d", &mark[student][unit]);
if (mark[student][unit]<=100||mark[student][unit]>=80)
gp[student][unit] = 4.00;
else if (mark[student][unit]<=79||mark[student][unit]>=75)
gp[student][unit] = 3.75;
else if (mark[student][unit]<=74||mark[student][unit]>=70)
gp[student][unit] = 3.50;
else if (mark[student][unit]<=69||mark[student][unit]>=65)
gp[student][unit] = 3.00;
else if (mark[student][unit]<=64||mark[student][unit]>=60)
gp[student][unit] = 2.75;
else if (mark[student][unit]<=59||mark[student][unit]>=55)
gp[student][unit] = 2.50;
else if (mark[student][unit]<=54||mark[student][unit]>=50)
gp[student][unit] = 2.00;
else if (mark[student][unit]<=49||mark[student][unit]>=45)
gp[student][unit] = 1.75;
else if (mark[student][unit]<=44||mark[student][unit]>=40)
gp[student][unit] = 1.00;
else if (mark[student][unit]<=39||mark[student][unit]>=0)
gp[student][unit] = 0.00;
else
gp[student][unit] = 0.00;

if (gp[student][unit] == 4.00)
else if (gp[student][unit] == 3.75)
else if (gp[student][unit] == 3.50)
else if (gp[student][unit] == 3.00)
else if (gp[student][unit] == 2.75)
else if (gp[student][unit] == 2.50)
else if (gp[student][unit] == 2.00)
else if (gp[student][unit] == 1.75)
else if (gp[student][unit] == 1.00)
else
}
}

for (student=1;student<=CLASS_SIZE;++student)
}```

2. Arrays start at zero, not one.
I believe you'll find the answer to your other problems in the FAQ, in particular:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385

3. Also, this code:
Code:
```		if (mark[student][unit]<=100||mark[student][unit]>=80)
gp[student][unit] = 4.00;
else if (mark[student][unit]<=79||mark[student][unit]>=75)
gp[student][unit] = 3.75;
....
else if (mark[student][unit]<=39||mark[student][unit]>=0)
gp[student][unit] = 0.00;
else
gp[student][unit] = 0.00;```
Can be written like this:
Code:
```		if (mark[student][unit]>=80)
gp[student][unit] = 4.00;
else if (mark[student][unit]>=75)
gp[student][unit] = 3.75;
....
else
gp[student][unit] = 0.00;```
There is no point in checking the upper bound on each statement - you already know if the that the value is below this point when you get past the else. And the final step will be 0 anwyays, because it won't get there unless it's below 39.

[You may of course want to check for entries of greater than 100 or less than 0 - I guess negative scores and scores over 100 are invalid, so should really ask again for those].

Also, it's very picky, but technically:
Code:
`if (gp[student][unit] == 4.00)`
is comparing a "float" with a "double", which can lead to problems - [you are also assigning doubles into floats, but that's slightly less of a problem]. The problem with comparing a small float with a bigger double is that the smaller item has to be "enlarged", which is done by adding extra zeros onto the end of the number. In this particular case, it's not too bad, but try something like 0.7 or 1.4, and you'll see that it fails - because 1.4 isn't possible to describe PRECISELY with binary floating point numbers.

You should use float precision postfix:
[code]
Code:
```gp[student][unit] = 4.00f;
...
if (gp[student][unit] == 4.00f)```
--
Mats