What is the output of the following program segment? in C++ double temp[ 5]; for
ID: 3569171 • Letter: W
Question
What is the output of the following program segment? in C++
double temp[ 5];
for ( int i = 0; i < 5; i++)
temp[ i] = pow( i, 2.0) + 2;
for ( int i = 0; i < 5; i++)
cout << temp[ i] << " "; cout << endl;
temp[ 0] = pow( temp[ 1], 3);
temp[ 1] = temp[ 4] - temp[ 2];
temp[ 2] = temp[ 0] - 5;
for ( int i = 0; i < 5; i++) cout << temp[ i] << " "; cout << endl;
I have two more if anybody can help....
What is stored in list after the following C++ code executes?
int list[ 10];
for ( int i = 0; i < 5; i++)
{ list[ i] = i * i - 5;
if ( i % 3 == 0) list[ i] = list[ i] + i;
else list[ i] = list[ i] - i; }
and this one.
What is stored in list after the following C++ code executes?
int list[ 10];
list[ 0] = 2;
list[ 1] = 3;
for ( int i = 2; i < 10; i++)
{ list[ i] = list[ i - 1] + list[ i - 2];
if ( i > 7) list[ i] = 2 * list[ i] - list[ i - 2]; }
Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double temp[ 5];
for ( int i = 0; i < 5; i++)
temp[ i] = pow( i, 2.0) + 2; //square i=0,1,2,3,4 and add 2 to it
/*so
* temp[0]=2
* temp[1]=3
* temp[2]=6
* temp[3]=11
* temp[4]=18
*/
for ( int i = 0; i < 5; i++)
cout << temp[ i] << " "; cout << endl; //Prints the values of temp[i]
temp[ 0] = pow( temp[1], 3); //temp[0]=pow(3,3) i.e cube of 3 =3*3*3=27
temp[ 1] = temp[ 4] - temp[ 2]; //temp[1]=18-6=12
temp[ 2] = temp[ 0] - 5;//temp[2]=27-5=22
/*so new values
* temp[0]=27
* temp[1]=12
* temp[2]=22
* temp[3]=11
* temp[4]=18
*/
for ( int i = 0; i < 5; i++)
cout << temp[ i] << " "; //Prints the values of new temp[i]
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.