cosc1315_l4_lab6_1

docx

School

Central Texas College *

*We aren’t endorsed by this school

Course

COSC 1315

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

4

Uploaded by JusticeUniverse2467

Report
Lesson 4 Lab 6.1 – For Loop and Pseudocode Critical Review A count-controlled loop iterates a specific number of times. Although you can write this with a while or a do-while loop as performed in Lab 5, most programming languages provide a loop known as the for loop. This loop is specifically designed as a count- controlled loop. The process of the for loop is: The loop keeps a count of the number of times that it iterates, and when the count reaches a specified amount, the loop stops. A count-controlled loop uses a variable known as a counter variable to store the number of iterations that it has performed. Using the counter, the following three actions take place (Initialization, Test, and Increment). The pseudocode for a for statement looks as follows: For counterVariable = startingValue to maxValue Statement Statement Statement Etc. End For 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.: In Step 1, the code will display the message "The hour is" followed by the value of the variable hours for each iteration of the loop. The loop iterates from 1 to 24 inclusive, as defined by the constant MAX_HOURS . Therefore, the loop will execute 24 times, displaying the hour from 1 to 24 on each iteration. 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 “Minute: “ + minutes 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 = 60 Declare Integer seconds For seconds = 1 to MAX_SECONDS Display “Second: “ + seconds End For Step 5: For loops can also be used to increment by more than one. Examine the following code. Constant Integer MAX_VALUE = 10 Declare Integer counter For counter = 0 to MAX_VALUE Step 2 Display “The number is “, counter End For Step 6: Explain what you think will be displayed to the screen in Step 5.: In Step 5, the code will display the message "The number is" followed by the value of the variable counter for each iteration of the loop. The loop iterates from 0 to 10 inclusive, with a step size of 2. Therefore, the loop will execute 6 times, incrementing the value of counter by 2 on each iteration. Each line represents one iteration of the loop, with the value of counter increasing by 2 on each iteration until it reaches or exceeds MAX_VALUE (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 = 200 Declare Integer counter For counter = 20 to MAX_VALUE Step 20 Display “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 numStudents Display “Student #”, counter End For Step 9: Explain what you think will be displayed to the screen in Step 8.: In Step 8, the code will prompt the user to enter the number of students in the class. After the user inputs the number of students, the loop will iterate from 1 to the value entered by the user ( numStudents ). During each iteration, it will display "Student #" followed by the current value of the counter . Step 10: For loops are also commonly used to calculate a running total. Examine the following code. Declare Integer counter Declare Integer total = 0 Declare Integer number For counter = 1 to 5 Display “Enter a number: “ Input number Set 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: In Step 10, the code will prompt the user to enter a number five times within the loop. For each iteration, it will accumulate the entered number into the total variable by adding it to the current value of total . After the loop completes, it will display "The total is: " followed by the final value of the total variable. Let's consider an example: Suppose the user enters the numbers 5, 10, 15, 20, and 25 when prompted. Then, the output will be: The total is: 75
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
This is because the loop will add each of the entered numbers to the total variable, resulting in a final total of 75. 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 counter Declare Integer totalAge = 0 Declare Real averageAge = 0 Declare Integer age Declare Integer number Display “How many ages do you want to enter: “ Input number For counter = 1 to number Display “Enter age: “ Input age Set totalAge = totalAge + age End For averageAge = totalAge / number Display “The average age is “, avergaeAge