swap two arrays

Pages: 12
can anyone test this code is it working to swap each element in the array one by one?
because its keep swapping the last two elements that i've beem entered!

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

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

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

    int element1,element2;

for (int i=0;i<size;i++)
 {
 cout<<"please Enter Element of The first array"<<endl;
 cin>>element1;
 cout<<"please Enter Element of The second array"<<endl;
 cin>>element2;
    array1[size] = element1;
    array2[size] = element2;
 }
 swapArray (array1,array2,size);
 
    return 0;
}
shall i put pointer to each element in the arrray?
or there is other way for that?
once again what is the issue with what i suggested? im going to make a function that does what your asking in ten minutes.
what i did is swapping the whole array like if we swap the arrays names but what i actually need a function that swap each element one by one . but actually i dont know where is the problem in my code since its works 100%
even when m displaying array[5] which suppose to be an error cuz the size is 5 so it should be print from 0 to 4 anyway idk
i have this. it almost works but i when i tested it arrayOne didnt have the write output

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
void swapArrays(int* arrayOne, int* arrayTwo)
{
    int size = sizeof(arrayOne), counter;
    vector<int> tempArray(sizeof(arrayOne));
        
    //temp holds aOne
    for(counter = 0; counter < size; counter = counter + 1)
    {
        tempArray[counter] = arrayOne[counter];
    }

    //aOne holds aTwo
    for(counter = 0; counter < size; counter = counter + 1)
    {
        arrayOne[counter] = arrayTwo[counter];
    }
    
    int tempSize = sizeof(arrayTwo);
    
    //aTwo holds temp
    for(counter = 0; counter < tempSize; counter = counter + 1)
    {
        arrayTwo[counter] = tempArray[counter];
    }
}
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
#include <iostream>

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

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

    int element1,element2;

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

    return 0;
}


this code took the 5 element but just swap the last two elements !
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
#include <iostream>

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

void swapArray(int &array1[],int &array2[], int size)
{
        int temp;

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

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

for (int y=0;y<size;y++)
 cout<<"array 1 elements is:"<<array1[y]<<endl;
 for (int z=0;z<size;z++)
cout <<"array 2 elements is:"<<array2[z]<<endl;

    return 0;
}


can anyone tell me why it is not working for swapping!
??
There are a number of reasons.

Firstly, arrays in C and C++ are zero-indexed. This means that the first element has an index of 0, the second has an index of 1, and so on. When you create array1 and array2, you're giving them a size of 5, but on lines 20 and 21, you're trying to initialise the 6th element of each array - which is beyond the end of the array. This will cause you to be setting values in memory that could be being used for something completely different, such as another variable.

On top of that, you haven't initialised element1 and element2, so even if array1[size] and array2[size] were valid array members, you would be "initialising" them to random uninitialised values anyway.

While you're attempting to initialise the non-existant 6th element of your arrays, you're not initialising any of the elements that do actually exist. So the arrays you pass into swapArray() are full of random, unitialised values.

In your for loop starting at line 23, you repeatedly read a number into element1, but do nothing with it before reading the next number. But you never use element1 anyway, so none of the values you read in ever get used.

The same is also true of the loop starting at 26, reading values into element2 that never get used.

You only ever call swapArray once, and you pass in the value of size. This means that the only elements you are trying to swap are the non-existent 6th elements. You should be looping over all the elements and swapping them.

You could have found a lot of these things out yourself, if you'd stepped through the code with a debugger, or even if you'd just output some debugging information to std::cout to show you what was happening.

Hope that helps.
Last edited 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[],int size);

void swapArray(int array1[],int array2[], int size)
{
    int temp;
    temp= array1[size];
    array1[size] = array2[size];
    array2[size] = temp;

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

    array1[size-1] = element1;
    array2[size-1] = element2;
for (int i=0;i<size;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,array2,size);
 }
 cout<<"array 1 elements is:"<<array1[size-1]<<endl;
cout <<"array 2 elements is:"<<array2[size-1]<<endl;

    return 0;
}


m still getting totally different output!
Well, you've fixed some of the issues I've mentioned, but ignored others.

You're still only initialising the 5th (size -1) element of your arrays, leaving the first 4 elements set to random garbage.

You're still not initilising element1 and element2, so the number you're initialising your 5th element to is random garbage.

You're still reading values into element1 and element2 but doing nothing at all with those values.

You're still only ever calling swapArray with size as the argument, so you always swap the same elements.

Seriously: use a debugger, or else just add some debugging output, so you can see for yourself what your code is doing.


Edit: Also, it would be much easier for you and us to see what your code is doing if you would indent your code sensibly. For example:
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  array2[],int size);

void swapArray(int array1[],int array2[], int size)
{
    int temp;
    temp= array1[size];
    array1[size] = array2[size];
    array2[size] = temp;

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

    array1[size-1] = element1;
    array2[size-1] = element2;
    for (int i=0;i<size;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,array2,size);
    }
    cout<<"array 1 elements is:"<<array1[size-1]<<endl;
    cout <<"array 2 elements is:"<<array2[size-1]<<endl;

    return 0;
}


It makes it much clearer what's inside the loop, and what's outside it.
Last edited 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
35
36
37
38
39
#include <iostream>
using namespace std;

	void swapArray(int array1[], int array2[], int i);
	void swapArray(int array1[], int array2[], int i)
	{
	   int temp = array1[i];               // Set temp to array1[i]
	    array1[i] = array2[i];          // Set array1[i] to array2[i]
	    array2[i] = temp;               // Set array2[i] to temp
	}

	int main()
	{

        const int size=5;
        int array1[size];
        int array2[size];
	    int element1=0,element2=0;

	    for (int i=0;i<size;i++)  // to take the 5 elements from the user
	    {
	        cout<<"please Enter Element of The first array"<<endl;
	        cin>>element1;
            cout<<"please Enter Element of The second array"<<endl;
            cin>>element2;
                 array1[i] = element1;      //then save them in the array to send them to the function and swap them
                 array2[i] = element2;
        swapArray(array1,array2,i);  //function call
                	    }

	    for(int i=0;i<size;i++)              // Display the arrays
	    {
	        cout<<"\n\n Array 1 elements is:"<<array1[i]<<endl;
	        cout <<"\n Array 2 elements is:"<<array2[i]<<endl;
	    }

	    return 0;
	}


wow, now it works completely but what if i want the loop to be in the function itself
Well, there's no reason why you can't have a loop in main, for the user to input the members of the arrays, and then exit the loop. Then, have another loop inside swapArray() that loops from 0 to size (which you'll have to pass in as an argument) and performs the swap.

In fact, looking at the wording of the original assignment, that's probably what your tutor intended.
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
#include <iostream>
using namespace std;

	void swapArray(int array1[], int array2[], int i, int size);
	void swapArray(int array1[], int array2[], int i,int size)
	{
	    int temp;
	    for (int i=0;i<size;i++)
	   {
        temp = array1[i];               // Set temp to array1[i]
	    array1[i] = array2[i];          // Set array1[i] to array2[i]
	    array2[i] = temp;               // Set array2[i] to temp
	   }
	}

	int main()
	{

        const int size=5;
        int array1[size];
        int array2[size];
	    int element1=0,element2=0;

	    for (int i=0;i<size;i++)  // to take the 5 elements from the user
	    {
	        cout<<"please Enter Element of The first array"<<endl;
	        cin>>element1;
            cout<<"please Enter Element of The second array"<<endl;
            cin>>element2;
                 array1[i] = element1;      //then save them in the array to send them to the function and swap them
                 array2[i] = element2;
        swapArray(array1,array2,i,size);  //function call
                	    }

	    for(int i=0;i<size;i++)              // Display the arrays
	    {
	        cout<<"\n\n Array 1 elements is:"<<array1[i]<<endl;
	        cout <<"\n Array 2 elements is:"<<array2[i]<<endl;
	    }

	    return 0;
	}


That what u were mean ? yea it works gd but well i dont know if its from the loop in the function or its doesnot matter
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
#include <iostream>
using namespace std;

	void swapArray(int array1[], int array2[], int i,int size);
	void swapArray(int array1[], int array2[], int i,int size )
	{
	    int temp;
	    if (i<size)
	    {temp = array1[i];               // Set temp to array1[i]
	    array1[i] = array2[i];          // Set array1[i] to array2[i]
	    array2[i] = temp;               // Set array2[i] to temp
        swapArray (array1,array2,i,size);
        }
	else
        cout<<"Not valid"<<endl;
	}

	int main()
	{

        const int size=5;
        int array1[size];
        int array2[size];
	    int element1=0,element2=0;

	    for (int i=0;i<size;i++)  // to take the 5 elements from the user
	    {
	        cout<<"please Enter Element of The first array"<<endl;
	        cin>>element1;
            cout<<"please Enter Element of The second array"<<endl;
            cin>>element2;
                 array1[i] = element1;      //then save them in the array to send them to the function and swap them
                 array2[i] = element2;
        swapArray(array1,array2,i,size);  //function call
                	    }

	    for(int i=0;i<size;i++)              // Display the arrays
	    {
	        cout<<"\n\n Array 1 elements is:"<<array1[i]<<endl;
	        cout <<"\n Array 2 elements is:"<<array2[i]<<endl;
	    }

	    return 0;
	}


well the same question when i write it in recursion its not work tell me that windows can find a solution on the internet
yea it works gd but well i dont know if its from the loop in the function or its doesnot matter


Have I mentioned yet that YOU CAN USE A DEBUGGER TO STEP THROUGH YOUR CODE AND SEE EXACTLY WHAT IT IS DOING FOR YOURSELF?
Topic archived. No new replies allowed.
Pages: 12