Question

In: Computer Science

Chapter 6 Debugging Exercises - Problem 1 The programmer intends for this pseudocode to display three...

Chapter 6 Debugging Exercises - 

Problem 1
The programmer intends for this pseudocode to display three random numbers in the range of 1 through 7. According to the way we've been generating random numbers in the book; however, there appears to be an error. Assume that the random() function is a built-in library function. Correct the pseudocode so that the program works as it should (This has 1 error and is easy to spot)
//This program displays 3 random numbers in the range from 1 to 7.
    Declare Integer count
    //Display 3 random numbers
    For count = 1 to 3
        Display random(7,1)
    End For

Problem 2
This problem should return the amount of a discount to a main() module when the calcDiscountPrice() function is called. (This has 1 error and is pretty easy to spot.)
//The calcDiscountPrice funciton accepts an item's price and the discount percentage as arguments. It uses those values to calculate and return the discounted price.
Function Real calcDiscountPrice(Real price, Real percentage)
Function Real calcDiscountPrice(Real price, Real percentage)
    //Declarations
    Declare Real discount
    Declare Real discountPrice

    //Calculations
    discount = price * percentage
    discountPrice = price - discount
    
    //Return the discount amount
    Return discount

Problem 3
This problem should calculate and display 10% of any number given. Correct the pseudocode (a bit harder than the last 2 problems)
Module main()
    //Declarations
    Declare Real value, result
    
    //Get a value from the user
    Display "enter a value"
    Input value
    
    //Set 10% of the value
    result = tenPercent(value)
    
    //Display value
    Display "10 percent of ", value, " is ", result
End Module

//The tenPercent() function calculates and returns 10% of any number passed into it
Function Real tenPercent(Real num)
    //Declarations
    Declare Integer myResult
    
    //Calculations
    myResult = num * .10
    
    //Return the result
    Return myResult
    
End Function
    

Solutions

Expert Solution

CODE :

Modified code reason has been commented and kept in bold.

Problem - 1:

Module main() // Good way to start PseudoCode

Declare Integer count

    //Display 3 random numbers

    For count = 1 to 3

        Display Integer random(1,7) // Type has to be declared

    End For

End Module

___________________________________________________________________________________________

Problem - 2:

// Main Module

Module main()

        discount = Function Real calcDiscountPrice(Real price, Real percentage)  

// returned value has to be captured.

End Module

Function Real calcDiscountPrice(Real price, Real percentage)

    //Declarations

    Declare Real discount

    Declare Real discountPrice

    //Calculations

    discount = price * percentage

    discountPrice = price - discount

    

    //Return the discount amount

    Return discount

___________________________________________________________________________________________

Problem - 3:

Module main()

    //Declarations

    Declare Real value, result

    //Get a value from the user

    Display "enter a value"

    Input value

    //Set 10% of the value

    result = tenPercent(value)

    //Display value

    Display "10 percent of ", value, " is ", result

End Module

//The tenPercent() function calculates and returns 10% of any number passed into it

Function Real tenPercent(Real num)

    //Declarations

    Declare Real myResult // Type has to be Real

    //Calculations

    myResult = num * .10

// Return the result

    Return myResult

    

End Function










Related Solutions

The following problem should be done in pseudocode. Problem 1 The programmer intends for this pseudocode...
The following problem should be done in pseudocode. Problem 1 The programmer intends for this pseudocode to display three random numbers in the range of 1 through 7. According to the way we've been generating random numbers in the book; however, there appears to be an error. Assume that the random() function is a built-in library function. Correct the pseudocode so that the program works as it should (This has 1 error and is easy to spot) //This program displays...
Read and understand the pseudocode of problem R2.19 in java from the end of chapter 2...
Read and understand the pseudocode of problem R2.19 in java from the end of chapter 2 in your textbook. In a Java class named WeekDays, write a java program that reads an input int between (0-6), and displays the corresponding day for that given input as below: Enter a number (0-6): 2 ** 2 is Tue ** !!! NO if-else conditions
Chapter 6 Problem A consumer finds only three products, X, Y, and Z, are for sale....
Chapter 6 Problem A consumer finds only three products, X, Y, and Z, are for sale. The amount of utility which their consumption will yield is shown in the table below. Assume that the prices of X, Y, and Z are $10, $2, and $8, respectively, and that the consumer has an income of $74 to spend. Product X (Price $10) Product Y (Price $2) Product Z (Price $8) Quantity Utility Marginal Utility per $ Quantity Utility Marginal Utility per...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Rework problem 35 from the Chapter 2 review exercises in your text, involving auditioning for a...
Rework problem 35 from the Chapter 2 review exercises in your text, involving auditioning for a play. For this problem, assume 9 males audition, one of them being Wayne, 6 females audition, one of them being Lorraine, and 5 children audition. The casting director has 4 male roles available, 2 female roles available, and 1 child role available. (1) How many different ways can these roles be filled from these auditioners? (2) How many different ways can these roles be...
Rework problem 35 from the Chapter 2 review exercises in your text, involving auditioning for a...
Rework problem 35 from the Chapter 2 review exercises in your text, involving auditioning for a play. For this problem, assume 11 males audition, one of them being Seamus, 4 females audition, one of them being Nisha, and 5 children audition. The casting director has 4 male roles available, 2 female roles available, and 2 child roles available. (1) How many different ways can these roles be filled from these auditioners? (2) How many different ways can these roles be...
Rework problem 35 from the Chapter 2 review exercises in your text, involving auditioning for a...
Rework problem 35 from the Chapter 2 review exercises in your text, involving auditioning for a play. For this problem, assume 13 males audition, one of them being Karthikey, 7 females audition, one of them being Tiffany, and 5 children audition. The casting director has 4 male roles available, 2 female roles available, and 1 child role available. (1) How many different ways can these roles be filled from these auditioners? (2) How many different ways can these roles be...
1) In this problem, you may use the fact (which we will prove in Chapter 6)...
1) In this problem, you may use the fact (which we will prove in Chapter 6) that an integer n is not divisible by 3 if and only if there exists an integer k such that n = 3k + 1 or n = 3k + 2. (a) Prove that for all integers n, if 3 | n2, then 3 | n. 2) Let a and b be positive integers. Prove that if a | b and b | a,...
Chapter Three Exercises Exercise 3.5 At a used dealership, let X be an independent variable representing...
Chapter Three Exercises Exercise 3.5 At a used dealership, let X be an independent variable representing the age in years of a motorcycle and Y be the dependent variable representing the selling price of used motorcycle. The data is now given to you. X = {5, 10, 12, 14, 15} Y = {500, 400, 300, 200, 100} A.) Construct a 95% confidence interval for B1. B.) Do the data provide sufficient evidence to indicate that X contributes information to the...
Chapter Three Exercises Exercise 3.9 At a used dealership, let X be an independent variable representing...
Chapter Three Exercises Exercise 3.9 At a used dealership, let X be an independent variable representing the age in years of a motorcycle and Y be the dependent variable representing the selling price of used motorcycle. The data is now given to you. X = {5, 10, 12, 14, 15} Y = {500, 400, 300, 200, 100} A.) Predict the selling price of a used motorcycle when the age is 11 years. B.) Find a 95% confidence interval for E(y)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT