C/Pseudo Code for Math Quiz Program
Expert: Zlatko - 11/15/2010
QuestionQUESTION: Hello, and thank you for taking time to review my question. I have been instructed to design a simple math quiz. The program has to display two random numbers (such as 247 + 129) that are to be added. The program needs to allow the entering of an answer. If the answer is correct, a "congratulations" message will be displayed. However, if incorrect, the program needs to display the correct answer. How would I give a detailed pseudo code of this?
ANSWER: Hello Bellissa
Pseudo code is the middle step between a problem description, like what you gave me, and real code, which can be compiled into a runnable program.
The idea in writing pseudo code is to provide enough detail so that each line of pseudo code can be translated into one or more lines of a programming language, but not so much detail that you lose the big picture of what you're trying to accomplish.
For example, consider a problem that requires you to open a file, and do something with the third element of each line. Pseudo code for that might be:
open file
for each line do
get third item
process the item
next line
Notice that there is no mention of how to get the third item. That is left to the coding phase.
Ok, back to your problem. The pseudo code might be something like this
loop forever
get first number
get second number
calculate result
print problem to screen
get user's answer
if user's answer is correct then congratulate user
if users answer is incorrect then print the correct answer
end loop
You see that pseudo code is just your instructions for solving the problem in a natural language like English. With some experience you will get a hunch about how detailed your pseudo code needs to be. The pseudo code above is detailed enough to easily be translated into code. Each line of pseudo code translates to between 1 and 3 lines of C code.
Your pseudo code can have more or less detail. There are no strict rules, but you would know bad pseudo code when you see it.
For example, here is bad pseudo code.
loop forever
do a quiz question
end loop
It's bad because there is really not enough detail to know what is happening.
On the other hand, a single line of pseudo code can take the place of dozens of lines of C code, as in this example.
get data into array
sort array
Each operation, getting the data, and sorting it, are quite involved, but also quite common and mechanical to do, and an experienced programmer could create code from it.
I hope that helps you out.
Best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: OK, I got it. That was very well explained. Now, how do I implement the functions into the psuedo code? Such as:
1 Module main ()
2 Declare string doAnother
3
4 Do
5 // Calculate and display sum.
6 Call showSum ()
7 End Module
Or, something like that? I'm now sure where to implement the functions (call, end module, display, declare, and etc.). Can you please help me on that part. I think I can take it from here if I can get help on the functions.
AnswerHello Bellissa
I am not quite sure what you are asking. I think you still want pseudo code, and not C code, and you want to break the program down into functions and describe the functions in pseudo code. Is that correct ?
The program is pretty short and does not really need functions, but if your instructor wants functions, then I suppose you could have a function for printing the problem to the screen, a function for getting the response, and a function for evaluating the response.
So something like this:
Function main
get first number
get second number
Call PrintProblem(firstNumber, secondNumber)
usersAnswer = Call GetAnswer
calculate realAnswer
Call EvaluateAnswer(usersAnswer, realAnswer)
end function
Function PrintProblem(firstNumber, secondNumber)
print firstNumber + secondNumber =
end function
Function GetAnswer
read integer answer from user
return user input
end function
Function EvaluateAnswer(userAnswer, realAnswer)
if userAnswer matches realAnswer then
print congratulations
else
print punishing message
print realAnser
endif
end function
Ok, that is pseudo code, with the problem broken down into small functions. It feels a little forced because the problem is so small to begin with, but I suspect your instructor wants you to get into the habbit of breaking a program down into small functions. It will be very useful when problems get bigger.
Remember there is no standard pseudo code language. You just make it up as you go along, so what I wrote may be quite different from examples your instructor has given you.
I hope that helps
Best regards
Zlatko