C/Pseudo Code for Input Validation Program
Expert: Zlatko - 11/23/2010
QuestionQUESTION: Hi Zlatko, thanks for taking the time to review my question. I do greatly appreciate it in advance.
I have been instructed to pseudo code a program for a theater seating revenue. The program should ask for the number of tickets sold in each section and display (print) the income amount generated from ticket sales. Lastly, the program should validate the numbers entered in each section.
Here's the data:
Section A (300 seats w/ cost of $20 per seat)
Section B (500 seats w/ cost of $15 per seat)
Section C (200 seats w/ cost of $10 per seat)
How should I pseudo code this?
ANSWER: Hello Kanji
Pseudo code is really just instructions for solving a problem. The instructions are written in a natural language, like English, and specify in some detail how to solve a problem. The instructions don't have to be as detailed as real code. Each pseudo code line is usually translated into many lines of a programming language. Knowing how much detail to put into pseudo code is a matter of practice. The objective is to specify what has to be done, in enough detail so that a programmer can translate the pseudo code into a programming language, but not put in so much detail that you get bogged down in syntax and implementation details.
There is no standard pseudo code language, so everyone will do if differently.
For you assignment one approach is to do it like this:
Get count of seats sold in section A
Check that count is valid (between 0 and 300)
Calculate revenue from section A (count * $20)
Print revenue from section A
Add revenue to total revenue
Then you can repeat the above lines for sections B and C and finally have a pseudocode statement to print the total revenue.
You might notice that the pseudocode for all three sections really follow the same steps, but just the data is different. By data, I mean things like the section name, the seat price, and the number of seats available in a section. You might want to create a function that takes the different data for a section and returns the revenue. For example.
Function RevenueForSection (
input sectionName,
input sectionPrice,
input seatsAvailable)
This part is for you, Kanji, to fill in
End Function
Then in your main function, you would have code like this
Function Main
totalRevenue = totalRevenue + RevenueForSection(A, $20, 300)
End Function
Here I have made up my own pseudocode language, and I've left some work for you to do.
Which approach you take, with or without functions is up to you. The point is to make it clear to a human reader how you intend to solve the problem.
I hope that gets you started.
Best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: Ok, thank you SO much. That definitely helped me get started. I now just need the last bit of help. Here is what I have ending my pseudo code for the program:
//this module calculates totalSales
//totalSales can be defined as a value parameter due to
//totalSales being unchanged in the module.
Module calcTotalSales(Real Ref sectionSales, Real totalSales)
totalSales = totalSales + sectionSales
End Module
//this module prints the total sales
Module totalSales
Print “The generated income from tickets sold is:”
End Module
Now, here is what I have beginning the pseudo code for the program:
Module main( )
//Declare local variables
Declare Real totalSales
My question is: Do I also need to declare Real Ref sectionSales in my Module main? My guess is no because the overall purpose of the program is to generate total sales. What do you suggest?
AnswerHello Kanji
The style of your pseudo code is closer to that of a programming language than it is to a natural language if your pseudo code goes as far a declaring variables. If that is the type of pseudo code your instructor wants, then I'd suggest that you do need to declare your sectionSales variable. Your pseudo code would first calculate sectionSales, and then pass that to the calcTotalSales module. That's just my opinion.
I disagree with your comments about the calcTotalSales module. I would say that sectionSales can be defined as a value parameter because the module does not change it. The module does change totalSales, so totalSales should be the one declared as a reference (Ref) parameter.
I hope that helps you out.
Best regards
Zlatko