| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Exercise 11 Q2b Solution

Page history last edited by Steve Sweeney 15 years, 2 months ago

/* Write a program that asks the user the same single,

 simple, random math problem until they get it right

 (a) allow the user to specify the number of problems */

 

var firstDigit, secondDigit : int

var answer, correctAnswer : int

var numProblems : int

var continue : string

numProblems := 0    % user has answered no problems

continue := "yes"   % assume the user wants to continue

 

loop

    randint (firstDigit, 1, 9)

    randint (secondDigit, 1, 9)

    correctAnswer := firstDigit + secondDigit

    answer := -99 %initialize to a value that cannot be right

 

    loop

        put "What is ", firstDigit, " + ", secondDigit, " = " ..

        get answer

 

        if (answer = correctAnswer) then

            put "That is correct"

        else

            put "Sorry, try again..."

        end if

 

        exit when (answer = correctAnswer)

    end loop

 

    numProblems := numProblems + 1

   

    put "Do you wish to continue? " ..

    get continue

 

    exit when (continue = "no") or (continue = "No") or (continue = "NO")

end loop

 

put "You answered ", numProblems, " problems"

Comments (0)

You don't have permission to comment on this page.