Please help me with these questions!!! Im sooo lost

B) i) Suppose that we have this program fragment which is supposed to get the product of the numbers from 1 to max:

cout << "Enter a maximum value → ";

cin >> max;

// for you to fill in

for ( int count = 2; count <= max; count++ )

product *= count;

Write a correct statement (or statements) to completely define the accumulator in the above fragment.

ii) Rewrite the above for statement so that the loop no longer has a body but stills performs the same action.

C) i) Convert the following do-while loop to a while loop:

char val;

do

{

cout << "Enter your choice: ";

cin >> val;

} while (val < 'A' || val > 'Z' );

ii) For the loop:

a) For what values of val will the loop terminate?

b) What would the result be if the do/while condition was changed to:

while (val < 'A' && val > 'Z' );

// && instead of ||

D) Here is a for loop:

for ( c = 9, d = -5, y = 8; c > d && y != 0; ++d, --y )

{

if ( c < y )

cout << "Value" << endl;

else if ( d == y )

cout << d << " is the value" << endl;

v = c * d * y;

++d,

cout << v << " is the result" << endl;

}

i) Rewrite as a while loop.

ii) For what values of c, d, and y does the loop terminate? Be specific.

iii) What is the output?

Extra Credit – 9 Points

E) Correct the following without using the pow function. You must verify that the input will be correct. (I strongly suggest that you work this through by hand to verify what it does and how to correct it. Use simple numbers as examples.)

// Program uses a loop to raise num to a power

#include <iostream>

using namespace std;

int main()

{

int num, power, count;

cout << "Enter a base value --> ";

cin >> num;

cout << "Enter an exponent --> ";

cin >> power;

while (count++ < power )

num *= num;

return 0;

}

cout << "The result is: " << num << endl;

B) i) Suppose that we have this program fragment which is supposed to get the product of the numbers from 1 to max:

cout << "Enter a maximum value → ";

cin >> max;

// for you to fill in

for ( int count = 2; count <= max; count++ )

product *= count;

Write a correct statement (or statements) to completely define the accumulator in the above fragment.

ii) Rewrite the above for statement so that the loop no longer has a body but stills performs the same action.

C) i) Convert the following do-while loop to a while loop:

char val;

do

{

cout << "Enter your choice: ";

cin >> val;

} while (val < 'A' || val > 'Z' );

ii) For the loop:

a) For what values of val will the loop terminate?

b) What would the result be if the do/while condition was changed to:

while (val < 'A' && val > 'Z' );

// && instead of ||

D) Here is a for loop:

for ( c = 9, d = -5, y = 8; c > d && y != 0; ++d, --y )

{

if ( c < y )

cout << "Value" << endl;

else if ( d == y )

cout << d << " is the value" << endl;

v = c * d * y;

++d,

cout << v << " is the result" << endl;

}

i) Rewrite as a while loop.

ii) For what values of c, d, and y does the loop terminate? Be specific.

iii) What is the output?

Extra Credit – 9 Points

E) Correct the following without using the pow function. You must verify that the input will be correct. (I strongly suggest that you work this through by hand to verify what it does and how to correct it. Use simple numbers as examples.)

// Program uses a loop to raise num to a power

#include <iostream>

using namespace std;

int main()

{

int num, power, count;

cout << "Enter a base value --> ";

cin >> num;

cout << "Enter an exponent --> ";

cin >> power;

while (count++ < power )

num *= num;

return 0;

}

cout << "The result is: " << num << endl;

Topic archived. No new replies allowed.