Thread: need help with these

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    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];
    	char grade[CLASS_SIZE][2];
    	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)
    			strcpy(grade[student][unit],'A');
    		else if (gp[student][unit] == 3.75)
    			strcpy(grade[student][unit],'A-');
    		else if (gp[student][unit] == 3.50)
    			strcpy(grade[student][unit], 'B+');
    		else if (gp[student][unit] == 3.00)
    			strcpy(grade[student][unit], 'B');
    		else if (gp[student][unit] == 2.75)
    			strcpy(grade[student][unit], 'B-');
    		else if (gp[student][unit] == 2.50)
    			strcpy(grade[student][unit], 'C+');
    		else if (gp[student][unit] == 2.00)
    			strcpy(grade[student][unit], 'C');
    		else if (gp[student][unit] == 1.75)
    			strcpy(grade[student][unit], 'C-');
    		else if (gp[student][unit] == 1.00)
    			strcpy(grade[student][unit], 'D');
    		else
    			strcpy(grade[student][unit], 'F');
    		}
    	}
    
    	for (student=1;student<=CLASS_SIZE;++student)
    		printf("%s	%s	%.2d	%.2d\n", grade[student][1], grade[student][2], gp[student][1], gp[student][2]);
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed