Question: Could you please answer the bonus question in C++ Since were now dealing with iteration logic in the labs and lecture, here are some small
Could you please answer the bonus question in C++
Since were now dealing with iteration logic in the labs and lecture, here are some small looping problems for practice. For the first problem in this homework, write a single program that does the following: a) prints out the even integers between 2 and 100.
b) prints out the integers that are multiples of 3 from 99 down to 3.
c) prints out the integers between 2 and 1,048,576 (220) that are integer powers of 2.
d) prints out the integers between 1,048,576 down to 2 that are integer powers of 2.
e) does something new involving loops, different than any of the above. (This is an exercise in being creative. Give yourself a task, and solve it.) For each of these problems, include a counter variable inside the loop that counts how many numbers are printed out. Report that result as well. The results for problems (a) and (b) should be printed out with 10 integers per line, right justified in a 5 byte field. For example, the output for the even integers between 2 and 40 will look like: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 Number of numbers = 20. Some of the numbers are fairly big in problems (c) and (d), so the output looks better if you print out 8 numbers per line instead of 10. Youll want to adjust the field size to handle the larger numbers as well. You may use any type of loop you want, but make sure to use at least one instance of all three looping structures. Thus, if you decide to use a for loop for part (a), you might consider using a while or do-while loop for (b). Since there are five problems, there will obviously be two duplicate loop types among the problems. But each of the three looping structures should be represented at least once. Note that you do not need to write separate programs for each of these problems. They should all be done within one program. However, leave a comment before each segment of code that describes exactly which problem is being solved. This is especially important for part (e). Finally, please dont use the pow() function for this assignment, especially for parts (c) and (d). Part of my purpose in assigning those problems was to make the point that updating a loop control variable can involve any type of operation, not just addition and / or subtraction. If we use the pow() function to control the exponents in parts (c) and (d), we convert those problems into basic iteration problems and defeat that purpose. Bonus Question For a voluntary analysis question, worth five bonus points on the assignment, can you come up with a formula for the number of numbers printed for each of the five problems as a function of n, the maximum number? (The formula will be different for each problem.) As an example, a formula for the number of numbers for an algorithm that prints the even numbers between 2 and some even number n might be f(n) = n/2. When that is applied to n=40, as in the example above, we get 40/2 = 20 numbers, exactly as our program reveals. What about the other problems? Of particular difficulty are problems (c) and (d).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
