Week 3 Lab 1 + answer

8
LAB 1 WEEK 3 Objective To solve a given problem by applying the following steps: 1. Specify the problem requirements. 2. Analyze the problem. 3. Design the algorithm to solve the problem. Deliverable items For each of the following activities, you are required to: 1. Determine input, output and process. Provide your assumptions, if any, 2. develop a complete algorithm, a. pseudo-code, OR b. flowchart, and 3. Desk checking with some sample data. Activity #1 Obtain three numbers from the keyboard, compute the average of the numbers and display the result. Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. Obtain a series of positive numbers from the keyboard, and determine and display the sum of the numbers. Assume that the user types the sentinel value -1 to indicate “end of data entry.” Activity #2 You were asked by your project leader to write a simple program that obtains the lengths of the sides of a rectangle. The program calculates the area and perimeter then prints lengths, the area and the perimeter. Prepare a pseudo-code and a flowchart for the program. Activity #3 Calculate Carpeting Area Write pseudocode for calcCarpeting, based on the following structure chart.

description

Matlab

Transcript of Week 3 Lab 1 + answer

LAB 1 WEEK 3

LAB 1 WEEK 3Objective

To solve a given problem by applying the following steps:

1. Specify the problem requirements.

2. Analyze the problem.

3. Design the algorithm to solve the problem.

Deliverable items

For each of the following activities, you are required to:

1. Determine input, output and process. Provide your assumptions, if any,

2. develop a complete algorithm,

a. pseudo-code, OR

b. flowchart, and

3. Desk checking with some sample data.

Activity #1

Obtain three numbers from the keyboard, compute the average of the numbers and display the result.

Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers.

Obtain a series of positive numbers from the keyboard, and determine and display the sum of the numbers. Assume that the user types the sentinel value -1 to indicate end of data entry.Activity #2You were asked by your project leader to write a simple program that obtains the lengths of the sides of a rectangle. The program calculates the area and perimeter then prints lengths, the area and the perimeter. Prepare a pseudo-code and a flowchart for the program.

Activity #3Calculate Carpeting Area

Write pseudocode for calcCarpeting, based on the following structure chart.

LAB 1 WEEK 3 [SOLUTION]Activity #1

Obtain three numbers from the keyboard, compute the average of the numbers and display the result.i. Start

ii. Declare variables

iii. Get input from user number 1, number 2, and number 3

iv. Calculate average of number 1, number 2, and number 3

v. Display average

vi. End

Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers.i. Start

ii. Declare variables

iii. Get input from user number 1 and number 2

iv. Compare number 1 and number 2

v. Display the smaller number

vi. End

Obtain a series of positive numbers from the keyboard, and determine and display the sum of the numbers. Assume that the user types the sentinel value -1 to indicate end of data entry.i. Start

ii. Declare variables

iii. Initialize total value = 0

iv. Get a number from user

v. If number is not equal to -1, go to step 6, otherwise go to step 7

vi. Calculate sum

vii. Display sum of numbers

viii. End

Activity #2

You were asked by your project leader to write a simple program that obtains the lengths of the sides of a rectangle. The program calculates the area and perimeter then prints lengths, the area and the perimeter. Prepare a pseudo-code and a flowchart for the program.

1. Start

2. Declare variables

3. Get input length and width from user

4. Calculate area and perimeter

5. Display area and perimeter

6. End

Activity #3Calculate Living AreaWrite a pseudo-code for calcCarpeting, based on the following structure chart.

Algorithm calcCarpetingStart

1. Prompt user for famRoom width

2. Read familyRoom width

3. Prompt user for famRoom length

4. Read famRoom length

5. famRoom area = famRoom width * famRoom length

6. Prompt user for dineLive width

7. Read dineLive width

8. Prompt user for dineLive length

9. Read dineLive length

10. dineLive area = dineLive width * dineLive length

11. Living areas = famRoom area + dineLive area

12 Prompt user for bedRoom width

13. Read bedRoom width

14. Prompt user for bedRoom length

15. Read bedRoom length

16. BedRoom area = bedRoom width * bedRoom length

17. Carpeting area = BedRoom area + Living areasEnd Algorithm calcCarpeting

Start

Number 1,

Number 2,

Number 3

Average = (Number 1 + Number 2 + Number 3) / 3

Display Average

End

Start

End

Number 1,

Number 2

Display Number 1 is smaller than Number 2

Number 1 < Number 2

Display Number 1 is smaller than Number 2

5