Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Simple mistake but stuck (http://www.go4expert.com/forums/simple-mistake-stuck-t2513/)

PEANUTS 10Jan2007 21:40

Simple mistake but stuck
 
The code works but the percentages dont work correctly. They register has 0. A litle help needed please


Code:

/* Header.c */
#include <stdio.h>

//////////////// Prototypes /////////////////////////////////////////////////////

void OutputHeader();
void Variable();
void Student();
void Grades();
void Summary();


//////////////////////// Global Variables //////////////////
float StudentsName, DOB, Test1, Test2, Test3, Test4, Average, Grading, SumOfMarks, Exit;
int Pass, Merit, Distinction, Fail, y=2, TStudent, DStudent, MStudent, PStudent, FStudent;
char Name[15];
char Flag;
///////////////////////////////////////////////////////////////////////////

void main (void)
{
        OutputHeader();
        Variable();
        Flag=1;
        scanf("%s",Name);
        Flag=stricmp(Name, "exit");

                        while (Flag !=0)
                {
                        Student();
                        Grades();
                        y++;
                        scanf ("%s", Name);
                        Flag = stricmp(Name, "exit");
                }
                        Summary();








getch();

}

///////////////////////////////// My Functions ///////////////////////////////////

void OutputHeader()
        {
        clrscr();
        gotoxy(1, 1);
        printf("Students Name  D.O.B  Test 1  Test 2  Test 3  Test 4  Average  Grading\n");
        }


void Variable()
        {
        Pass=0;
        Merit=0;
        Distinction=0;
        Fail=0;
        }

void Student()
        {
        gotoxy(16, y);
        scanf("%f", &DOB);
        gotoxy(24, y);
        scanf("%f", &Test1);
        gotoxy(32, y);
        scanf("%f", &Test2);
        gotoxy(40, y);
        scanf("%f", &Test3);
        gotoxy(48, y);
        scanf("%f", &Test4);

        /* CalcAverageMark */
        SumOfMarks = Test1 +Test2 + Test3 + Test4;
        Average = SumOfMarks / 4;
  gotoxy(53, y);  printf("%6.0f\n", Average);

  /*UpdateGrades*/



        {
        gotoxy (68,y);
        if (Average>=85)
        printf("D");

        else

        if (Average>=65)
        printf("M");

        else

        if (Average>=40)
        printf("P");

        else
        printf("F");
        }
}
        void Grades()
        {
        if (Average>=85)
        ++Distinction;

        else

        if (Average>=65)
        ++Merit;

        else

        if (Average>=40)
        ++Pass;

        else
        ++Fail;
        }


        void Summary()
                {

                TStudent=Distinction+Merit+Pass+Fail;
                FStudent=100*(Fail/TStudent);
                PStudent=100*(Pass/TStudent);
                MStudent=100*(Merit/TStudent);
                DStudent=100*(Distinction/TStudent);


        printf("Summary\n");
        printf("Total number students processed:> %d\n", TStudent);
        printf("Average of all students average mark:> %d\n", Average);
        printf("Percentage of students with a D:> %d\n", DStudent);
        printf("Percentage of students with a M:> %d\n", MStudent);
        printf("Percentage of student with a P:> %d\n", PStudent);
        printf("Percentage of students with a F:> %d\n", FStudent);
          }


        //////////////////////////////////////////////////////////


shabbir 10Jan2007 21:48

Re: Simple mistake but stuck
 
Can you provide a bit more as to what is not working as looking at the total code seems not what I am enjoying without knowing what its suppose to do and what its doing.

PEANUTS 10Jan2007 22:03

Re: Simple mistake but stuck
 
Void Summary.
printf("Percentage of students with a D:> %d\n", DStudent);
printf("Percentage of students with a M:> %d\n", MStudent);
printf("Percentage of student with a P:> %d\n", PStudent);
printf("Percentage of students with a F:> %d\n", FStudent);
}


It should display the percentage of students with that grade. But it displays 0

DaWei 10Jan2007 23:28

Re: Simple mistake but stuck
 
Any percentage less than 100 is a fraction. Are you trying to print that fraction as an integer? Just curious, looks that way from the snippet.

PEANUTS 11Jan2007 04:03

Re: Simple mistake but stuck
 
I'm trying to print the percentage of how many students got that grade.

e.g

If 4 students took the exams
2 got Pass
2 got Merit

Then Pass would be 50%
Merit would be 50%

I hope this clears things up.

DaWei 11Jan2007 04:54

Re: Simple mistake but stuck
 
int Students = 4;
int Pass = 2;

Pass/Students = 2 / 4 = ? (Zero, that's what. These are integers.)

I hope this clears things up.

shabbir 11Jan2007 08:11

Re: Simple mistake but stuck
 
DStudent=100*(Distinction/TStudent);

Try DStudent=(100*Distinction)/TStudent;

DaWei 11Jan2007 08:45

Re: Simple mistake but stuck
 
That will work, so long as the category is at least 1% of the students. Four distinctions out of a thousand students would still fail, though. I would convert the category and the number of students to floats, do the division, and convert back. I would definitely multiply first, as Shabbir shows, since a larger numerator will usually result in more accuracy. Any piddling decimal fractions that occur will be truncated on the reconversion to int, or you could round.

PEANUTS 11Jan2007 23:57

Re: Simple mistake but stuck
 
Thank you for clearing that up. Much appreciated.


All times are GMT +5.5. The time now is 06:55.