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!
#include <iostream>
usingnamespace 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;
}
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
#include <iostream>
usingnamespace 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 !
#include <iostream>
usingnamespace 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!
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.
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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.
#include <iostream>
usingnamespace 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()
{
constint 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.
#include <iostream>
usingnamespace 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()
{
constint 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
#include <iostream>
usingnamespace 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()
{
constint 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