Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Stupid question: Counters and Accumulators
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Angel




PostPosted: Mon Jan 05, 2004 10:52 pm   Post subject: Stupid question: Counters and Accumulators

I have to make up a code using counters and accumulators for a multiple choice quiz. The idea is to add a point for each correct answer. There are 10 questions and each of the users answers has been declared as an array. I haven't declared the actual answers or anything (I'm not sure if I'm supposed to). Here's an example of the code:

code:
var questions1 : array 1 .. 10 of int
        for m : 1 .. 10
        end for
        put "Question 1:"
        put ""
        put "Which of the following actors/actresses is completely blind in their left eye "
        put "after undergoing an operation to correct a squint? (Please enter the letter of "
        put "your selection)"
        put ""
        put "1) Bruce Willis"
        put "2) Christina Ricci"
        put "3) Demi Moore"
        put "4) Jason Biggs"
        put "5) Sean Connery"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (1)
        if questions1 (1) > 5 or questions1 (1) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop % Will rerun the program when the user does not choose one of the choices
                get questions1 (1)
                exit when questions1 (1) = 1 or questions1 (1) = 2 or questions1 (1) = 3 or questions1 (1) = 4 or questions1 (1) = 5
                put "Please select another choice:  " ..
            end loop
        end if


Say the answer for this question is 3 and I want to give 1 point to the person for getting the correct answer (if they get it). And I want to do that with the rest of my questions and in the end, display their total out of 10. I was told that I should use counters and accumulators, so can someone please give me the code? It will be greatly appreciated! Thanks guys. Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Jan 05, 2004 10:55 pm   Post subject: (No subject)

it's something like
code:

for question:1..10
put text(question)
get answer
if correctanswer(question) = answer then
%user's correct
% points+=1
else
%user's incorrect
end if
end for

put points


you could load questions/answers from a datafile or something
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Angel




PostPosted: Mon Jan 05, 2004 11:04 pm   Post subject: (No subject)

I'm not sure how to incorporate that into my program, so can I paste the program to you and you can make the code specifically for that program?

code:
var questions1 : array 1 .. 10 of int
        for m : 1 .. 10
        end for
        put "Question 1:"
        put ""
        put "Which of the following actors/actresses is completely blind in their left eye "
        put "after undergoing an operation to correct a squint? (Please enter the letter of "
        put "your selection)"
        put ""
        put "1) Bruce Willis"
        put "2) Christina Ricci"
        put "3) Demi Moore"
        put "4) Jason Biggs"
        put "5) Sean Connery"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (1)
        if questions1 (1) > 5 or questions1 (1) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop % Will rerun the program when the user does not choose one of the choices
                get questions1 (1)
                exit when questions1 (1) = 1 or questions1 (1) = 2 or questions1 (1) = 3 or questions1 (1) = 4 or questions1 (1) = 5
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 2:"
        put ""
        put "Who was the famous director of films such as E.T., Indiana Jones and Jurassic "
        put "Park?"
        put ""
        put "1) Peter Jackson"
        put "2) Michael Jackson"
        put "3) Steven Spielberg"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (2)
        if questions1 (2) > 3 or questions1 (1) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (2)
                exit when questions1 (2) = 1 or questions1 (2) = 2 or questions1 (2) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 3:"
        put ""
        put "The richest actress today, who has starred in movies such as Runaway Bride, "
        put "Stepmom and Mona Lisa Smile is:"
        put ""
        put "1) Julia Styles"
        put "2) Julia Roberts"
        put "3) Demi Moore"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (3)
        if questions1 (3) > 3 or questions1 (3) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (3)
                exit when questions1 (3) = 1 or questions1 (2) = 2 or questions1 (2) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 4:"
        put ""
        put "Which famous body builder who became a Hollywood sensation, starred in the "
        put "Terminator Trilogy?"
        put ""
        put "1) Arnold Schwartzenegger"
        put "2) The Rock"
        put "3) McBain"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (4)
        if questions1 (4) > 3 or questions1 (4) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (4)
                exit when questions1 (3) = 1 or questions1 (3) = 2 or questions1 (3) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 5:"
        put ""
        put "Who was the actress who played Lara Croft in the movies, Tomb Raider I and II?"
        put ""
        put "1) Jennifer Lopez"
        put "2) Lara Croft"
        put "3) Angelina Jolie"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (5)
        if questions1 (5) > 3 or questions1 (5) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (5)
                exit when questions1 (5) = 1 or questions1 (5) = 2 or questions1 (5) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 6:"
        put ""
        put "Which actor/actress from the Lord of the Rings Trilogy stepped on a piece of "
        put "glass and was taken to the hospital while shooting a scene?"
        put ""
        put "1) Liv Tyler"
        put "2) Elija Wood"
        put "3) Sir Ian McKellen"
        put "4) Sean Austin"
        put "5) Miranda Otto"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (6)
        if questions1 (6) > 5 or questions1 (6) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (6)
                exit when questions1 (6) = 1 or questions1 (6) = 2 or questions1 (6) = 3 or questions1 (6) = 4 or questions1 (6) = 5
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 7:"
        put ""
        put "Which actress started her movie career by starring in the 1984 film, "
        put "Firestarter?"
        put ""
        put "1) Jennifer Aniston"
        put "2) Drew Barrymore"
        put "3) Kirsten Dunst"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (7)
        if questions1 (7) > 3 or questions1 (7) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (7)
                exit when questions1 (7) = 1 or questions1 (7) = 2 or questions1 (7) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 8:"
        put ""
        put "Which actress was denied 2 roles opposite Leonardo DiCaprio as Juliet in Romeo "
        put "and Juliet and Rose in Titanic?"
        put ""
        put "1) Courtney Cox"
        put "2) Christina Ricci"
        put "3) Uma Thurman"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (8)
        if questions1 (8) > 3 or questions1 (8) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (8)
                exit when questions1 (8) = 1 or questions1 (8) = 2 or questions1 (8) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 9:"
        put ""
        put "Which of these trios starred in the 2001 movie, Charlie's Angels?"
        put ""
        put "1) Lucy Lui, Drew Barrymore, Carmen Electra"
        put "2) Cameron Diaz, Lucy Lawless, Drew Barrymore"
        put "3) Drew Barrymore, Lucy Lui, Cameron Diaz"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (9)
        if questions1 (9) > 3 or questions1 (9) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (9)
                exit when questions1 (9) = 1 or questions1 (9) = 2 or questions1 (9) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        put skip
        put "Question 10:"
        put ""
        put "Mila Kunis, the star of the hit TV show, That 70's Show and the star of "
        put "American Psycho 2 was born in:"
        put ""
        put "1) Portugal"
        put "2) Ukraine"
        put "3) Austria"
        put ""
        put "Please enter the number of your selection:  " ..
        get questions1 (10)
        if questions1 (10) > 3 or questions1 (10) = 0 then
            put "This is not a choice."
            put "Please select another choice:  " ..
            loop
                get questions1 (10)
                exit when questions1 (10) = 1 or questions1 (10) = 2 or questions1 (10) = 3
                put "Please select another choice:  " ..
            end loop
        end if
        cls


The array questions1 should technically be answers (the users answer that is). I'm not sure how to declare the actual answers, so can you help me with that too? You can just make them up.
Thuged_Out_G




PostPosted: Tue Jan 06, 2004 4:31 pm   Post subject: (No subject)

im not sure what your saying but here is what i get

code:

put "questions"
put "1) some answer"
put "2) some answer"
put "3)the correct answer"
get answer

if answer=3 then
put "Correct"
point +=1
else
put "Wrong answer"
end if


just do something like that for each question
roer




PostPosted: Tue Jan 06, 2004 5:58 pm   Post subject: (No subject)

The easiest thing to do is too declare two variables, right and wrong. Or just right if you like.

code:
if answer = "D" or answer = "d" then
right := right + 1
else
wrong := wrong + 1
end if


Simple way to do it...or you can do it like tony and thugedoutG suggested
shorthair




PostPosted: Tue Jan 06, 2004 6:05 pm   Post subject: (No subject)

What they are trying to tell you is to use count
code:

var ans,count : int
count:= 0
loop
locate(10,20)
put"1+1 ="..
get ans
cls
if ans = 2 then
put" Correct "..
count := count + 1
put "you have :" ,count," point(s)"
else
put " Incorrect"
end if
end loop

that is the easiest aspect of count and that is exactly what you need.
in your code do an if statment after you get the answer
if ans1 = 1 then
count := count + 1
else
end if
Angel




PostPosted: Tue Jan 06, 2004 7:21 pm   Post subject: (No subject)

I'm not quite sure that I understand. You said:

code:
if ans1 = 1 then... etc.


The thing is, the correct answer may not always be 1. Oh wait hold on I think I'm getting it, so after I get an aswer I should put that code upthere and the number of the correct answer?
Thuged_Out_G




PostPosted: Tue Jan 06, 2004 8:25 pm   Post subject: (No subject)

yuo will need an if statement for each question...it doesnt matter what the answer is

if the answer is 1:
if answer=1 then
count +=1
else
put "Wrong answer"
end if

if the answwer is 5:
if answer=5 then
count +=1
end if
Sponsor
Sponsor
Sponsor
sponsor
Angel




PostPosted: Tue Jan 06, 2004 11:56 pm   Post subject: (No subject)

You guys are geniuses! Honestly, without you people I wouldn't have been able to do this ISP! Thanks, you've really helped! Very Happy

Just one more thing: I wanna make a table that says the number of the question, then the user's answer, then the correct answer, the number of points they received for each answer and then the total also converted to %? Anyone have any ideas? Thinking
Tony




PostPosted: Wed Jan 07, 2004 8:59 am   Post subject: (No subject)

using string spacing. Such as
code:

put question1:20, answer1:5, useranswer1:5, score:5


the :number will make the string atleast that long (it will not shorten it if the string is longer). Very useful for text alignment and making text tables.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: