Answer to Question #267533 in C++ for malika

Question #267533

What does the following program print and why? Be sure to explain why each line of output prints the way it does

    #include <iostream>
    using namespace std;

    int* maxwell(int* a, int* b)
    {
        if (*a > *b)
            return a;
        else
            return b;
    }

    void swap1(int* a, int* b)
    {
        int* temp = a;
        a = b;
        b = temp;
    }

    void swap2(int* a, int* b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    }

    int main()
    {
        int array[6] = { 5, 3, 4, 17, 22, 19 };

        int* ptr = maxwell(array, &array[2]);
        *ptr = -1;
        ptr += 2;
        ptr[1] = 9;
        *(array+1) = 79;

        cout << &array[5] - ptr << endl;

        swap1(&array[0], &array[1]);
        swap2(array, &array[2]);

        for (int i = 0; i < 6; i++)
            cout << array[i] << endl;
    } 
1
Expert's answer
2021-11-17T12:02:59-0500
// Output:
3
4
79
-1
9
22
19

The program uses three identical swap funtions and the integer pointer ptr 
recieves the max element between 0 and 2 element of the array

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS