6. What is an infinite loop? Write the code for an infinite loop.
Infinite loops usually occur when the programmer forgets to write code inside the loop that makes the test condition false. In most circumstances you should avoid writing infinite loops.
Declare String keepGoing = " y" keepGoing == " y" 7. A For loop looks like what other loop in a flowchart?
A For loop looks like a counting- controll loop in a flowchart. 8. Why is it critical that accumulator variables are properly initialized?
An accumulator is used to keep a running total of numbers. In a loop a value is usually added to the current value of the accumulator. If it is not properly initialized it will not contain the correct total 9. What is the advantage of using a sentinel?
The advantage of using a sentinel is that when you are processing a long list of values with a loop a sentinel marks the end of a list of items.
10. Why must the value chosen for use as a sentinel be carefully selected?
Because it can not be mistaken as a regular value in a list
Algorithm workbench
3. Design a For loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50 . . . 1000
// Declare a counter variable.
Declare Integer
// Constant for the maximum value
Constant Integer MAX_VALUE = 1000
//Display the multiples of 10 from 0 through 1000
For counter = 0 to MAX_VALUE
Display counter
End For
4. Design a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered. //Declare Integer number
Declare an accumulator variable
Declare Integer total = 0
Declare Integer counter
For counter = 1 to 10
Display “Enter a number.”
Input number
Set total = total + number
End For
Display “The total is”, total
9. Convert the following While loop to a For loop:
Declare Integer count = 0 change to (Delcare integer count)
While count < 50 change to (For count 50) Display " The count is ", count Set count = count + 1 change to ( Set count = 1 to 50 )
End While change to ( End For)
10. Convert the following For loop to a While loop:
Declare Integer count
While count End while
Programming exercise
7. Average Rainfall Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. number numberOfMonths number TotalRainfall
display “enter the number of years” read numberOfYears
for every year in numberOfYears for every month in 1 to 12 display “ enter the rainfall in inches” read number_of_inches totalRainfall = totalRainfall + numberOfInches end for end for
numberOfMonths = numberOfYears * 12
display “number of months is numberOfMonths” display “total rainfall is totalRainfall” display average rainfall is (“totalRainfall / numberOfMonths”)
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies.
Integer Days = 0
Integer Pennies = 0
Accumulator = totalPennies
Display “how manys did you work”
Loop
day 1= 1 pennies day 2 = 2 pennies day 3 = 4 pennies day 4 = 8 pennies day 5 = 16 pennies day 6 = 32 pennies day 7 = 64 pennies day 8 = 128 pennies day 9 = 256 pennies day 10 = 512 pennies
Days< 0
Pennies*days = totalPennies totalPennies + penny + total pennies= days * days = 1
Display “you have made + totalpennies+ cents”
10. Design a program with a loop that lets the user enter a series of numbers. The user should enter – 99 to signal the end of the series. After all the numbers have been en-tered, the program should display the largest and smallest numbers entered. declare Integer lowest declare Integer highest declare Integer number lowest = 0 highest = 0 number = 0
Do While number -99 display("Enter a number.") Input(number) If number -99 And number < lowest Then lowest = number If number > highest Then highest = number
Loop
display("lowest = " & lowest) display("highest = " & highest)
Unit 8 Research assignment 1
List three examples that show when a count-controlled loop is better than a condition- controlled loop in a program. Counted-controlled loop is lnterates a specific number of time. When Counting Backward by Decrementing Set counter = counter – 1. When Incrementing by Values Other Than 1 // Declare a counter variable Declare Integer counter = 1 // Constant for the maximum value Constant Integer MAX_ VALUE = 11 Display the odd numbers from 1 // through 11.
While counter
You May Also Find These Documents Helpful
-
6. a loop that never hits a max value or that never turns true is infinite…
- 431 Words
- 4 Pages
Satisfactory Essays -
From my table of values above, it is clear that the change of sign from negative…
- 2430 Words
- 10 Pages
Powerful Essays -
The “job force” of Canebreak would have made Walt Disney smile! Every slave had an assigned job…
- 692 Words
- 3 Pages
Good Essays -
import java.util. Scanner; public class LoveCS { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter how many times you wish the message to be printed."); int limit = 0; limit= scan.nextInt(); int sum=0; int count=1; while (count <=limit) { System.out.println(count+" I love Computer Science!!"); sum+=count; count++; } System.out.println("Printed this message " + limit + " times."); System.out.println("The sum of the numbers from 1 to "+limit+" is "+sum); } } import java.util. Scanner; public class PowersOf2 { public static void main(String[]args) { int valuePowersOf2; int nextPowersOf2 =1; int exponent= 0; int count=0; Scanner scan = new Scanner(System.in); System.out.println("Enter A Number.");…
- 422 Words
- 2 Pages
Better Essays -
1) Initialization: the variable is initialized 2) Test: the loop tests the variable by comparing it to the maximum value 3) Increment: to increase the value during each iteration the loop increments the counter variable by adding 1 to it.…
- 1058 Words
- 5 Pages
Satisfactory Essays -
Connected to a JetDirect print server with an IP of 172.20.20.76 Accessible to all users…
- 254 Words
- 1 Page
Satisfactory Essays -
2. Display the contents of the file GRADES created in Problem 1. Each student’s record should appear on a separate line and include the total score (the sum of the three tests) for that student. For example, a line of output might be as follows:…
- 286 Words
- 2 Pages
Satisfactory Essays -
When the module is executing, what happens when the end of the module is reached? Computer jumps back to the part of the program that called the module…
- 401 Words
- 2 Pages
Satisfactory Essays -
Between 600 and 1450 CE., trade and exchange continually remained important and influential in around Eurasia and as well as in the Mexica society. Trade and exchange were main ideas that were both useful and necessary for success during this time. In Eurasia the use of the Silk Road and the Indian Ocean were both used as important trade routes. The Monsoons in the Indian Ocean were able to widen their agricultural standpoint allowing them to trade internally not only with their new goods but also with specific regions and their goods. In the Mexica society, surrounding territories had to pay a tribute to the Aztec capital and also had traded with surrounding areas. In the Islamic empires, goods such as cotton and spices were traded which were a continuous trade, in addition to the use of the Silk Road and the Indian Ocean, however, this led to new inventions, which then created the Aztec empire and later the crusades.…
- 638 Words
- 3 Pages
Satisfactory Essays -
The program will terminate because it is expecting an integer value and a “Wingding” will cause an error in the program which will cause it to crash.…
- 469 Words
- 2 Pages
Satisfactory Essays -
a) Find a candidate loop invariant I. Show the induction steps using which you find I.…
- 443 Words
- 3 Pages
Good Essays -
Add a further field, of your choosing, to the Track class, and provide accessor and mutator methods to query and manipulate it. Find a way to use this information in your version of the project; for instance, include it in a track’s details string, or allow it to be set via a method in the MusicOrganizer class.…
- 341 Words
- 3 Pages
Satisfactory Essays -
9. Run the script again using the at command to schedule the job in the future. Wait for the job to run to make sure it executes.…
- 268 Words
- 2 Pages
Good Essays -
An accumulator is used to keep a running total of numbers. In a loop, a value is usually added to the current value of the accumulator. If it is not properly initialized, it will not contain the correct total.…
- 457 Words
- 4 Pages
Satisfactory Essays -
Show all the possible interleavings of the execution of preceding two processes (Show this by giving execution “traces” in terms of the atomic statements)…
- 810 Words
- 4 Pages
Good Essays