urgent help with sorting a two D array
Hi all I need to get in an assignment to night, and I cant get a 2 D array to sort correctly. The arry has 5 columbs and 10000 rows. The columbs are Postcode, Month, Year, Bedrooms and Price. I have to sorted in ascending order according by price, then if 2 prices are the same then year, then if 2 prices and years are the same then month, and the same for then postcode and last bedrooms.
This is how the data is when it is put in,
| Postcode Month Year Bedrooms Price |
+----------------------------------------+
| 2081 12 2007 4 795500 |
| 2180 07 2009 6 165000 |
| 2140 12 2008 5 689000 |
| 2153 03 2008 4 491500 |
| 2140 02 2010 5 886500 |
| 2005 11 2007 4 776500 |
| 2151 07 2007 5 305500 |
| 2199 12 2010 5 895000 |
| 2101 04 2010 7 389500 |
| 2176 02 2008 2 959500 |
and this is what it is like afte the sort, it seem that bedrooms and postcodes do not move with the others.
+========================================+
| Postcode Month Year Bedrooms Price |
+----------------------------------------+
| 2151 07 2009 5 165000 |
| 2180 07 2007 6 305500 |
| 2101 04 2010 7 389500 |
| 2153 03 2008 4 491500 |
| 2081 12 2008 4 689000 |
| 2005 11 2007 4 776500 |
| 2140 12 2007 5 795500 |
| 2140 02 2010 5 886500 |
| 2199 12 2010 5 895000 |
| 2176 02 2008 2 959500 |
What I believe to the relevent bits of code are below
Code:
#include <stdio.h>
#define ROWS 10000
#define COLUMNS 5
void printHeader (void);
void printTable (int realestate[ROWS][COLUMNS], int i, int j);
void printStage (int stage);
void swap(int realestate[ROWS][COLUMNS] , int i, int k);
//int improve_order(int[][COLUMNS], int);
int main(int argc, char *argv[]) {
int tallyCount[10000]={0};
int realEstate[ROWS][COLUMNS];
int postSum[10000]={0};
int avgPostSum[10000]={0};
int yearCount[3000][2]= {{0}};
int median[200]={0};
int swapped1=1;
int yearSum=0;
int i, j, k, l;
int num=1;
int numberrow;
char c=0;
//Ignores the headers//
while(c != '\n') {
scanf("%c", &c);
}
for(i=0; (i<ROWS && num==1); i=i+1) {
for(j=0; j<COLUMNS; j=j+1) {
num=scanf("%8d", &realEstate[i][j]);
}
}
numberrow=i-1;
for(i=0; i<numberrow; i=i+1) {
for(l=1000;l<9999;l=l+1) {
if(realEstate[i][0]==l) {
tallyCount[l]=tallyCount[l]+1;
postSum[l]=(postSum[l]+realEstate[i][4]);
}
}
}
while (swapped1==1){
swapped1=0;
for(i=0; i < numberrow-1; i++){
if(realEstate[i][4]>realEstate[i+1][4]) {
swap(realEstate, i, 4);
swap(realEstate, i, 3);
swap(realEstate, i, 2);
swap(realEstate, i, 1);
swap(realEstate, i, 0);
swapped1=1;
}
else if ( (realEstate[i][4]==realEstate[i+1][4]) && (realEstate[i][2] > realEstate[i+1][2]) ) {
swap(realEstate, i, 3);
swap(realEstate, i, 2);
swap(realEstate, i, 1);
swap(realEstate, i, 0);
swapped1=1;
}
else if ( (realEstate[i][2]==realEstate[i+1][2]) && (realEstate[i][1] > realEstate[i+1][1]) ) {
swap(realEstate, i, 3);
swap(realEstate, i, 1);
swap(realEstate, i, 0);
swapped1=1;
}
else if ( (realEstate[i][1]==realEstate[i+1][1]) && (realEstate[i][0] > realEstate[i+1][0]) ) {
swap(realEstate, i, 3);
swap(realEstate, i, 0);
swapped1=1;
}
else if ( (realEstate[i][0]==realEstate[i+1][0]) && (realEstate[i][3] > realEstate[i+1][3]) ) {
swap(realEstate, i, 3);
swapped1=1;
}
}
}
swapped1=0;
printStage(4);
printf("Sales sorted in ascending order according to price, etc.:\n");
printHeader();
for (i=0; i < numberrow; i=i+1) {
for (j=0; j < COLUMNS; j=j+1) {
printTable(realEstate, i, j);
}
}
return 0;
}
oid printHeader (void) {
printf("+========================================+\n");
printf("| Postcode Month Year Bedrooms Price |\n");
printf("+----------------------------------------+\n");
}
void printTable (int realEstate[ROWS][COLUMNS], int i, int j) {
if (j==0) {
printf("| %4d", realEstate[i][j]);
} else if (j==1) {
printf(" %02d", realEstate[i][j]);
} else if (j==2) {
printf(" %4d", realEstate[i][j]);
} else if (j==3) {
printf(" %4d ", realEstate[i][j]);
} else if (j==4) {
printf(" %8d |\n", realEstate[i][j]);
}
}
void swap(int realEstate[ROWS][COLUMNS] , int i, int k) {
int temp;
temp = realEstate[i][k];
realEstate[i][k] = realEstate[i+1][k];
realEstate[i+1][k] = temp;
}