How to find the values being swapped in two different arrays?

How to find the values being swapped in two different arrays?
and not allowed to use the std library

{2, 3, 6}
{1, 4, 7}

will become

{1, 3, 7}
{2, 4, 6}

With 2 swaps, and the values being swapped are
1 & 2, 6 & 7

So far, I have gotten the swap and counter to work but I cannot seem to figure out how to find the individual values of the elements that are swapped, I can get only one value to show.

Can I get any hints on how to all grab the values? I am not allowed to use any additional libraries except for the input and output streams. Thanks ahead of time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  int main() {
    int a1[3] = { 2, 3, 6 };
    int a2[3] = { 1, 4, 7 };
    int i;
    int swapcount = 0;
    int swapvalue;

    std::cout << "Before swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        cout << " " << a1[i] << endl;

    }
    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    for (int i = 0; i < 3; i++) {
        if (a1[i] % 2 != 1) {
            swapcount++;
            int temp = a1[i];
            a1[i] = a2[i];
            a2[i] = temp;
            swapvalue = i;
        }
    }

    std::cout << "After swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a1[i] << endl;
    }

    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    std::cout << "swap count: " << swapcount << endl;
    std::cout << "swap value: " << swapvalue << endl;
}
Your current program is not even outputting a value. Its outputting the index of the array. More specifically, its outputting the final index which was swapped, which happens to be 2.

Last edited on
There are better solutions, but adding a cout statement in the if-statement seems to be a simple one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
  using namespace std;
  
  int main() {
    int a1[3] = { 2, 3, 6 };
    int a2[3] = { 1, 4, 7 };
    int i;
    int swapcount = 0;
    int swapvalue;

    std::cout << "Before swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        cout << " " << a1[i] << endl;

    }
    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    for (int i = 0; i < 3; i++) {
        if (a1[i] % 2 != 1) {
            swapcount++;
            int temp = a1[i];
            a1[i] = a2[i];
            a2[i] = temp;
            //swapvalue = i;
            cout << endl;
            cout << "The values swapped when i = " << i << " are " << a1[i] << " and " << a2[i] << endl;
        }
    }

    std::cout << "\nAfter swap:\n" << endl;
    std::cout << "Array 1:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a1[i] << endl;
    }

    std::cout << "Array 2:\n" << endl;
    for (int i = 0; i < 3; i++) {
        std::cout << " " << a2[i] << endl;

    }

    std::cout << "swap count: " << swapcount << endl;
    //std::cout << "swap value: " << swapvalue << endl;
}
Last edited on
Another solution is to make an array (maybe call it 'swapArray') the size of 6 (since that is the most amount of values that can be swapped). Fill up the array with the swapped values within the if-statement.

Then traverse through the array using a for-loop going up to 2*swapcount (as each swapcount means 2 values being swapped), and cout the values.
Last edited on
If you're able to use std::vector, I'd create a vector, and then push_back() the values that were swapped into the vector every time the swapValue gets called. Then have a for loop that calls on .size() and output the contents of that vector (the contents that got swapped).
Topic archived. No new replies allowed.