Computer Science Canada

Outputting average of all non-negative marks and invalid marks (IF STATEMENTS)

Author:  Loomis [ Wed Nov 04, 2009 7:16 pm ]
Post subject:  Outputting average of all non-negative marks and invalid marks (IF STATEMENTS)

What is it you are trying to achieve?
I am creating a program that continues to read marks from the user until a negative integer is inputted. For each non-negative mark entered, the program outputs the corresponding grade using a table. If a mark entered is greater than 100 the message "Invalid mark" is outputted. Also, the average of all the non-negative marks will be outputted.


What is the problem you are having?
I can't seem to output the average correctly because it gives this error " Division (or modulus) by zero in Real expression". Also, when I enter an invalid mark that is greater than 100, it still counts that.

Describe what you have tried to solve this problem
For the program not to count the invalid marks, I did this:
Turing:


if mark > 0 and mark < 100 then
    sum := sum + mark
    count := count + 1
end if



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
And this is the whole program:

Turing:


var mark, sum, count : int

sum := 0
count := 0

put "Mark" : 15, "Grade"
put "80-100" : 17, "A"
put "70-79" : 17, "B"
put "60-69" : 17, "C"
put "50-59" : 17, "D"
put "0-49" : 17, "E"
put ""

loop
    put "Please enter a mark: " ..
    get mark
    exit when mark < 0

    if mark >= 80 and mark < 100 then
        put "Your grade is A."
    elsif mark >= 70 and mark < 80 then
        put "Your grade is B."
    elsif mark >= 60 and mark < 70 then
        put "Your grade is C."
    elsif mark >= 50 and mark < 60 then
        put "Your grade is D."
    elsif mark >= 0 and mark < 50 then
        put "Your mark is F."
    else
        put "Invalid mark."
    end if
    put ""
end loop

if mark > 0 and mark < 100 then
    sum := sum + mark
    count := count + 1
end if

put ""
put "The average of all non-negative marks is ", sum/count : 0 : 1



Please specify what version of Turing you are using
Turing Version 4.1.1

Author:  Tony [ Wed Nov 04, 2009 7:33 pm ]
Post subject:  RE:Outputting average of all non-negative marks and invalid marks (IF STATEMENTS)

When you get to the last line and try to use "sum/count" the value of count is 0.

Hint: the value of "sum" is 0 as well.

Author:  saltpro15 [ Thu Nov 05, 2009 8:04 am ]
Post subject:  RE:Outputting average of all non-negative marks and invalid marks (IF STATEMENTS)

code:

if mark > 0 and mark < 100 then
    sum := sum + mark
    count := count + 1
end if


That needs to be inside your loop, otherwise you're always adding a 0 to sum Wink


: