swapping elements
Jul 31, 2014 at 6:19pm UTC
Hi,I'm a beginner and I am trying to swap 2 elements, but when it swaps, it swaps the wrong one and always the same one.This is what I have
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#include <iostream>
using namespace std;
int main()
{
int nu;
int num [7];
int arra[6];
int tr;
cout <<"enter the numbers in your array (has have 6(six)numbers)" <<endl;
for (int t=0;t<6; t++)
{
cout<<t<<":" ;
cin>>arra[t];
}
cout<<"in:" ;
for (int t=0;t<6; t++)
{
cout<<arra[t]<<", " ;
}
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"what element do you want to swap" ;
cin >>tr;
cout<<"out:" ;
if (tr=arra[1])
{
num[0]=arra[1];
num[1]=arra[0];
num[2]=arra[2];
num[3]=arra[3];
num[4]=arra[4];
num[5]=arra[5];
}
else if (tr=arra[2])
{
num[0]=arra[0];
num[1]=arra[2];
num[2]=arra[1];
num[3]=arra[3];
num[4]=arra[4];
num[5]=arra[5];
}
else if (tr=arra[3])
{
num[0]=arra[0];
num[1]=arra[1];
num[2]=arra[3];
num[3]=arra[2];
num[4]=arra[4];
num[5]=arra[5];
}
else if (tr=arra[4])
{
num[0]=arra[0];
num[1]=arra[1];
num[2]=arra[2];
num[3]=arra[4];
num[4]=arra[3];
num[5]=arra[5];
}
else if (tr=arra[5])
{
num[0]=arra[0];
num[1]=arra[1];
num[2]=arra[2];
num[3]=arra[3];
num[4]=arra[5];
num[5]=arra[4];
}
else if (tr=arra[0])
{
num[0]=arra[5];
num[1]=arra[1];
num[2]=arra[2];
num[3]=arra[3];
num[4]=arra[4];
num[5]=arra[0];
};
for (int o=0; o<6;o++)
{
cout<<num[o]<<", " ;
}
return 0;
}
Thanks!!!
Jul 31, 2014 at 6:38pm UTC
@Dexter 3202
The main thing I see wrong, is you are not checking an array value, you are assigning. A single equal sign, will assign, whereas the double equal sign, means, 'if value IS EQUAL TO'.
So, code like this
should be, like this..
Last edited on Jul 31, 2014 at 6:50pm UTC
Jul 31, 2014 at 7:02pm UTC
Thanks!, it worked
Last edited on Jul 31, 2014 at 7:02pm UTC
Topic archived. No new replies allowed.