Need Help w/C++ Array Exchange Maximum Sort...

Status
Not open for further replies.

See Plus Plus

Baseband Member
Messages
63
Hi everyone,

Can anyone please please please help me with this:
I need to write a source code for this - I only know how to do it for a single array (list?), but this one asks for a table. I know I have to use the exchange sort code with for loops but I can't figure it out beyond a list.

Here goes:
A 2-D array has these 20 elements:
3 33 333 3333
5 55 555 5555
1 11 111 1111
4 44 444 4444
2 22 222 2222

Write an exchange maximum program to:
a) Sort the array so that it will look like this:

5 55 555 5555
4 44 444 4444
3 33 333 3333
2 22 222 2222
1 11 111 1111

b) Sort the array so that it will look like this:

1111 111 11 1
2222 222 22 2
3333 333 33 3
4444 444 44 4
5555 555 55 5

If anyone can please help me out I would really appreciate it.
Thank you
 
See Plus Plus,

Okie, for the 1 st one ,code is below. for the 2nd one, just change the a[k]>a[j][k] to a[k]<a[j][k] ....

EDIT: convert it to c++, ive written in C.

int main(void)
{
int b[5][4],i,j,temp[5][4],k;
int a[5][4]={3,33,333,3333,5,55,555,5555,1,11,111,1111,4,44,444,4444,2,22,222,2222};

for(k=0;k<4;k++)
{
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[k]>a[j][k])
{
temp[k]=a[k];
a[k]=a[j][k];
a[j][k]=temp[k];
}
}
}
}

printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
printf("%d",a[j]);
printf(" ");
}
printf("\n");

}
}
 
Exchange sort analyses every element sequentially and compares it with the next. The element is exchanged if its smaller/greater.
Eg 4,7,2,9
first cycle, first time
4 with 7
Its smaller, therefore exchange it.
so 7,4,2,9
now we compare 4 and 2.
so 7,4,2,9
Now 2 and 9
so 7,4,9,2
In the next cycle we will compare only till the second last element.
The thing to be noted is that after each cycle the smallest item will become the last.

7,4,9,2
7,9,4,2

9,7,4,2

Therefore the array is sorted.
There r many other types of sorting techniques.
 
Thank you sooo much for all your help. The program worked once I changed it all to C++, however, the other program (part two) didn't come out like this:

1111 111 11 1
2222 222 22 2
3333 333 33 3
4444 444 44 4
5555 555 55 5

Am I doing something wrong? I changed > and < around, but it is coming out like this:

1 11 111 1111
2 22 222 2222
3 33 333 3333
4 44 444 4444
5 55 555 5555
 
See Plus Plus,

Ah, sorry . Just change the way you print the result. That will get you the o/p you want. Any. here is the changed program for the second one..

#include <stdio.h>
int main(void)
{
int b[5][4],i,j,temp[5][4],k;
int a[5][4]={3,33,333,3333,5,55,555,5555,1,11,111,1111
,4,44,444,4444,2,22,222,2222};

for(k=0;k<4;k++)
{
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(a[k]>a[j][k])
{
temp[k]=a[k];
a[k]=a[j][k];
a[j][k]=temp[k];
}
}
}
}

printf("\n");
for(i=4;i>=0;i--)
{
for(j=3;j>=0;j--)
{
printf("%d",a[j]);
printf(" ");
}
printf("\n");

}
}
 
Oh thanks, I though that maybe I had messed something up, haha.

How long does it take to learn all this stuff and know it like the back of your hand?
 
See Plus Plus,

There is nothing to learn. You can learn all the languages easily . Java in a month, C++ in a month.... but what matters is practise . You should do a lot of coding ,fun programs ..etc to set your mind to program logically. Once you ve done it with one program, learning other programs are always easy. So, finally it comes down to practise , practise, and practise
 
Status
Not open for further replies.
Back
Top Bottom