Chapter 5 MPL
int counter; counter = FIRST_YEAR; while ( counter <= LAST_YEAR ) { cout << counter << ": " << oil << endl; oil *= 1.20; counter++; }
Assume that two int constants ,FIRST_YEAR and LAST_YEAR have already been declared and initialized with year values (like 2009, 2014), along with a double variable oil that has been initialized with the number of barrels of oil consumed in Canada in the year given by FIRST_YEAR. Write some code that uses a while statement to print on a line by itself, each of the years from FIRST_YEAR to LAST_YEAR inclusive. On each line, after the year, separated by a colon and a space, print the new value amount of oil, taking into account that each year the oil consumed increases by 20%.
header
The first line in a while expression is also called the loop __________.
while (s != "Y" && s != "y" && s != "N" && s != "n" ) { cin >> s; }
Given a string variable s that has already been declared , write some code that repeatedly reads a value from standard input into s until at last a "Y" or "y"or "N" or "n" has been entered.
k=1; do { cout << "*"; k++; } while (k < 98);
Given an int variable k that has already been declared , use a do...while loop to print a single line consisting of 97 asterisks. Use no variables other than k.
k = 1; while (k < 98) { cout << "*"; k++; }
Given an int variable k that has already been declared , use a while loop to print a single line consisting of 97 asterisks. Use no variables other than k.
cin >> n; while (n < 1 || n > 10) { cin >> n; }
Given an int variable n that has already been declared , write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered.
j = 0; do { cout << "*"; j++; } while ( n > j);
Given an int variable n that has already been declared and initialized to a positive value , and another int variable j that has already been declared , use a do...while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.
j = 0; while ( n > j ) { cout << "*"; j++; }
Given an int variable n that has already been declared and initialized to a positive value , and another int variable j that has already been declared , use a while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.
strawsOnCamel++;
Given an integer variable strawsOnCamel, write a statement that uses the auto-increment operator to increase the value of that variable by 1.
timer--;
Given an integer variable timer, write a statement that uses the auto-decrement operator to decrease the value of that variable by 1.
k = 1; total = 0; do { total += (k*k); k++; } while ( k <= 50);
Given int variables k and total that have already been declared , use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.