1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple mistake but stuck

Discussion in 'C' started by PEANUTS, Jan 10, 2007.

  1. PEANUTS

    PEANUTS New Member

    Joined:
    Oct 13, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    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);
    	  }
    
    
    	//////////////////////////////////////////////////////////
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  3. PEANUTS

    PEANUTS New Member

    Joined:
    Oct 13, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     
  5. PEANUTS

    PEANUTS New Member

    Joined:
    Oct 13, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    int Students = 4;
    int Pass = 2;

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

    I hope this clears things up.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    DStudent=100*(Distinction/TStudent);

    Try DStudent=(100*Distinction)/TStudent;
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     
  9. PEANUTS

    PEANUTS New Member

    Joined:
    Oct 13, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Thank you for clearing that up. Much appreciated.
     

Share This Page