Intro to Programming
Unit 7 Assignment 1: Homework
1. Why should you indent the statements in the body of a loop? You visually set them apart from the surrounding code.
2. Describe the difference between pretest loops and posttest loops? A pretest loop is a loop tests the condition before performing the iteration. A posttest loop performs the iteration, then tests the condition.
3. What is a condition-controlled loop? A condition-controlled loop uses a true/false condition to control the number of times it repeats.
4. What is a count-controlled loop? A count-controlled loop repeats a specific number of times.
5. What three actions do count-controlled loops typically perform using the counter variable? Initialization: Before the loop begins, the counter variable is initialized to a starting value. Test : The loop tests the counter variable by comparing it to a maximum value. If the counter variable is less than or equal to the maximum, the loop iterates. If the counter is greater than the maximum value, the program exits the loop. Increment : To increment a variable means to increase its value. During each iteration, the loop increments the counter variable by adding a predetermined amount to it
Algorithm Workbench
1.declare integer product declare integer number product = 0 do while product < 100 display "Enter a number" input number product = number * 10 loop display product
2. Design a Do-While loop that lets the user enter two numbers. do{ sum = 0; print("Enter A: ") read A print("Enter B: ") read B sum = A + B print("Sum = " + sum) print("Do you wish to continue?") read RESPONSE } while(RESPONSE) 7. Convert the While loop in the following code to a Do-While loop: int x; do { cout << "enter a number: "; cin >> x; } while (x > 0) 8. Convert the Do-While loop in the following code to a While loop:
While sure ! = “y”
Display “Are you sure you want to quit?”
Input sure
End While
3. def check_budget ( expenses , budget ) : if expenses < budget : difference = format ( budget−expenses, ’ ,.2 f ’ ) print (”You were $” , difference , ” under budget .” , sep=””) if expenses > budget : difference = format ( expenses − budget , ’ ,.2 f ’ ) print (”You were $” , difference , ” over budget .” , sep=””) else : print (”You were right on budget . Congrats !”)
budget = float ( input (”What’ s your budget this month? ”) ) print (” Please enter each of your expenses from this month :”)
total expenses = 0 keep going = ’y ’
while keep going == ’y ’ : expense = float ( input (”How much was the expense? $”) ) total expenses = total expenses + expense keepgoing = input (”Do you have any additional expenses ? (
Enter y for yes . ) ”)
Check budget ( total expenses , budget )
You May Also Find These Documents Helpful
-
6. Write a pseudocode statement that declares the variable total so it can hold integers. Initialize the variable with the value 0.…
- 1062 Words
- 8 Pages
Satisfactory Essays -
4) (1 point) What kind of control is this control procedure – a preventive or detective control? Explain.…
- 711 Words
- 3 Pages
Good Essays -
1. A while loop will not execute the body of the code if you have your test condition incorrect.…
- 3740 Words
- 15 Pages
Satisfactory Essays -
it the functions listed below for Exercise 2. In each case, the appropriate error message should be generated if an invalid condition occurs. For example, an error message should be generated when trying to insert an item in a given location in the list and the location is out of range, a. ArrayList(inl size): create a constructor that sets the size of the array list to the value passed in size (note that the class variable SIZE cannot be final anymore), b. int length(): create this function to determine the number of items in the list (accessor function), c. int gelSize(): create this function to determine the size of the list (accessor function), d. void clear(): create this function to remove all of the items from the list. After this operation, the length of the list is zero, e. void replace(int location, int item): create this function to replace the item in the list at the position specified by location. The item should be replaced with item. f. void insert(int location, int item): create this function to add an item to the list at the position specified by location, g. void remove(int item): create this function to delete an item from the list. All ...…
- 714 Words
- 3 Pages
Good Essays -
Step 9: Explain what you think will be displayed to the screen in Step 8. (Reference:…
- 365 Words
- 2 Pages
Satisfactory Essays -
7. The program will calculate and display a final total for the items that are purchased.…
- 498 Words
- 2 Pages
Satisfactory Essays -
DQ 1: Review the definition of control structure on p. 45 in Extended Prelude to Programming: Concepts and Design (2nd ed.). Then, think about the pseudocode algorithm you would write for a simple task (making a peanut butter sandwich, for example) as well as three simple control structures that could be used to create this algorithm.…
- 437 Words
- 2 Pages
Satisfactory Essays -
2. A pretest loop is a loop tests the conditions before performing the iteration. A posttest loop performs the iteration then test the condition.…
- 261 Words
- 2 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 -
The client-server configuration that I would recommend would be a server-based network. The reason I would recommend this configuration is because a server-based network enables the business to have a data center without an IT staff. The services that this server would supply would be; file management, application management, mobility, collaboration, security, and performance.…
- 260 Words
- 1 Page
Satisfactory Essays -
Control : Continuously monitor the process as it continues using the measuring systems developed. Set up appropriate corrective actions for anticipated deviations in the process.…
- 790 Words
- 4 Pages
Good Essays -
According to Brech, controlling is a systematic exercise which is called as a process of checking actual performance against standards or plans with a view to ensure adequate progress and also recording such experience as is gained as a contribution on to possible future needs.…
- 1077 Words
- 5 Pages
Powerful Essays -
Control is a management function that focuses on the process of monitoring activities to ensure that they are being accomplished as planned; they ensure that plans are effective; they make the organisation effective and efficient and aid in decision-making.…
- 1854 Words
- 8 Pages
Better Essays -
Another type of control is referred to as concurrent control. This takes place when the work activity is in process and seeks to correct problems…
- 748 Words
- 3 Pages
Good Essays -
• Feedback loop: Feed loop is sometimes called balancing loop since its function to keep situation in balance. (Deviation from the desired state in one direction balanced by an action in the opposite direction).…
- 978 Words
- 4 Pages
Good Essays