Q- Use swapping concept to swap the elements of 1D array consist of 10 integers. Ask the user to enetr the values of array elements then print them out to the screen before and after swapping.

e.g. a[0] ----> a[9]
a[1] ----> a[8]
a[2] ----> a[7]
a[3] ----> a[6]
a[4] ----> a[5]


what i did is ..

#include<iostream.h>
void main()
{
int a[10],temp;
for(int i=0;i<5;i++)
cin>>a[i];
for(int j=9;j>5;j--)
cin>>a[j];
for(int i=0,j=9;i<5,j>5;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(i=0;i<5;i++)
cout<<a[i]<<"\t";
cout<<"\n";
for(j=9;j>5;j--)
cout<<a[j];
}

* I think there's something goes wrong or missing with this code ... umm if u just put me on the right direction, i'd really appreciate it.

line 8 should be cout, not cin.

*scratch the head* .. i'm lost now .. coz i just finished compiling and runnng it ...it didn't give me the required result ..ha?

You have lines 5 thru 8 all screwed up. You only need one loop to get the values, the first loop should count from 0 to 10. Then the second loop should be identical to lines 15 and 16.

seems like a tough program ... i tried several times but pointless

ok .. the second attempt is..

#include<iostream.h>
void main()
{
int a[10],j,temp;
for(int i=0;i<10;i++)
ci>>a[i];
for(i=0;i<10;i++)
cout<<"a[i]:"<<a[i]<<endl;
for(i=0,j=9;i<5,j>4;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(i=0;i<10;i++)
cout<<a[i]<<"\t";
cout<<endl;
for(j=9;j>4;j--)
cout<<a[j]<<"\t";
}

waiting your coments ..

delete lines 18 and 19 because they are redundent. After doing that I compiled and ran your program and it worked for me.

1
2
3
4
5
6
7
8
9
10

a[i]: 1
a[i]: 2
a[i]: 3
a[i]: 4
a[i]: 5
a[i]: 6
a[i]: 7
a[i]: 8
a[i]: 9
a[i]: 10
10      9       8       7       6       5       4       3       2       1

Press any key to continue . . .

Thnx a lot ...u guys on this site are awesome :)

The code is working and the output was as u mentioned but my question is:

can i get an output like this..

a[0]:1 isnstead of a:1 ?

>>cout<<"a:"<<a<<endl;

change it to cout << "a[" << i << "]:" << a[i] << endl;

Thnx...u have been very helpful :)

This question has already been answered. Start a new discussion instead.