Chapter 5 C++

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

You need to write a loop that will keep reading and adding integers to a sum, until the sum reaches or exceeds 21. The numbers are all less than 20 and the sum is initially 0. Which is the preferred loop construct to use?

do while loop

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.

do { cout << '*'; n--; } while(n>0);

Given an int variable count that has already been declared, write a for loop that prints the integers 50 through 1, separated by spaces. Use no variables other than count.

for (count=50; count>0; count--) cout << count << " "; for (count=50; count>=1; count--) cout << count << " ";

Given an int variable i that has already been declared, write a for loop that prints the integers 0 through 39, separated by spaces. Use no variables other than i.

for (i=0; i<40; i++) cout << i << " "; for (i=0; i<=39; i++) cout << i << " ";

Assume the int variables i and result, have been declared but not initialized. Write a for loop header -- i.e. something of the form for ( . . . ) for the following loop body: result = result * i; When the loop terminates, result should hold the product of the odd numbers between 10 and 20. NOTE: just write the for loop header; do not write the loop body itself.

for (i=11,result=1;i<20;i+=2) for (i=11,result=1;i<20;i++,i++) for (i=19,result=1;i>10;i-=2)

Assume that the int variables i, j and n have been declared, and n has been initialized. Write code that causes a "triangle" of asterisks to be output to the screen, i.e., n lines should be printed out, the first consisting of a single asterisk, the second consisting of two asterisks, the third consisting of three, etc. The last line should consist of n asterisks. Thus, for example, if n has value 3, the output of your code should be (scroll down if necessary): * ** ***

for (i=1;i<=n;i++) { for (j=1;j<=i;j++) cout << "*"; cout << "\n"; }

Write a for loop that prints the odd integers 11 through 121 inclusive, separated by spaces.

for (int k=11;k<=121; k+=2) cout << k <<" "; for (int k=11; k<123; k=k+2) cout << k <<" ";

Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by spaces.

for (int k=80; k>=20; k-=2) cout << k <<" "; for (int k=80; k>=20; k--,k--) cout << k <<" ";

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 for 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.

for (j=0; j<n; j++) cout << '*';

Write a for loop that computes the following sum: 5+10+15+20+...+485+490+495+500. The sum should be placed in a variable sum that has already been declared and initialized to 0. In addition, there is another variable, num that has also been declared. You must not use any other variables.

for (num = 5; num <= 500; num+=5) sum += num;

You have a variable, n, with a non-negative value, and need to write a loop that will keep print n blank lines. What loop construct should you use?

for loop

You need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use?

for loop

Write a loop that reads positive integers from standard input, printing out those values that are even, separating them with spaces, and that terminates when it reads an integer that is not positive. Declare any variables that are needed.

int x; cin >> x; while (x>0) { if (x%2==0) cout << x << " "; cin >> x; }

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.

int x; int esum=0; int osum=0; cin >> x; while (x>0) { if (x%2==0) esum += x; else osum += x; cin >> x; } cout << esum << " " << osum;

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read. Declare any variables that are needed.

int x; int sum=0; cin >> x; while (x>0) { if (x%2==0) sum += x; cin >> x; } cout << sum;

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=97; do { cout << '*'; k--; } while(k>0);

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.

n = -1; while (n<1 || n>10) { cin >> n; } do { cin >> n; } while (n<1 || n>10);

Given an integer variable strawsOnCamel, write a statement that uses the auto-increment operator to increase the value of that variable by 1.

strawsOnCamel++;

Given an integer variable timer, write a statement that uses the auto-decrement operator to decrease the value of that variable by 1.

timer--;

Given that two int variables, total and amount, have been declared, write a loop that reads integers into amount and adds all the non-negative values into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0.

total = 0; cin >> amount; while (amount>=0) { total += amount; cin >> amount; }

Given int variables k and total that have already been declared, use a 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.

total=0; k=1; while (k<=50) { total+=k*k; k++; }

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.

while (n>0) { n--; cout << '*'; }

You need to write a loop that reads integers and adds them to a sum as long as they are positive. Once 0 or a negative value is read in your loop terminates. Which is the preferred loop construct to use?

while loop


Ensembles d'études connexes

Igneous Rocks & Volcanoes, first chapter science, Weathering, Soils, and mass wasting test

View Set

Chapter 7 - Social Perception and Attributions

View Set

Agile Design Principles - Section 2: Introduction

View Set

HRM Chapter 1: Managing Human Resources

View Set