Thread: 12 hours later I am giving a c exam need help solving sample question

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    14

    12 hours later I am giving a c exam need help solving sample question

    Question:
    Swinburne Sarawak Grading Scheme:


    Student mark (0-100) Corresponding grade
    85 – 100 “H”
    75 – 84 “D”
    65 – 74 “C”
    50 – 64 “P”
    0 – 49 “N” (4 points) Draw a flowchart to:
    · get one student’s mark · decide and display which grade the mark is in ( make a series of if / else decisions) - 1 pt · add 1 (one) to the corresponding counter for grade HD and grade N
    · ask if this is the last student · if not the last student, return to the start to get the next student’s mark
    · if no more student, find if the number of students who score H is bigger than the number of students who score N
    o if yes, print “Good Result” o else, print “Poor Result’ mY ANSWER NEDDS EDITING and has some error please correct:
    Code:
    #include<stdio.h>
    int main()
    {
         float mark;
         int hd=0;
         int l=0;
         char decision;
         
         do{
              printf("Enter mark of student:");
              scanf("%f",&mark);
              if(mark<=100 && mark>=85)
              {
                    printf("H\n");
                    
                               
              } 
              if(mark<=84 && mark>=75)
         
              {
                   printf("D\n");
               
              }
             
              if(mark<=74 && mark>=65)
         
              {
                   printf("C\n");
             
              }
              if(mark<=64 && mark>=50)
         
              {
                   printf("P\n");
             
              }
              if(mark<=49 && mark>= 0)
              {
                   printf("N\n");
         
                   
              }
             
              if(mark<=100 && mark>=85)
              { 
              hd=0;
              l=hd+l;
              l++;
              
             }      
                    
              printf("Is this the last student?\n");
              scanf("%ch",&decision);
              
         }while(decision!='y');
         
                              
         if(hd>l)
         {
              printf("Good Result");
         } 
         else
         {
              printf("Poor Result");
         }
         
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add a space after the %f, on line 11

    Add the int variable's of h and n. Assign zero to both of them. Remove l.

    If the marks are either earning an H or an N, then h++ or n++.

    and welcome to the forum!

    P.S. i guess "hd" could work for the "h" variable, just fine.

    You'll need to replace the el variable, though. Looks too much like a 1 or an I.
    Last edited by Adak; 10-04-2012 at 08:37 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What does "some error" mean?

    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:67:1: warning: control reaches end of non-void function [-Wreturn-type]
    Edit:
    > Add a space after the %f, on line 11
    Adding a space before the %c would be better.

    Further, "%ch" really doesn't do what you think it should.

    Unless you insist on typing
    "yh"
    or
    "nh"
    for every response to the yes/no question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    where should I write h++ and n++?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    line 15 for h++.

    line 44-50 should be deleted.

    line 40 should be n++.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    But in the sample question it is said to ask the user if it is the last student?. We are supposed to take the marks multiple students. If the total number of H and D exceeds the number of N then good result is displayed. Can you please show in proper codes where I am going wrong?
    Last edited by Burns111; 10-04-2012 at 10:17 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No I won't do that.

    When you enter a high score, you add one to the number of students with a high score, h++ (or hd if you like)
    When you enter a low score, you add one to the number of students with a low score: n++;

    After you have entered all the students scores, you compare to see if (h > n) or not. If so, then print Good Result, if not, print Bad Result.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    14
    I finally got it right!
    Code:
    #include<stdio.h>
    int main()
    {
         float mark;
         int h=0;
         int n=0;
         char decision;
         
         do{
              printf("Enter mark of student:");
              scanf("%f",&mark);
              if(mark<=100 && mark>=85)
              {
                    printf("H\n");
                    h++;
                               
              } 
              if(mark<=84 && mark>=75)
         
              {
                   printf("D\n");
                   h++;
                   
              }
             
              if(mark<=74 && mark>=65)
         
              {
                   printf("C\n");
             
              }
              if(mark<=64 && mark>=50)
         
              {
                   printf("P\n");
             
              }
              if(mark<=49 && mark>= 0)
              {
                   printf("N\n");
                   n++;
                   
              }
             
              printf("Is this the last student?\n");
              fflush(stdin);
              scanf("%c",&decision);
              
         }while(decision!='y');
                             
         if(h>n)
         {
               printf("Good Result");
         } 
         else
         {
               printf("Poor Result");
         }
         
    }

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Look for fflush(stdin) on the FAQ -> you shouldn't use it.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I finally got it right!
    Erm, no you didn't.

    > fflush(stdin);
    You put this in, instead of reading posts from myself and Adak regarding the use of space in format strings.
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two suggestions Burns:

    Code:
              fflush(stdin);           //remove this fflush() works on OUTWARD streams, not inward ones
              scanf(" %c",&decision);  //note the space added before the % - will help, here
    And well done!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exam question
    By Fahad Ch in forum C Programming
    Replies: 6
    Last Post: 04-27-2012, 08:34 AM
  2. help with question from exam in c...
    By engti in forum C Programming
    Replies: 9
    Last Post: 02-11-2012, 07:42 AM
  3. Help solving a question
    By everyone0 in forum C Programming
    Replies: 9
    Last Post: 05-06-2010, 11:09 AM
  4. another exam question
    By rjeff1804 in forum C Programming
    Replies: 4
    Last Post: 02-12-2003, 10:29 PM
  5. exam question
    By teja22 in forum C Programming
    Replies: 2
    Last Post: 10-08-2001, 08:03 PM