#include <iostream>
usingnamespace 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?
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?
#include <iostream>
usingnamespace 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 ?
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
#include <iostream>
usingnamespace 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
#include <iostream>
usingnamespace 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;
}
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.
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 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?
#include <iostream>
usingnamespace 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;
}