Thread: Help Have no idea with this proplem

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    Exclamation Help Have no idea with this proplem

    Hello Please Help me with this proplem

    i know how to make the input and use loop to ask user to input
    50 grade
    but
    how can i count the number of the student that grade more than 85
    and the student that grade more than 75


    write a program that reads in the grades of 50 students in a course (out of 100 points each ) and then count the number of A students ( grade > 85 ) and the number of B students (grade > 75 ).


    Thank You

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    Code:
    #include<stdio.h>
    
    int main()
    
    {
        double G;
        printf("Please Input your Grade:> ");
    
        scanf("%lf",&G);
        if(G>85){printf("A");}
        else if (G>75){printf("B");}
    
        return 0;
    
    }


    can i make this code as function

    and make loop with it but also how can i count the student that grade more than 85 and more than 75 ??

    i know this is normal proplem but i have no idea about count the student

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    hey i have this idea i will make normal code
    user will input grade and if it is more than 75 i will use GOTO and +1 to var the same thing for more than 85 and after the code will add 1 to the var will use another GOTO the code that will ask user for input and loop all that 50 time
    then i print the var1 and var2 that +1
    but i think this is playing not programming
    not good algorithm also the code must be fast and small

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by tost View Post
    can i make this code as function
    It is already a function, named main().

    You can use another name and call it inside main ...
    Code:
    int anothername() { /* your code */ }
    
    int main() { othername(); }

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    yes thank you i was mean i will make it func with another name to make new main cause i cannot make more than 1 main
    but please help me i cannot write this program

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, read this to learn a bit about taking things slowly, and breaking the problem down into smaller steps.
    A development process

    For example, make sure you can do the assignment for one student.

    When it works, then it's simply a matter of adding a for() loop around the code which does the work for one student, to do it 50 (or however many you want) times.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    yes mr.Salem i did i red this topic last week
    but i realy cannot answer this proplem so please help me

    Thank you

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You seemed to be largely on the right track with post #4, except for the goto nonsense.

    Code:
        if(G>85){printf("A");}
        else if (G>75){printf("B");}
    You already have the logic, so instead of printing 'A', how about say
    Code:
    if(G>85){numStudentsWithAGrade++;}
    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.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    yea you know when you i saw how about you say i got jerk hahaha that's easy if the condition true i wil add 1 to var
    then i print the var that will be have the number of every time the condition was true i got it thank you

    and i knew why i was not able to write it
    proplem was how can i make var that count the number of the student in every condition so i Used Goto to make it got to var and +1 then i print it
    it was long way bad too

    then you told me to put the +1 but short way

    sorry for bad english
    thanks again

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    your code is already asking for an input, so put a for loop around it

    Code:
    for(....)
    {  YOUR CODE
    }
    then you are asking how to count something. that is pretty easy. Just use an IF and then put the appropriate condition (inside the loop of course)
    Code:
    if(condition) counter++
    and forget all about the GOTO nonsense. learn the mantra. You should not use goto in C. You dont need it. (said in a Jedi voice :P )

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    thank you KansaiRobot


    Code:
    #include<stdio.h>
    int main()
    {
    int count =0;
    int A=0,B=0,C=0,D=0;
    char into;
    printf(" A for mathematics\n B for ecnomics\n C for programming\n D for physics\n");
    while(count<=20)
    {
    printf(" what is your favorite subject\n");
    scanf("%c",&into);
    if(into =='A'){A+=1;}
    else if (into == 'B'){B+=1;}
    else if (into =='C'){C+=1;}
    else if (into =='D'){D+=1;}
    
    count++;
    }
    printf("Number of people who chose A answer =%d\n",A);
    printf("Number of people who chose B answer =%d\n",B);
    printf("Number of people who chose C answer =%d\n",C);
    printf("Number of people who chose D answer =%d\n",D);
    
    return 0;
    
    }
    Where is the error in this code whene i run it
    it print every thing 2 times Why

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tost
    Where is the error in this code whene i run it
    it print every thing 2 times Why
    Probably because it reads the character entered on one iteration, then it reads the newline character from entering the previous character on the next iteration.

    By the way, you need to indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    okay but what must i do

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One way is to change the "%c" to " %c" so that the space will match the whitespace that is the newline, hence effectively discarding it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proplem with TETRIS game in C++
    By Dylan Metz in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2012, 12:49 AM
  2. proplem with function
    By cprog12 in forum C Programming
    Replies: 2
    Last Post: 12-10-2010, 04:33 PM
  3. Proplem with keyStroke (ALT-x)
    By meokhung1980 in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2008, 04:57 PM
  4. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM
  5. proplem searching runtime arguments
    By angelfly in forum C Programming
    Replies: 4
    Last Post: 04-28-2002, 03:49 PM