Thread: New guy help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    New guy help

    All

    First thanks for whatever help you can offer. i am trying to finish up a degree is statistics and the powers that be say i have to have a programming class. I am woefully uneducated when it comes to everything besides email and google. So I humbly beg of your help.

    My second assignment goes something like this:

    write a program to convert test scores from numeric to letter grade

    Question one:

    what is the best way to do inequalities? I can get "(score >= 90) printf("Grade is A) but I can only use one other inequality because of the left side right side thing.

    Question two:

    Teacher wants program to loop so user can continue to enter grades. I have the loop set up, but don't know how make the program read y and n for commands to yes repeat or no stop.

    Any help that can be offered would be greatly apprecaited. I am currently reading the rules on how to post code.

    chip

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Code:
    /*Define maximum and minimum numeric grade values*/
    #define LOW 0
    #define HIGH 100
    #define YES 1
    #define NO 0
    #define A 90
    #define B 80
    #define C 70
    #define D 60
    #define F 59
    
    int grading (int score);
    
    int main()  {
    
    	int a_score, process, again, intvar;
    
    	again = YES;
    	printf("This program will assign a letter grade to a numeric test grade");
    
    	while ( again == YES) {
    		/*prompt user and call function to get the test grade, assign returned value to grade in main */
    		printf("\n\nPlease enter the numeric test grade.  Grade must be between %d and %d:  ", LOW, HIGH);
    		scanf("%d%*c",&a_score);
    		process = grading (a_score);
    		/*prompt user and call function to determine whether another test grade will be entered*/
    		printf("\nWould you like to enter another test grade? y/n");
    		scanf("%d%*c", &again);
    	}
    
    return 0;
    
    }
    
    int grading (int score)
    {
    
    		if (score >= A)
    			printf("Grade is A");
    		if (score < A)
    			printf("Grade is B");
    
    		return YES;
    
    }
    
    int repeat (again)
    {
    
    	while (again == 'y' || again == 'Y')
    		return YES;
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    For question one you need to use else statement in order to only print one result.
    Code:
    if(score >= 90)
      printf("Grade is A\n");
    
    else if(score >= 80)  //if is not >= than 90, but >= than 80..
      printf("Grade is B\n";
    
    else if(score >= 70) //If is not >= than 80, but >= than 70
      printf("Grade is C\n");
    There are also constants called "true" and "false" which can be used instead of your defined yes/no
    Last edited by 39ster; 09-07-2008 at 08:01 PM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    [QUOTE=39ster;785323]For question one you need to use else statement in order to only print one result.
    QUOTE]

    It seems so obvious when someone tells you how to go about it. I tried similar things to that. i just didn't realize the above statements would bound the ones below. Thanks very much for the help.

    Chip

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by chip.litch View Post
    i just didn't realize the above statements would bound the ones below.
    They are only bound with an else clause. The else if statement really is a shortcut for the following:
    Code:
    if (grade >= 81) {
       printf ("A");
    } else {
       if (grade >= 70) {
           printf ("B");
       } else {
          if (grade >= 60) {
             printf ("C");
          }
       }
    }
    Which of course is ugly.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    An alternative solution is something like this:
    Code:
        char letter = 'F';   
        if (grade >= 60) letter = 'D';
        if (grade >= 70) letter = 'C';
        if (grade >= 80) letter = 'B';
        if (grade >= 90) letter = 'A';
    I'm not suggesting you do this - it's pretty inefficient, but it is sometimes useful to know that you can do this sort of thing. It is of course important to have things in the right order, the following would obviously not work right:
    Code:
        char letter = 'F';   
        if (grade >= 60) letter = 'D';
        if (grade >= 70) letter = 'C';
        if (grade >= 90) letter = 'A';
        if (grade >= 80) letter = 'B';
    --
    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.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    int main()  {
    
        int a_score, process, again, intvar;
    
        again = YES;
        printf("This program will assign a letter grade to a numeric test grade");
    
        while ( again == YES) {
            /*prompt user and call function to get the test grade, assign returned value to grade in main */
            printf("\n\nPlease enter the numeric test grade.  Grade must be between %d and %d:  ", LOW, HIGH);
            scanf("%d%*c",&a_score);
            process = grading (a_score);
            /*prompt user and call function to determine whether another test grade will be entered*/
            printf("\nWould you like to enter another test grade? y/n");
            scanf("%d%*c", &again);
            /* You do not break out of the loop. So you have an infinite loop.
             * Probably because it's a work-in-progress. */
         }
    
    return 0;
    
    }
    
    int repeat (again) /* You do not have a type here */
    {
    
        while (again == 'y' || again == 'Y')
            return YES;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anybody know this guy?
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-03-2003, 01:35 PM
  2. A little help for a new guy? Functions problem!
    By dustmanx in forum C++ Programming
    Replies: 10
    Last Post: 09-13-2002, 08:30 AM
  3. a british question/joke
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-02-2002, 01:05 PM
  4. Guy or Girl
    By golfinguy4 in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 03-14-2002, 05:00 AM
  5. SpongeBob avatar guy. Who are you!
    By sean in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-03-2002, 11:10 AM