Please help with last detail on this

This is a discussion on Please help with last detail on this within the C Programming forums, part of the General Programming Boards category; Why does this code return all the grades as F, and not A B C or D Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    98

    Please help with last detail on this

    Why does this code return all the grades as F, and not A B C or D

    Code:
    #include <stdio.h>
    
    #define N 10
    
    int main(void)
    
    {
    
      double grade[N], highscore, average_score;
      int sum, numraise, studentname, i;
      char score;
      highscore=0.0;
      sum=0.0;
    
      printf("Enter %d grades: ", N);
      for ( i=0 ; i<N ;i++ )
    
      {
       scanf("%lf", &grade[i]);
       sum += grade[i];
       if (highscore < grade[i])
       highscore = grade[i];
    
     if (i >= 90.0)
     score='A';
     else if (i >= 80.0 && i< 90.0)
     score='B';
     else if (i >= 70.0 && i< 80.0)
     score='C';
     else if (i >= 60.0 && i< 70.0)
     score='D';
     else if (i < 60.0)
     score='F';
      }
    
     average_score= sum/10;
    
     for (studentname = 0; studentname < 10; studentname++)
     {
    
    
     printf("Student %d's grade is %.1lf, this is an %c\n", studentname+1, grade[stu
    dentname], score);
     }
    printf("The highest score is %.1lf\n", highscore);
    printf("The average score is %.2lf\n", average_score);
    
    return 0;
    }

    Thank you!
    Last edited by LightYear; 04-09-2010 at 12:02 AM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Why is every grade returned an A?
    Last edited by LightYear; 04-09-2010 at 12:03 AM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Okay it's taking the last entered grade and using that to compute the letter grade for all the inputs, I see that much...

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You're using i as your loop counter, and then comparing it to your grade constants.

    There is no point within your current loop (when N is defined as 10) where i is anything greater than 60.

    I think you want to compare the constants with grade[i] instead.

    Oh, and in the interest of flexibility/ease-of-change, you might want to change average = sum / 10 to average = sum / N. Otherwise, if you change N you will get the wrong average.

    Also, you will probably want to change N to a less common name. I think that the C preprocessor will still replace defined constants if they are in the middle of a word. For example, if you had a variable called Number, it would give you a compiler error because it would replace the N with 10. I don't think this is actually an issue in this particular piece of code, but it's good practice.
    Last edited by DeadPlanet; 04-09-2010 at 03:18 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a duplicate thread, please don't post in it.

    See the real thread "need guidance on project", also by Lightyear, and up to page 5 already.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. How far in detail should I go?
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-22-2007, 02:55 PM
  3. Compilation steps in detail?
    By hardi in forum C++ Programming
    Replies: 18
    Last Post: 12-09-2006, 11:21 AM
  4. Linked Lists in Detail
    By SlyMaelstrom in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2005, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21