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

. What is the output of the following C++ code? int myfunction ( int arr[], int

ID: 3819916 • Letter: #

Question

. What is the output of the following C++ code?

int myfunction ( int arr[], int s, int e, intc){

            int y = 0; int i;

            if ((s>e) || (s<0)|| (e<0)) {return -1;}

            for (i=s; i<=e; i++);

                 if (arr[i] == c) {

                        y++;

                 }

                    return y;

            }

int main(){

            int x[10] = {9, 4, 9, 1, 2, 3, 9, 8, 11, 15};

cout << myfunction (x, 1, 6, 9) << endl;

return 0;

}

a. -1

b. 1

c. 2

d. 10

Note : Please provide a solution

Explanation / Answer

Answer) The answer would be 2.

Here basically, the no. of 9's in the arrays are counted. In the function named as myfunction, comparisons are made. i.e the value of x is taken as an array in the function, the value 1 is taken as s in function, the value 6 is taken as e in the function. since none of the conditions provided in the functions are met, therefore the for loop will execute. In the for loop, for every value of i for which the the value in the array is equal to c i.e.9, the value of y is incremented by 1. Therefore on running the for loop from 1st position to the 6th position, there are only two 9 in the array, therefore y is incremented by 2 only. And y becomes 2 which is returned to the main function and printed.