Thread: Need guidance on project

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

    Need guidance on project

    Hi,
    I am writing a piece of code for class. I am getting quite lost, so I was hoping perhaps I could get some guidance/direction. I didn't want to ask this early without having a complete program to post for review, but I'm not even sure how to construct the logic.

    This is the question:

    Suppose we have 10 students taking a test. Scores 0-100 can be with one decimal fraction. First implement the simpler problem for 60%, then extend to the full requirements.
    Write a program that will read the scores into an array.

    Simple version: After reading the scores, display all scores, then the average.
    Student 1 score 95.0
    ....
    Student 10 score 85.5
    Test average 95.53
    // note that scores all display 1 decimal digit and are lined up, average displays 2 decimal digits rounded.

    Full version: Modify the display to display the letter grade A, B, C, D, or F of using the standard 10% scale.
    Student 1 score 95.0 grade A
    //etc

    Extra 10: At the end, display the lowest and the highest score, using functions
    float findLowest(float [], int)
    float findHighest(float [], int).
    If there are ties, pick any.



    And this is what I have so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define N 10
    int main(void)
    float grade[N], highscore, average_score;
    int x, i, numraise, studentname;
    
    for ( i<N; i=0 ;i++ )
     {
    printf("Enter a grade: ");
    scanf("%f", &grade[x])
     }
    while (0=numraise && numraise<=9, studentname=numraise+1, numraise++)
     {
    printf("Student %d's grade is %.1f", studentname, grade[x])
     }
    return 0;
    }


    What I am really unsure of is that in the printf line that is supposed to print studentname (student1...student2..student3...etc) how will I get the matching grade (grade[x]) to print alongside it?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    then after the last printf call I will need a
    printf("The class average is %.2f", average)

    where
    average=

    How do I get it to tally up all the elements of the array and divide by the number of elements in the array.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A few pointers:

    1) Think of the logic without thinking about the programming language. Just imagine i gave you a piece of paper on which I wrote the input for a problem and ask you to do all of that. List the steps you would take to do it. This will help you clear your head and differentiate between the logic of the ALGORITHM and the logic of the PROGRAM.

    2) Don't use floats! Use doubles instead. Float has poor precision.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    I think I'm getting somewhere

    Code:
    #define N 10
    int main(void)
    double grade[N], highscore, average_score;
    int x, i, numraise, studentname;
    highscore=0;
    while ( i<N; i=0 ;i++ )
     {
    printf("Enter %d grades: ", N);
    scanf("%lf", &grade[x]);
    
    if (highscore<=x)
     x=highscore;
     }
    while (0=numraise && numraise<=9, studentname=numraise+1, numraise++)
     {
    printf("Student %d's grade is %.1lf\n", studentname, grade[x])
     }
    printf("The highest score is %.1lf", highscore);
    printf("The average score is %.1lf", average_score);
    
    return 0;


    How does one get an array to sum all the scores entered and print them though?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) You are mistaking a while loop for a for loop. You are using the structure of a for loop so just change while to for everywhere.

    2) Take out the printf statement from the first loop and move it above it, if you dont want it to print "Enter 10 grades" ten times.

    3)You want to scanf grade[i] not grade[x] since i is the counter of that loop. x is undefined at that point (contains some garbage value).

    4)Your if inside the first loops is wrong. What you want to do is:

    a) initialize highscore to 0.0 at declaration.
    b) if(highscore < grade[i] highscore = grade[i]. (i.e. if the number I have just read is higher than the highscore, make highscore the new number.

    5) To calculate the average you can have another variable say sum which is type double and initialized to 0.0 at the beginning of the program. As you read another grade add it to sum (i.e. sum += grade[i]). Then at the end just divide sum by 10.0 and that's your average.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My name is mr. X, and I want to thank you for adding me to your for loop:
    Code:
    for ( i<N; i=0 ;i++ ) //start i at zero, then test for < N
     {
        printf("Enter a grade: ");
        scanf("%f", &grade[x])
     }
    Really, I like things all X, and not i's!

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Adak View Post
    My name is mr. X, and I want to thank you for adding me to your for loop:
    Code:
    for ( i<N; i=0 ;i++ ) //start i at zero, then test for < N
     {
        printf("Enter a grade: ");
        scanf("%f", &grade[x])
     }
    Really, I like things all X, and not i's!
    X is indeed a wonderful loop counter Adak

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Thanks claudiu.

    I am getting an error that says syntax error before highscore. But I don't see any error...

    Code:
    #include <stdio.h>
    
    
    #define N 10
    int main(void)
    double grade[N], highscore, average_score, sum;
    int i, numraise, studentname;
    highscore=0.0;
    sum=0.0;
    average_score= sum/10;
    printf("Enter %d grades: ", N);
    for ( i<N; i=0 ;i++ )
     {
    scanf("%lf", &grade[i]);
    
    if (highscore < grade[i] highscore = grade[i])
    
     }
    for (0=numraise && numraise<=9, studentname=numraise+1, numraise++)
     {
    printf("Student %d's grade is %.1lf\n", studentname, grade[i])
     }
    printf("The highest score is %.1lf", highscore);
    printf("The average score is %.1lf", average_score);
    
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Code:
    if (highscore < grade[i] highscore = grade[i])
    I believe you want to try this instead:
    Code:
    if (highscore < grade[i])
       highscore = grade[i];

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    oh crap there's no { after int main

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Okay, getting better here but still a few things:

    1) Your for loops are messed up. In the first one you have the condition first the initialization second and the incrementation third.

    The correct order is :
    a) initialization
    b) condition
    c) increment

    as such:

    Code:
    for(i = 0;i < N;i++)
     /* do stuff*/
    2) Now you probably know why the second for loop is even more messed. Change it to look like the first.

    3) Don't compute average_score before you know what the sum of the grades actually is. You haven't even read them in yet. Move that instruction below the first for loop.

    4) Your error comes from the fact that the if clause closes its paranthesis after the assignment instead of before (i.e. if(highscore < grade[i]) highscore = grade[i]

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Just for a point of clarification on my part is

    if (highscore < grade[i]) highscore = grade[i]);

    the same as

    if (highscore < grade[i])
    highscore = grade[i]);


    ??

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not grade[X]?

    Oh clam it!

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    for (0=numraise && numraise<=9, studentname=numraise+1, numraise++)

    When comparing ints you need to use == not =
    You are assigning 0 to numraise here. Also, if you are comparing numraise to 0 this loop will only run once as after the first pass numraise is always going to be 1, not sure what you're looking to accomplish here

  15. #15
    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;
    highscore=0.0;
    sum=0.0;
    printf("Enter %d grades: ", N);
    for ( i=0 ; i<N ;i++ )
     {
    scanf("%lf", &grade[i]);
    
     if (highscore < grade[i])
     highscore = grade[i];
    
     }
    average_score= sum/10;
    
    for (numraise=0  ;numraise<=9, numraise++)
     {
     {
    studentname=numraise+1;
    printf("Student %d's grade is %.1lf\n", studentname, grade[i])
     }
    
    printf("The highest score is %.1lf", highscore);
    printf("The average score is %.1lf", average_score);
    
    return 0;
    }
    error before ) token.


    ALmost there I think?



    William I am trying to get it to print the student names and the grades together, like...


    student 1 <grade>

    student 2 <grade>

    ...
    student 10 <grade>

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