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

1. (TCO 3) Given the following code and flag = 4, what is the value of x after t

ID: 3768805 • Letter: 1

Question

1. (TCO 3) Given the following code and flag = 4, what is the value of x after the code executes?

int x = 0; switch (flag) { case 0: x++; break; case 1: case 2: x++; case 3: x++; break; case 4: x++; case 5: case 6: x++; case 7: x++; break; default: break; } (Points : 5)

x will be 1.

x will be 2.

x will be 4.

x will be 3.

       myFile.close();
       myFile.close("file.txt");
       ofstream myFile;
       myFile.exit("file.txt");

       30
       200
       400
       8

       int myFunc( int);
       int myFunc(int x)
   {
       return 2*x;
   }
       y = myFunc(3);
       int myFunc(int){}

       0
       40
       20
       10

       It is a postprocessor command.
       It is required to use cin and cout.
       It provides access to a user-created header file.
       All of the above

       16
       0
       8
       4

       If flag1 is greater than flag2, 2 is stored in X.
       If flag1 is less than flag2, 2 is stored in X.
       X, flag1, and flag2 are set to 1:02.
       If flag1 and flag2 is less than 1, 2 is stored in X.

Page 2

Question 1.1. (TCO 3) Write a range-based For loop to print out the sum of the values in the following array.


int x[] ={ 1,2,3,7,8,9,20,270}; (Points : 10)

2. (TCO 5) Write a program that passes a float[8] array in such a way as to assure the values in the array can be changed. (Points : 10)

2. (TCO 8) Which is the command used to close a file? (Points : 5)

Explanation / Answer

1. X value will be 3

when switch statement is excuted with flag as 4, excuction starts fr om case 4 to case 7 beacuse break statment is encounted only in case 7, once break is excuted control jumps out of swtich.

switch (flag) {
   case 0: x++; break;
   case 1: case 2: x++;
   case 3: x++; break;
   case 4: x++; [ will be excuted]
   case 5:
   case 6: x++;[ will be excuted]
   case 7: x++; break;[ will be excuted]
   default: break; }

2. myFile.close(); function flushes the associated buffers and closes the file

3. myArray[3][2] = 8

4. y = myFunc(3); is function call

5. X will be 10 ,

When myFunc(x) is called x = 10, ‘x’value will not change, Because in function myFunc a local variable ‘x’ is used not global variable ‘x’ and function call is call by value

6. #include <iostream> : It is required to use cin and cout.

7. X value will be 16

Here function called by reference, so the actual reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. In myFunc ‘x’ was multiplied 2 twice. Hence ‘x’ value is 16.

8. If flag1 is greater than flag2, 2 is stored in X.

    Ternary Operator:

            X = (flag1 <= flag2)? 1: 2;

If condition is true 1 will be stored in X or else 2 will be stored

Page 2

Question 1.1.

            int sum = 0;

               for(int i =0; i<8 ;i++){

                 sum += x[i];

               }

               cout<< "sum of x valuse "<<sum<< endl;

Question 1.2

           

#include <iostream>

using namespace std;

// Function call by refrence to float variable , means we can chage the values of array

void myFunction(float *x)

{

                float sum= 0;

                *(x+3) = 300.003;

                for(int i =0; i<8 ;i++){

                   sum += *(x+i);

                   };

}

int main(){

                float g_float[8] = {1.2, 3.4, 4.5, 5.4, 6.45,7.543,8.545};

                myFunction(g_float); // call by reference

                return 0;

}