Thread: Need guidance on project

  1. #31
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Good job LightYear! It works now.

  2. #32
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by LightYear View Post
    Yay it works!


    Now I just need to modify it so that it displays a letter grade next to each student's grade...


    I was going to use an if (grade>=90, grade=='A')
    else if (grade>=80, grade=='B')
    etc

    but would that go inside the second for loop?
    Yes be careful with syntax though:

    if(grade >= 90) grade = 'A';

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The inner loop refers to each individual score.

    If you want a letter grade as a summary, then do it after that inner loop.

  4. #34
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Code:
    #include <stdio.h>
    
    #define N 10
    
    int main(void)
    
    {
    
      double grade[N], highscore, average_score, sum;
      int i, numraise, studentname, 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];
    
      }
    
     average_score= sum/10;
    
     for (studentname = 0; studentname < 10; studentname++)
     if (score >= 90)
     score='A';
     else if (score >= 80)
     score='B';
     else if (score>= 70)
     score='C';
     else if (score>=60)
     score='D';
     else score='F';
     {
     printf("Student %d's grade is %.1lf, this is a %d", studentname+1, grade[studen
    tname], grade);
     }
    
    
    
    
    printf("The highest score is %.1lf\n", highscore);
    printf("The average score is %.2lf\n", average_score);
    
    return 0;
    }


    hmmm it returns this when executed:


    admiral% gcc testgrades.c
    admiral% a.out
    Enter 10 grades: 10 10 10 10 10 10 10 10 10 90
    Student 11's grade is 0.0, this is a -4195384The highest score is 90.0
    The average score is 18.00


    What do you think? (thanks for the help btw this is very interesting)


    So average score looks good, and highest, but where did this student number 11 stuff come from?


    NOTE: oh crap that bracket is misplaced after the for loop isnt it let me go fix that

  5. #35
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) score is undefined. You need to compare grade[i] not score. Also, when comparing make sure you compare double values to double values:

    i.e. if(grade[i] >= 90.0)

    2) when printing out the grade the correct flag is %c not %d because it is a char not an int. SCORE should be type char.

    I'm going offline now, it's late here and it's time for my beauty sleep.

    Good luck!

  6. #36
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    now with the bracket at

    Code:
     average_score= sum/10;
    
     for (studentname = 0; studentname < 10; studentname++)
     {   // Bracket moved here
     if (score >= 90)
     score='A';
     else if (score >= 80)
     score='B';
     else if (score>= 70)
     score='C';
     else if (score>=60)
     score='D';
     else score='F';
     {        //This removed
     printf("Student %d's grade is %.1lf, this is a %d", studentname+1, grade[studen
    tname], grade);
     }
    produces
    admiral% gcc testgrades.c
    admiral% a.out
    Enter 10 grades: 10 10 10 30 30 30 90 90 90 100
    Student 1's grade is 10.0, this is a -4195384Student 2's grade is 10.0, this is a -4195384Student 3's grade is 10.0, this is a -4195384Student 4's grade is 30.0, this is a -4195384Student 5's grade is 30.0, this is a -4195384Student 6's grade is 30.0, this is a -4195384Student 7's grade is 90.0, this is a -4195384Student 8's grade is 90.0, this is a -4195384Student 9's grade is 90.0, this is a -4195384Student 10's grade is 100.0, this is a -4195384The highest score is 100.0
    The average score is 49.00

  7. #37
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Oh I thought characters were treated as integers oops

  8. #38
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    How's this

    Code:
    #include <stdio.h>
    
    #define N 10
    
    int main(void)
    
    {
    
      double grade[N], highscore, average_score, sum;
      int i, numraise, studentname,;
      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];
    
      }
    
     average_score= sum/10;
    
     for (studentname = 0; studentname < 10; studentname++)
     {
    
     if (score >= 90)
       sum += grade[i];
       if (highscore < grade[i])
       highscore = grade[i];
    
      }
    
     average_score= sum/10;
    
     for (studentname = 0; studentname < 10; studentname++)
     {
    
     if (i >= 90.0)    
     score='A';
     else if (i >= 80.0)
     score='B';
     else if (i>= 70.0)
     score='C';
     else if (i>=60.0)
     score='D';
     else score='F';
    
     printf("Student %d's grade is %.1lf, this is a %c", studentname+1, grade[studen
    tname], score);         //Replaced grade here with score
     }  
    
    
    
    
    printf("The highest score is %.1lf\n", highscore);
    printf("The average score is %.2lf\n", average_score);
    
    return 0;
    
    }


    Still getting an error, but it's only some syntactical error. The logic is right tho yes?
    Last edited by LightYear; 04-08-2010 at 11:01 PM.

  9. #39
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    last question I promise!


    Here is near final code

    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];
    
    
      }
    
     average_score= sum/10;
    
     for (studentname = 0; studentname < 10; studentname++)
     {
    
     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';
    
     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;
    }


    Why does it print all grades as being F?

    Should I move the if then statements into the first for loop?

  10. #40
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because i is never more than 10, let alone 60.

  11. #41
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Oh so it would be grade[i] > 90 ?

  12. #42
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    I substituted grade[i] in for every i in the if then statements and now all grades are A's !!!

  13. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    score is a double, so you want to add up a student's marks, and put them into score.

    First, set score to 0.0 (I believe you do that)

    Then every time through the loop, you should be having:
    score += grade[i];

    that line of code in your for loop.

    Letters play no part in this. Letters should go to your char variable you want to use for the student's finalGrade.

    Make sense?

    Just replace "studentName" with i, FCOL. Use i for your loop iterator, if possible!

  14. #44
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    so at the beginning,

    char score, A, B, C, D, F;

    but score is a char, not a double?

    I'm a little confused as to what the score += grade[i] will do for me being that score is a char letter.

    I am going to rename score as "Lettergrade" to make it easier to understand.

  15. #45
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Code:
    #include <stdio.h>
    
    #define N 10
    
    int main(void)
    
    {
    
      double grade[N], highscore, average_score;
      int sum, numraise, studentname, i;
      char lettergrade;
      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 (grade[i] >= 90.0)
      lettergrade='A';
      else if (grade[i] >= 80.0 && grade[i] <90.0)
      lettergrade='B';
      else if (grade[i] >= 70.0 && grade[i] <80.0)
      lettergrade='C';
      else if (grade[i] >= 60.0 && grade[i] <70.0)
      lettergrade='D';
      else if (grade[i] < 60.0)
      lettergrade='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], lettergrade);
     }
    
    
    
    
    printf("The highest score is %.1lf\n", highscore);
    printf("The average score is %.2lf\n", average_score);
    
    return 0;
    }

    see what i mean?
    Last edited by LightYear; 04-08-2010 at 11:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Guidance for a project idea in C\C++
    By wrkrishna in forum C Programming
    Replies: 2
    Last Post: 12-10-2005, 12:23 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM