\"Create the logic for a program that continuously prompts a user for a numeric
ID: 3583815 • Letter: #
Question
"Create the logic for a program that continuously prompts a user for a numeric value until the user enters 0. The application passes the value in turn to a method that squares the number and to a method that cubes the number. The program displays the results before prompting the user again. Create the two methods that respectively square and cube a number that is passed to them, returning the calculated value."
Your are building the logic for a special purpose calculator that only calculates two things: the square and the cube of a number entered by the user. The squaring and cubing of the number are each done in separate modules that are called by the main logic. The main logic asks for the number from the user, calls the square function and the cube function, and displays the results. A loop structure is used in the main logic that keeps repeating the process until the user enters a 0 to indicate they are done.
Note that Farrell uses the term method for a module that performs a sub-process or subtask. The termfunction is more generally used, unless the module is associated with a specific software object, in which case method is the appropriate term.
Only use Pseudocode.
Explanation / Answer
Here is the Pseudo code for you:
Method Square(x):
return x * x; //Squares the value passed as input.
Method Cube(x):
return x * x * x; //Cubes the value passed as input.
Method Main():
while(true): //Repeats infinitely.
Prompt for x.
Read x.
if x == 0:
return; //Stop executing.
y = Square(x) //Calls the method Square(x).
Print y. //Prints the square of a number x.
y = Cube(x) //Calls the method Cube(x).
Print y.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.