Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Need Help w/C++ Array Exchange Maximum Sort...
Closed Thread
Old 11-29-2004, 01:41 PM   #1 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 63

See Plus Plus

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

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 is offline  
Old 12-02-2004, 09:23 AM   #2 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 63

See Plus Plus

Default

Anyone? Please?

Is there any site that anyone knows of that can explain exchange sort to me?
See Plus Plus is offline  
Old 12-02-2004, 09:30 AM   #3 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

See Plus Plus,

Id like to have a look....but spare me a day..i dont have time now..
intercodes is offline  
Old 12-02-2004, 09:54 AM   #4 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 63

See Plus Plus

Default

I thank you SO SO much.
__________________
I hate Pi
See Plus Plus is offline  
Old 12-04-2004, 03:14 PM   #5 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

See Plus Plus,

Okie, for the 1 st one ,code is below. for the 2nd one, just change the a[i][k]>a[j][k] to a[i][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,4 44,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[i][k]>a[j][k])
{
temp[i][k]=a[i][k];
a[i][k]=a[j][k];
a[j][k]=temp[i][k];
}
}
}
}

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

}
}
intercodes is offline  
Old 12-05-2004, 11:47 AM   #6 (permalink)
 
Junior Techie

Join Date: Dec 2004

Posts: 88

developer

Default

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.
__________________
Somewhere I Belong...
developer is offline  
Old 12-08-2004, 09:06 AM   #7 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 63

See Plus Plus

Default

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
__________________
I hate Pi
See Plus Plus is offline  
Old 12-08-2004, 09:42 AM   #8 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

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[i][k]>a[j][k])
{
temp[i][k]=a[i][k];
a[i][k]=a[j][k];
a[j][k]=temp[i][k];
}
}
}
}

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

}
}
intercodes is offline  
Old 12-08-2004, 03:00 PM   #9 (permalink)
 
Junior Techie

Join Date: Oct 2004

Posts: 63

See Plus Plus

Default

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?
__________________
I hate Pi
See Plus Plus is offline  
Old 12-09-2004, 12:07 AM   #10 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

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
intercodes is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On