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

C++ C++ C++ 1. Consider the following: struct database { int id_number; int age;

ID: 3574226 • Letter: C

Question

C++ C++ C++

1. Consider the following:

struct database {
int id_number;
int age;
float salary;
};

How would you set the age variable equal to 22?

2. What is wrong with this function?

void swapShellsFirstInArray(int &basket[5])
{
int temp = basket[0];
basket[0] = basket[1];
basket[1] = temp;
}

3. I have declared the following function:

Here is my main function:

What is wrong?

4. The std::binary_search function returns the index number of the val if it is found. Otherwise, it returns -1.

5. Consider the following function named mystery:

void mystery()

{

myclass foo;

int original_num = 1;

std::cout << original_num << std::endl;

foo.passbyValue(original_num);

std::cout << original_num << std::endl;

foo.passbyRef(original_num);

std::cout << original_num << std::endl;

}

How would you call (activate) this function?

6. Consider the following code. Assume that the program includes iostream and uses namespace std.

Why would you get identifier not found errors?

int main ()
{
int i;
do {
    cout << "Type a number (0 to exit): ";
    cin >> i;
    odd (i);
} while (i!=0);
return 0;
}

void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd. ";
else even (a);
}

void even (int a)
{
if ((a%2)==0) cout << "Number is even. ";
else odd (a);
}

A. Because the functions even and odd are not declared prior to the function calls. Their prototypes are needed above main:

void odd (int a);
void even (int a);

__________________________________________________________________________

7. Consider the following program:

select all that apply

int operate (int a, int b)
{
return (a*b);
}

float operate (float a, float b)
{
return (a/b);
}

int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << " ";
cout << operate (n,m);
cout << " ";
return 0;
}

Which line(s) of code could be used to call this function (select all that apply):

float operate (float a, float b)

8. Consider the following prototype:

select all that apply

void duplicate (int& a, int& b, int& c)

What do the ampersands (&) signify? select all that apply

D. The ampersands signify that their corresponding arguments are to be passed by value instead of by reference.

9. Consider the following recursive binary search function:

select all that apply

int search::rBinarySearch(int sortedArray[], int first, int last, int key)
    {
       if (first <= last) {
           int mid = (first + last) / 2; // compute mid point.
           if (key == sortedArray[mid])
               return mid;   // found it.
           else if (key < sortedArray[mid])
               // Calls itself for the lower part of the array
               return rBinarySearch(sortedArray, first, mid-1, key);
           else
               // Calls itself for the upper part of the array
               return rBinarySearch(sortedArray, mid+1, last, key);
       }
       return -1;
    }

Which of the following might be described as the base case(s) for this method to end the recursion? select all that apply

B. When the midpoint value is greater than the target (when this statement is true)

C. when the value is found in the middle of the range (when this statement is true):

D. when there are no elements in the specified range of indices (when this statement is false):

10. Consider the following:

select all that apply

    int BinarySearch(int A[], int key, int low, int high)  
    {
  
        while (low <= high)  
        {
            int m = (low + high) / 2;
            if (key < A[m])
            {
                high = m - 1;
            }
            else if (key > A[m])
            {
                low = m + 1;
            }
            else
            {

                return m;
            }
        }

        return -1;
    }

What is the purpose of iteration in the above code? select all that apply

  A. database.age(22);  

Explanation / Answer

1. database employee;

employee.age = 22;

This is the correct way of assigning the value to the varaible. By the above statements we are clearly telling that we have a method or structure named database and we are creating a reference variable for the method in main function and with that variable we are calling the age and giving an input 22.

2. The array size is too large in the parameter list. Intially it was array size as 5 bytes but we are using 2 bytes while storing in temp variable so remainig will be memory waste what are actually needed.

3. The array is empty when the findfirst function is called. here in findfirst function array was declared but we diidnt initialised which contains only null values or garbage values.

4. False is the answer for this one as in the binary search it will give the value if found otherwise it gives the given criteria as output not specific as -1 i.e. if we specified not founf then it prints not found, if we specify -1 then it give -1.

5.foo.mystery(); The methos will be activated when we call it with the particular class reference with in the method. Here the function is mystery(), the class reference is foo.

6. Because the functions even and odd are not declared prior to the function calls. Their prototypes are needed above main:

void odd (int a);

void even (int a);

In C++ when we want to write function we must have to define the prototypes in the main function compulsorily.

7.   float n=5.0,m=2.0;

cout << operate (n,m); and

8. The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.

9.when the value is found in the middle of the range (when this statement is true):

When we found the needed element as a mid element that is the best case to end the method recursion.

10.To set the index point in the middle so the right hand side of the array can be searched before the left hand side is searched.