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;
These are the answers i get i dont really understand this but my teacher says these are wrong
cout << "Enter a maximum value → ";
cin >> max;
// for you to fill in
for ( int count = 2; count <= max; product *= count count++ )
product *= count;
i)
product= count
Product * ( product +1) = count
ii) for ( int count = 2; count <= max; product *= count count++ )
Part C)
i)
char val;
while (val< ‘A’ || val > ‘Z’ );
{
cout <<”Enter your choice: “;
cin>> val;
}
ii)
A) The loop terminates when val is less than 65 but greater than 0 or if it is greater than 90.
B) The statement would never be true because a number could never lie in two different number segments that don’t overlap.
Part D)
i)
int c=9, d=-5, y=8, v;
while (c>d && y!=0)
{
If (c < y)
cout << “Value” << endl;
Else if ( d==y)
Cout << d<< “ is the value” <<endl;
v= c*d*y;
++d, --y
cout<< v << “is the result”<< endl;
ii) The loop terminates for y when it equals 0 and when d is greater than c.
iii)
-360 is the result
-189 is the result
-54 is the result
45 is the result
108 is the result
135 is the result
126 is the result
EC)
#include <iostream>
using namespace std;
int main()
{
int num, power, result, count;
cout << "Enter a base value --> ";
cin >> num;
cout << "Enter an exponent --> ";
cin >> power;
num *=result
num+1
for(result = num , num =1, power>=1; ++num)
cout << "The result is: " << num << endl;
First of all, for all your code, please use the code tag, it will be easier for us to read.
For the EC question:
The algorithm is wrong. For example, if my num = 5, power = 3, then according on your algorithm, then it will be 5*5 = 25, next will be 25*25 = 625, 625*625=390625 (which is wrong). In additional, you will need to initialize your count, that will be int count = 0;, so that the count can start increasing from 0 all the way to the power value.
I have corrected a bit of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main()
{
int num=0, newnum = 1, power=0, count =0;
cout << "Enter a base value --> ";
cin >> num;
cout << "Enter an exponent --> ";
cin >> power;
while (count++ < power )
newnum = num*newnum;
return 0;
}
You really should use code brackets, it makes looking at your code immensely easier. (Programmers eyes usually go bad anyways). Just put your code in the
tags.
For Part C:
You almost got it :). You should ask for an input from the user first, then proceed with the while loop. Not proceed with a while loop, then ask for an input.
1 2 3 4 5 6 7
char val;
cout<< "Enter your choice: ";
cin >> val;
while (...)
{
...
}
These are the comments hes leaving me B i) You're guessing. The code is getting the product of the numbers from 2 to max. Do it by hand using small numbers and see what product should be.
ii) Missing answer for i) and missing something at the end
Ok, for Part B, not too sure if I read the question correctly.
1 2 3 4 5 6 7 8 9 10 11
int main()
{
int max; //Declare the 'max' first.
cout << "Enter a maximum value";
cin >> max;
int product = 1; //Declare and initialize 'product', so that we can do the multiplication.
for ( int count = 2; count <= max; count++ )
product *= count; //Start multiplying from 2 to all the way to 'max'
system("pause");
}
Hope this helps :)
**By the way, please try to use the code tag (Button at the side showing <>) for all your code. It is really difficult to read your code.