swap two arrays

Pages: 12
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
#include <iostream>

using namespace std;
void swapArray(int array1[],int element1, int array2[], int element2);

void swapArray(int array1[],int element1, int array2[], int element2)
{
        int temp;
    temp = array1[element1];
    array1[element1] = array2[element2];
    array2[element2] = temp;
    cout<<"array1 elements is:"<<array1[element1]<<endl<<"array 2 elements is:"<<array2[element2];
}

int main()
{
    int array1[5];
    int array2[5];
    int element1,element2;

 cout<<"please Enter Element of The first array"<<endl;
 cin>>element1;
 cout<<"please Enter Element of The second array"<<endl;
 cin>>element2;

 swapArray (array1,element1,array2,element2);


    return 0;
}


Write a function which takes two arrays as parameters (assume both arrays have the same size).
The function should swap the contents of both arrays (i.e contents of array 1 will be copied to
array 2 and vice versa). Write the function iteratively. Then test the correctness of
your functions by calling them from a test drive main program.

i guess what i tried so far is not as its suppose to solve this question because the output is different but i dont know what i have to change can anyone help me please?
You only swap one element. What you need is to call this swapArray in a loop for every index of the arrays.

Also, what kind of output do you expect? You never initialize your arrays.
so i have to write the for (int i=0;i<length;i++) in the main be4 the function call?

sorry i didnt get what did you mean by "what kind of output do you expect? You never initialize your arrays."
where should i do that if i have to write the array name[size of array];
where i have to initialize kind of the output??
so i have to write the for (int i=0;i<length;i++) in the main be4 the function call?
Sure.

sorry i didnt get what did you mean by <...>
In your original post you say
the output is different
so I have to ask, different from what? What is the output at all? And what do you expect to be? Unless you omitted some code, the arrays have random (garbage) values in them. How can you tell if they were swapped or not?
When you get element1 and element2, you forget to put them in your arrays.

In your swapArray() function, element1 and element2 should not even appear.
Replace them by 0 in the function's code.
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
#include <iostream>

using namespace std;
void swapArray(int array1[],int element1, int array2[], int element2);

void swapArray(int array1[],int element1, int array2[], int element2)
{
        int temp1,temp2;
    temp1 = array1[element1];
    array1[element1] = array2[element2];
    array2[element2] = temp1;
    cout<<"array1 elements is:"<<temp1<<endl;
    temp2 = array2[element2];
    array2[element2] = array1[element1];
    array1[element1] = temp2;
   cout <<"array 2 elements is:"<<temp2<<endl;
}

int main()
{
    int array1[5];
    int array2[5];
    int element1,element2;

for (int i=0;i<5;i++)
 {cout<<"please Enter Element of The first array"<<endl;
 cin>>element1;
 cout<<"please Enter Element of The second array"<<endl;
 cin>>element2;

 swapArray (array1,element1,array2,element2);
 }

    return 0;
}


here is the updated code.
fro example if i entered 12345 for the first array and 67890 for the second array they should swapped but in this code we will lose the original elements of array 2 because we save array 1 elements on it
As I told you, you forget to put your values into the arrays.
In main() your loop should be
1
2
3
4
5
6
7
8
9
for (int i=0;i<5;i++)
 {
    cout<<"please Enter Element of The first array"<<endl;
    cin>>element1;
    cout<<"please Enter Element of The second array"<<endl;
    cin>>element2;
    array1[k] = element1;
    array2[k] = element2;
 }


As for your swapArray() function, what are you trying to do with element1 and element2 ?
Last edited on
By the way you can swap two arrays only in case when they have equal sizes.:)
There is already standard function std::swap that can be used with arrays. So you can write simply

std:;swap( array1, array2 );

:)
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
#include <iostream>

using namespace std;
void swapArray(int array1[], int array2[]);

void swapArray(int array1[],int array2[])
{
        int temp1,temp2;
    temp1 = array1[5];
    temp2=array2[5];

    array1[5] = array2[5];
    array2[5] = temp1;
    cout<<"array 1 elements is:"<<temp1<<endl;

    array2[5] = array1[5];
    array1[5] = temp2;
   cout <<"array 2 elements is:"<<temp2<<endl;
}

int main()
{
    int array1[5];
    int array2[5];
    int element1,element2;

for (int i=0;i<5;i++)
 {
 cout<<"please Enter Element of The first array"<<endl;
 cin>>element1;
 cout<<"please Enter Element of The second array"<<endl;
 cin>>element2;
    array1[5] = element1;
    array2[5] = element2;
 swapArray (array1,array2);
 }

    return 0;
}


i understood what u have been just said but still the output is the same element of the the array itself its not swapping idk where is the error
vlad from moscow:
yea maybe but we are asked to write the function ^_^
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
#include <iostream>

using namespace std;
void swapArray(int array1[], int array2[]);

void swapArray(int array1[],int array2[])
{
        int temp1,temp2;
    temp1 = array2[5];
    temp2=array1[5];
    cout<<"array 1 elements is:"<<temp1<<endl;

   cout <<"array 2 elements is:"<<temp2<<endl;
}

int main()
{
    int array1[5];
    int array2[5];
    int element1,element2;

for (int i=0;i<5;i++)
 {
 cout<<"please Enter Element of The first array"<<endl;
 cin>>element1;
 cout<<"please Enter Element of The second array"<<endl;
 cin>>element2;
    array1[5] = element1;
    array2[5] = element2;
 swapArray (array1,array2);
 }

    return 0;
}


can we use that for swaping!
You made a mistake when you copied my code.

swapArray() should not be inside the loop.

In swapArray you are supposed to swap all the elements of array1 with those of array2.
So there should be a loop.
hmm i tried that too but still m having the same error that the element is displaying it self without swapping!
if the two arrays are int array1[100] and int array2[100]
You need a 3rd array int temp_array[100]
You can now use memcpy(temp_array,array1,n)where n = 100*sizeof(int)
to copy array1 to temp_array.
transfer array2 to array1 using same method
now transfer contents of temp_array to array2.
Y'all know C++ has swap_ranges(), right?
http://www.cplusplus.com/reference/algorithm/swap_ranges/
Last edited on
It's A Language.
why not just write a simple swap function that acts like one that swaps normal objects except you transfer though for loops? (and make everything a vector so you dont have to worry about size)
oh thanks a lot that's work
thanku all
oh guess what what i did is totally work gd by ur help but by that we are swapping the whole array well the question asked to swap each element in the array but sadly what shall do in order to do that? i know that i have to use the loop in the function but how swap the first element exactly then the second and so on?

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
#include <iostream>

using namespace std;
void swapArray(int array1[], int array2[]);

void swapArray(int array1[],int array2[], int size)
{
        int temp;
    temp= array1[5];
    array1[5] = array2[5];
    array2[5] = temp;
    cout<<"array 1 elements is:"<<array1[5]<<endl;
    cout <<"array 2 elements is:"<<array2[5]<<endl;
}

int main()
{
    int array1[5];
    int array2[5];
    int size=5;
    int element1,element2;

for (int i=0;i<5;i++)
 {
 cout<<"please Enter Element of The first array"<<endl;
 cin>>element1;
 cout<<"please Enter Element of The second array"<<endl;
 cin>>element2;
    array1[5] = element1;
    array2[5] = element2;
 }
 swapArray (array1,array2,size);
    return 0;
}
Consider your previous function swapArray. What you need is surely
1
2
3
4
5
6
7
swapArray(array1, 0, array2, 0);
swapArray(array1, 1, array2, 1);
swapArray(array1, 2, array2, 2);
...
swapArray(array1, i, array2, i);
...
swapArray(array1, 5, array2, 5);

You should see how that looks in a loop. When that works, reorganize your code into the functions you need.
Pages: 12