Thread: Loop Function

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    39

    Loop Function

    Quote Originally Posted by blank
    Write a program to compute the grade for every student of a class.
    Use exactly the same weights as in assignment #2 to determine the grade of each student. Then report the grade received by the student.
    In addition, your program needs to output a summary report showing the total number of students, and the number of students receiving each letter grade.

    To indicate that there is no more student, use a negative # as the first assignment score.
    In other words, if assign. #1 score for the next student is negative; your program should terminate without the need to read in the rest of the scores of the student.

    To allow mistakes in input, if any score (other than assign. #1) is out of the range of 0 – 100, do not count that student, and simply output an error statement, and move on to the next student.
    In other words, ignore the rest of the scores for that student.
    So basically as an assignment I must write a program that calculates the scores for students of a class and output a letter grade / etc, the rubric is attached. I am not asking for anyone to do the assignment, I have a program written to calculate the needed values for ONE student, but am wondering how to calculate it for multiple students while working with the rubric and through use of loop functions. I have attached the .cpp of the original program I wrote to calculate the average of one student.

    The only real issue I'm having is taking the program I wrote for one student and making it follow the rubric, teacher speaks very little English and I'm having trouble self teaching myself.

    Thanks greatly for any help you can provide to this subject.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you to assume there are possibly infinite students? Or is there a maximum class size? If the latter, use an array of whatever you used before for your single student. If the former, looks like a good time to learn about linked lists.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    It's supposedly for infinite students I suppose. The real problem I'm having is the code itself, like. How do I take the program for one student and make it applicable to all students. The notes mention like a process similar to this:

    i = 1; // loop initialization
    while (i <= n) // loop test
    {
    output("current i = ",i);
    processing;
    i = i+1; // loop update
    output("next i =",i);
    system("pause")
    }
    statementA;

    and an example for students.

    printf("please input the number of scores,n:");
    scanf("%d", &n);
    sum = 0;
    i = 1;
    while (i <= n)
    {
    scanf("%d", &score);
    sum = sum + score; i = i + 1;
    }
    printf("The total score=%f\n",sum);
    avg = sum /n; printf("The average score=%f\n",avg);


    the i represents the # of students, and which student is currently being worked on, n is the number of students. and sum represents the running total score. I'm just confused how to implement all of this into code!! It also mentions the use of a sentinel to keep the loop controlled, not sure how to do that either >:/.

    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    do
        ask if they want to enter a student's grade, -1 to exit or whatever
        if not -1
            for each grade they want to enter
                read this grade
                sum grades or whatever you're doing
            calculate final grade
            note it some place (that they got an A or a C or what not)
            increment your total student count
    while not -1 or whatever number you chose
    spit out your summary
    Pretty much like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    I have to make use of a "sentinel" to terminate the loop. He describes this as

    "To indicate that there is no more student, use a negative # as the first assignment score.
    In other words, if assign. #1 score for the next student is negative; your program should terminate without the need to read in the rest of the scores of the student."

    So does this mean just a simple statement like.

    if (assignment1 < 0);
    {
    return 0;
    }

    something like that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if your variable is called grade, maybe you should declare that instead of five separate variables that you never use.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    yeah thats what I meant

    char Grade

    but when

    Grade = `A';

    I get an error

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    I have another issue. These are the instructions

    To allow mistakes in input, if any score (other than assign. #1) is out of the range of 0 – 100, do not count that student, and simply output an error statement, and move on to the next student.
    In other words, ignore the rest of the scores for that student.
    '
    This is my code so far


    But when I compile I get this error

    parse error before `else'
    Last edited by evangela; 10-06-2009 at 08:18 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by evangela View Post
    I have another issue. These are the instructions

    To allow mistakes in input, if any score (other than assign. #1) is out of the range of 0 – 100, do not count that student, and simply output an error statement, and move on to the next student.
    In other words, ignore the rest of the scores for that student.
    '
    This is my code so far



    But when I compile I get this error

    parse error before `else'
    Well yes. An else must go with an if, not with a scanf.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by evangela View Post
    yeah thats what I meant

    char Grade

    but when

    Grade = `A';

    I get an error
    If that's how you declared it, then you don't get an error. If you get an error, then that's not what you did.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    Ok, the assignment asks that if the first assignment grade is negative, it ends the loop or shuts down the program, which I think that part of my code is right. But it also says, if any other grade is less than 0 or more than 100 it skips that student and moves onto the next, but is there an easy way to do that in one code line? Since I have 4 scan Fs for assignments, and 2 scan Fs for tests and another for the final, is there someway I can have it see if the grade input is higher than 100 or less than 0 in one code line instead of having an if statement for every scanf?

    Thanks

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if grade is out of range
        ...don't actually do anything with this grade, and put an error message...
    else
        ...store this grade...
    I'm not sure where you're hung up. I don't think you'd want to combine this with the -1 check, because it says to output an error message if it's out of range. You wouldn't put an error message if you were intentionally entering -1 to stop adding students.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    Is it possible to do something like this?

    else if(ass2, ass3, ass4 < 0 || ass2, ass3, ass4 > 100)
    so that you don't have to have an else if statement for every single variable?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can chain them together, but you need to do it differently. What you really should be doing is:
    Code:
    int newgrade;
    ...
    prompt for new grade
    if( newgrade == -1 )
        ...bail out...
    else
    if( newgrade is out of range )
        ...error message...
    else
        grade[ x ] = newgrade;
    Something like that. Have you learned about arrays yet? Seems like you should have, since you had an array of 50 earlier. For repeat variables "ass2" "ass3" "ass4", you might consider an array. Anyway...
    Code:
    if( (ass2 < 0 || ass2 > 100) || (ass3 ... ) || (ass4 ... ) )
    Don't actually put "..." in there. I just didn't feel like typing it over and over.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    Thanks a lot! Do I have to put this array if statement before the scanf or after them?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function (modules) and loop help.
    By GCNDoug in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 12:43 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM