student lab 1: input, processing, and...

24
Starting Out with Programming Logic and Design 1 Lab 6: Repetition Structures This lab accompanies Chapter 5 of Starting Out with Programming Logic & Design. Name: Devin Hill and Michael Schultz Lab 6.1 – For Loop and Pseudocode This lab requires you to implement a count-controlled loop using a for statement. Step 1: Examine the following code. Constant Integer MAX_HOURS = 24 Declare Integer hours For hours = 1 to MAX_HOURS Display “The hour is “, hours End For Step 2: Explain what you think will be displayed to the screen in Step 1. (Reference: For loop, page 186): The hour is 1 The hour is 2 The hour is 3 So on… Step 3: Write a for loop that will print 60 minutes to the screen. Complete the missing lines of code. Constant Integer MAX_MINUTES = 60 Declare Integer minutes For minutes = 1 to MAX_MINUTES Display “The minute is”, minute End For Step 4: Write a for loop that will print 60 seconds to the screen. Complete the missing lines of code.

Upload: dangdang

Post on 01-May-2018

757 views

Category:

Documents


31 download

TRANSCRIPT

Page 1: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 1

Lab 6: Repetition StructuresThis lab accompanies Chapter 5 of Starting Out with Programming Logic & Design.

Name: Devin Hill and Michael Schultz

Lab 6.1 – For Loop and Pseudocode

This lab requires you to implement a count-controlled loop using a for statement.

Step 1: Examine the following code.

Constant Integer MAX_HOURS = 24Declare Integer hours

For hours = 1 to MAX_HOURSDisplay “The hour is “, hours

End For

Step 2: Explain what you think will be displayed to the screen in Step 1. (Reference: For loop, page 186):

The hour is 1The hour is 2The hour is 3So on…

Step 3: Write a for loop that will print 60 minutes to the screen. Complete the missing lines of code.

Constant Integer MAX_MINUTES = 60Declare Integer minutes

For minutes = 1 to MAX_MINUTESDisplay “The minute is”, minute

End For

Step 4: Write a for loop that will print 60 seconds to the screen. Complete the missing lines of code.

Constant Integer MAX_SECONDS = 60Declare Integer seconds

For seconds = 1 to secondsDisplay “The second is”, minute

End For

Step 5: For loops can also be used to increment by more than one. Examine the following code.

Page 2: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 2

Constant Integer MAX_VALUE = 10Declare Integer counter

For counter = 0 to MAX_VALUE Step 2Display “The number is “, counter

End For

Step 6: Explain what you think will be displayed to the screen in Step 5. (Reference: Incrementing by Values Other than 1, page 190):

The counter will print by two such asThe number is 0The number is 2The number is 4The number is 6The number is 8The number is 10

Step 7: Write a for loop that will display the numbers starting at 20, then 40, then 60, and continuing the sequence all the way to 200.

Constant Integer MAX_VALUE = 200Declare Integer counter

For counter = 20 to MAX_VALUE Step 20Display “The number is “, counter

End For

Step 8: For loops can also be used when the user controls the number of iterations. Examine the following code:

Declare Integer numStudents Declare Integer counter

Display “Enter the number of students in class”Input numStudents

For counter = 1 to numStudentsDisplay “Student #”, counter

End For

Step 9: Explain what you think will be displayed to the screen in Step 8. (Reference: Letting the User Control the Number of Iterations, page 194):The user will be prompted to enter the number of students in the class and then the loop will print out the following:Student #1Student #2Student #3And so on…

Page 3: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 3

Step 10: For loops are also commonly used to calculate a running total. Examine the following code.

Declare Integer counterDeclare Integer total = 0Declare Integer number

For counter = 1 to 5Display “Enter a number: “Input numberSet total = total + number

End For

Display “The total is: “, total

Step 11: Explain what you think will be displayed to the screen in Step 10. (Reference: Calculating a Running Total, page 201):The user will be prompted to enter five numbers and then the total of the numbers will be displayed to the screen.

Step 12: Write the missing lines for a program that will allow the user to enter how many ages they want to enter and then find the average.

Declare Integer counterDeclare Integer totalAge = 0Declare Real averageAge = 0Declare Integer ageDeclare Integer number

Display “How many ages do you want to enter: “Input number

For counter = 1 to numberDisplay “Enter age: “Input ageSet totalAge = totalAge + age

End For

averageAge = totalAge / number

Display “The average age is “, averageAge

Page 4: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 4

Lab 6.2 –For Loop and Flowcharts

This lab requires you to convert various pseudocode steps in Lab 6.1 to a flowchart. Use an application such as Raptor or Visio.

The Seconds CounterStep 1: Start Raptor and save your document as Lab 6-2Seconds. The .rap file extension will be added automatically.

Step 2: The first loop to code is the pseudocode from Step 4, Lab 6.1. This loop will print 60 seconds to the screen. The complete pseudocode is below:

Constant Integer MAX_SECONDS = 60Declare Integer seconds

For seconds = 1 to 60Display “The second is “, seconds

End For

Step 3: Click the Loop symbol and add it between the Start and the End symbol. Above the Loop symbol, add two assignment statements. Set a variable named seconds to 1 and a variable named MAX_SECONDS to 60.

Step 4: Double click the Diamond symbol and add the condition that will execute the loop through 60 iterations.

Step 5: Add an output statement if the loop is NO. This statement will display the seconds variable to the screen.

Step 6: Add an assignment statement next that will increment the seconds variable by 1.

Step 7: Execute your flowchart to see if your output matches the following. If not, repeat the steps to identify the error and execute again.

The second is 1The second is 2The second is 3

…..Continues from 4 to 57……The second is 58The second is 59The second is 60----Run finished----

Step 8: Paste your finished flowchart in the space below.

Page 5: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 5

Start

seconds ← 1

max_seconds ← 60

seconds > max_seconds

PUT "Seconds: " + seconds¶

seconds ← seconds + 1

End

Yes

No

Loop

The AccumulatorStep 1: Start Raptor and save your document as Lab 6-2Accumulator. The .rap file extension will be added automatically.

Step 2: The next loop to code is the pseudocode from Step 10, Lab 6.1. This loop will take in a number and accumulate the total. The complete pseudocode is below:

Declare Integer counterDeclare Integer total = 0Declare Integer number

For counter = 1 to 5Display “Enter a number: “Input numberSet total = total + number

End For

Display “The total is total: “, total

Page 6: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 6

Step 3: Click the Loop symbol and add it between the Start and the End symbol. Above the Loop symbol, add three assignment statements. Set a variable named counter to 1, a variable named total to 0, and a variable named number to 0.

Step 4: Double click the Diamond symbol and add the condition that will execute the loop through 5 iterations.

Step 5: Add an input statement if the loop is NO. This statement will ask the user to enter a number.

Step 6: Add an assignment statement that will accumulate the total such as total = total + number.

Step 7: Add an assignment statement that will increment the counter variable by 1.

Step 8: Add an output statement outside of the loop if the condition is YES. This should display total.

Step 9: Execute your flowchart to see if your output matches the following. If not, repeat the steps to identify the error and execute again.

Input values are:1323245218

The expected output is:The total is 130----Run finished----

Step 10: Paste your finished flowchart in the space below.

Page 7: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 7

Start

counter ← 1

total ← 0

number ← 0

counter > 5

"Enter a number"GET number

total ← total + number

counter ← counter + 1

PUT "Your total is " + total¶

End

Yes

No

Loop

The Average Age Step 1: Start Raptor and save your document as Lab 6-2AverageAge. The .rap file extension will be added automatically.

Step 2: The next loop to code is the pseudocode from Step 12, Lab 6.1. This loop will take in various amounts of ages and then find the average. The complete pseudocode is below:

Declare Integer counterDeclare Integer totalAge = 0

Page 8: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 8

Declare Real averageAge = 0Declare Integer ageDeclare Integer number

Display “How many ages do you want to enter: “Input number

For counter = 1 to numberDisplay “Enter age: “Input ageSet totalAge = totalAge + age

End For

averageAge = totalAge / number

Display “The average age is “, averageAge

Step 3: Click the Loop symbol and add it between the Start and the End symbol. Above the Loop symbol, add five assignment statements. Set counter to 1, totalAge to 0, averageAge to 0, age to 0, and number to 0.

Step 4: Above the Loop symbol, add an Input symbol that asks the user how many ages they want to enter. Store the answer in the number variable.

Step 5: Double click the Diamond symbol and add the condition that will execute the loop as long as the number is less than the counter. This can be written as counter > number.

Step 6: Add an input statement if the loop is NO. This statement will ask the user to enter an age.

Step 7: Add an assignment statement that will accumulate the totalAge.

Step 8: Add an assignment statement that will increment the counter variable by 1.

Step 9: Add an assignment statement outside of the loop if the condition is YES. This should calculate the averageAge as averageAge = totalAge / number.

Step 10: Add an output statement outside of the loop if the condition is YES. This should display averageAge.

Step 11: Execute your flowchart to see if your output matches the following. If not, repeat the steps to identify the error and execute again.

Input values are:4 – how many ages to enter

45

Page 9: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 9

673427

The expected output is:The average age is 43.2500----Run finished----

Step 12: Paste your finished flowchart in the space below.

Page 10: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 10

Start

counter ← 1

total_age ← 0

average_age ← 0

age ← 0

number ← 0

"How many ages would you like to input?"GET number

counter > number

"Enter an age"GET age

total_age ← total_age + age

counter ← counter + 1

average_age ← total_age / number

PUT "The average age is " + average_age¶

End

Yes

No

Loop

Page 11: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 11

Lab 6.3 – Python CodeThe goal of this lab is to convert all flowcharts in Lab 6.2 to Python code.

Step 1: Start the IDLE Environment for Python. Prior to entering code, save your file by clicking on File and then Save. Select your location and save this file as Lab6-3.py. Be sure to include the .py extension.

Step 2: Document the first few lines of your program to include your name, the date, and a brief description of what the program does.

Step 3: Start your program with the following code for main:

#Lab 6-3 Practicing for loops

#the main functiondef main():

#A Basic For loop

#The Second Counter code

#The Accumulator code

#The Average Age code #calls mainmain()

Step 4: Under the documentation for A Basic For Loop, add the following lines of code:

print 'I will display the numbers 1 through 5.'for num in [1, 2, 3, 4, 5]:

print num On the first iteration, 1 is placed into the variable num and num is then printed to the screen. The process is continued as follows:

Page 12: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 12

Execute your program. Notice that the output is as follows:

>>> I will display the numbers 1 through 5.12345>>>

Step 5: The next loop to code is the Second Counter code. This loop can be processed in the same way as Step 4; however, it would take a long time to write 1 through 60 in the for loop definition. Therefore, the range function should be used to simplify the process. Write a for loop that has a range from 1 to 61. If you stop at 60, only 59 seconds will be printed. If you only provide one argument, the starting value will be 0. (Reference the Critical Review section above for the exact syntax.)

Step 6: The next loop to code is the Accumulator code. Start by initializing a total variable to 0. This must be done in order to accumulate values.

Page 13: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 13

Step 7: The next step is to write a for loop that iterates 5 times. The easiest way to do this is the following.

for counter in range(5): Step 8: Inside the for loop, allow the user to enter a number. Then, add an accumulation statement that adds the number to total. In Python, the range function determines the number of iterations, so it is not necessary to manually increment counter.

Step 9: Outside of the for loop, use a print statement that will display the total.

Step 10: Compare your sample input and output to the following:

Enter a number: 54Enter a number: 32Enter a number: 231Enter a number: 23Enter a number: 87The total is 427

Step 11: The final loop to code is the Average Age code. Start by initializing totalAge and averageAge to 0. (Reference the Critical Review section above on Letting the User Control the Number of Iterations).

Step 12: The next step is to ask how many ages they want to enter. Store the answer in the number variable.

Step 13: Write the definition for the for loop using the range function such as:

for counter in range(0, number):

Step 14: Inside the for loop, allow the user to enter an age.

Step 15: Inside the for loop, add the code that will accumulate age into the totalAge variable.

Step 16: Outside of the loop, calculate the averageAge as averageAge = totalAge / number.

Step 17: Outside of the loop, display the averageAge variable to the screen.

Step 18: Compare your sample input and output to the following:

How many ages do you want to enter: 6Enter an age: 13Enter an age: 43Enter an age: 25

Page 14: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 14

Enter an age: 34Enter an age: 28Enter an age: 43The average age is 31>>>

Step 18: Execute your program so that all loops work and paste the final code below

PASTE CODE HERE def main(): # basic for loop print 'I will sisplay the numbers 1 through 5. ' for num in[1, 2, 3, 4, 5]: print(num)

#seccond counter loop for num in range(1,61): print(num)

#the accumulator code total=0 for counter in range(5): total= total + input('Enter a number') print('the total number is '+str(total))

#the avrage age code TotalAge=0 avrgAge=0 numberAge=input('How many ages do you want to avreage?') for Age in range(1,numberAge+1): TotalAge=TotalAge+input('What is the age of the '+str(Age)+' person?') avrgAge=TotalAge/numberAge print('The avrage age is '+str(avrgAge))main()

Page 15: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 15

Page 16: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 16

Lab 6.4 – Programming Challenge 1 – Average Test Scores

Write the Flowchart and Python code for the following programming problem based on the provided pseudocode.

Write a program that will allow a teacher to calculate the average test score for a certain number of students. The teacher can enter the number of students who took the test, and then the score for each student. Your program will then calculate the average score and print out the results. Your program must use the appropriate loop, modules, and run multiple times for different sets of test scores.

Your sample output might look as follows:

How many students took the test: 9Enter their score: 98Enter their score: 78Enter their score: 99Enter their score: 92Enter their score: 87Enter their score: 100Enter their score: 88Enter their score: 81Enter their score: 79The average test score is 89Do you want to end program? (Enter no to process a new set of scores): yes

The Pseudocode

Module main()//Declare local variables

Call declareVariables (endProgram, totalScores, averageScores, score, number, counter)

//Loop to run program againWhile endProgram == “no”

//reset variablesCall declareVariables (endProgram, totalScores, averageScores, score, number, counter)

//calls functionsCall getNumber(number)Call getScores(totalScores, number, score, counter)Call getAverage(totalScores, number, averageScores)Call printAverage(averageScores)Display “Do you want to end the program? (Enter no to process a new set of test

scores )”Input endProgram

Page 17: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 17

End WhileEnd Module

Module declareVariables(Real Ref endProgram, Real Ref totalScores, Real Ref averageScores, Real Ref score, Integer Ref number, Integer Ref counter)

Declare String endProgram = “no”Declare Real totalScores = 0.0Declare Real averageScores = 0.0Declare Real score = 0Declare Integer number = 0Declare Integer counter = 1

End Module

Module getNumber(Integer Ref number)Display “How many students took the test: ”Input number

End Module

Module getScores(Real Ref totalScores, Integer number, Real score, Integer counter)For counter = 1 to number

Display “Enter their score:”Input scoreSet totalScores = totalScores + score

End ForEnd Module

Module getAverage(Real totalScores, Integer number, Real Ref averageScores)Set averageScores = totalScores / number

End Module

Module printAverage(Real averageScores)Display “The average scores is “, averageScores

End Module

The Flowchart

Page 18: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 18

Start

declare_variables

endProgram = "yes"

declare_variables

"How many students took the test?"GET number

getScores

averageScores ← totalScores / number

PUT "The average score is " + averageScores¶

"Do you want to end the program?"GET endProgram

End

Yes

No

Loop

Page 19: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 19

Start

endProgram ← "no"

totalScores ← 0.0

averageScores ← 0.0

score ← 0

number ← 0

counter ← 1

End

Start

counter > number

"Enter the score"GET score

totalScores ← totalScores + score

counter ← counter + 1

End

Yes

No

Loop

The Python Code

PASTE CODE HERE#the main functiondef main(): endProgram = 'no' print while endProgram == 'no': totalScores = 0 averageScores = 0 number = 0 number = getNumber(number) totalScores = getScores(totalScores, number) averageScores = getAverage(totalScores, averageScores, number) printAverage(averageScores) endProgram = raw_input('Do you want to end program? (Enter no to process a new set of scores): ') #this function will determine how many students took the test

Page 20: Student Lab 1: Input, Processing, and Outputdevinsbakercollegeeportfolio.weebly.com/.../lab_6.doc · Web view#Lab 6-3 Practicing for loops #the main function def main(): #A Basic

Starting Out with Programming Logic and Design 20

def getNumber(number): number = input('How many students took the test: ') return number

#this function will get the total scoresdef getScores(totalScores, number): for counter in range(0, number): score = input('Enter their score: ') totalScores = totalScores + score return totalScores

#this function will calculate the averagedef getAverage(totalScores, averageScores, number): averageScores = totalScores / number return averageScores

#this function will display the averagedef printAverage(averageScores): print 'The average test score is', averageScores # calls mainmain()