Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Assume an array is declared as int num[15]; Write the code to assign to each

ID: 3774558 • Letter: 1

Question

1) Assume an array is declared as int  num[15];  

Write the code to assign to each location in the array the square of its position

Print out the array in reverse order.

  

2) Given that x and y are integer variables, write a switch block equivalent to the nested if’s below:

              if ((x<0 )||(x>5)) cout << “invalid value”;

          else if(x==1) cout << x++ ;

             else if ((x==2)||(x==3)) cout<< “x is 3 or 2”;

              else if ((x>3) && (x<6))cout << “x is 4 or 5”;

3)What is produced as output for the following?

#include <iostream>

   using namespace std;

     int   w =-3;

   int  refunc(int, int) ;

int main() {

int x=5, k =2, m = -3;

x = refunc(m, k );

cout << "x=" << x << endl;

cout << "k="<< k << endl;  

cout << "m=" << m<<endl;

cout << "w=" << w<< endl ;

return 0; }

int  refunc(int a, int b) {

a = a + 4;

b += a ;

   w = b + w;

   return b;   }

Explanation / Answer

Question 1:

Answer:

#include <iostream>

using namespace std;

int main()
{
int num[15];
for(int i=0; i<15; i++){
num[i] = i * i;
}
for(int i=14; i>=0; i--){
cout<<num[i]<<endl;
}

return 0;
}

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                        

sh-4.3$ main                                                                                                                                                                                                                

196                                                                                                                                                                                                                         

169                                                                                                                                                                                                                         

144                                                                                                                                                                                                                         

121                                                                                                                                                                                                                         

100                                                                                                                                                                                                                         

81                                                                                                                                                                                                                          

64                                                                                                                                                                                                                          

49                                                                                                                                                                                                                          

36                                                                                                                                                                                                                          

25                                                                                                                                                                                                                          

16                                                                                                                                                                                                                          

9                                                                                                                                                                                                                           

4                                                                                                                                                                                                                           

1                                                                                                                                                                                                                           

0

Question 2:

Answer:

switch(x){
case 1:cout << x++ ;break;
case 2:
case 3:cout<< "x is 3 or 2";break;
case 4:
case 5:cout<< "x is 4 or 5";break;
default: cout << "invalid value";
}

Question 3:

Answer:

x=3                                                                                                                                                                                                                         

k=2                                                                                                                                                                                                                         

m=-3                                                                                                                                                                                                                        

w=0