can you pls help me find a way to sort two dimensional arrays with a size of [5][2] integer...i want to sort it from highest to lowest... can you pls help me with the codes...i only know how to sort 1 dimensional array using bubble sort... Code: printf("\nThe numbers from highest to lowest are: "); for(x=0;x<=8;x++) for(y=x+1;y<=9;y++) if(num[x]>num[y]){even = num[x]; num[x]=num[y]; num[y]=even; } for(x=0;x<=9;X++) printf("\n%d",num[x]); how about with two dimensional arrays?????
It looks like you have not told us your exact requirements and in the context of your problem the best solution could be to convert the 2 dimensional array into a one dimension array of 5X2 elements and then sort it.
You can actually just run through the 2D array and make your comparisons and swap when necessary. For instance, you may compare Array [3][1] with Array [4][6] and decide whether to swap them or not. This approach merely requires a loop for each dimension. For example, Code: for (i = 0; i < rowMax; i++) { for (j = 0; j < colMax; j++) { // Put your sort stuff here, which will require two more loops ... This approach will allow you to sort a 2D array which is not an actual 2D array, such as one where each row is gotten independently from malloc or new. All you have to do is get the indexing or pointing right in the loops.
Code: #include<stdio.h> int num[5][2]; int x,y,a,b,low; main() { clrscr(); for(x=0;x<=4;x++) for(y=0;y<=1;y++){ printf("Enter number: "); scanf("%d",&num[x][y]); } printf("\nThe number from highest to lowest are: "); { for(x=0;x<=4;x++) for(y=0;y<=1;y++) { for(a=x+1;a<=4;a++) for(b=y+1;b<=1;b++) { if(num[x][y]>=num[a][b]){low=num[x][y]; num[x][y]=num[a][b]; num[a][b]=low; } } } for(x=0;x<=4;x++) for(y=0;y<=1;y++){ printf("%d",num[x][y]); } getch(); }
Code: #include<stdio.h> int num[5][2]; int x,y,a,b,low; main() { clrscr(); for(x=0;x<=4;x++) for(y=0;y<=1;y++){ printf("Enter number: "); scanf("%d",&num[x][y]); } printf("\nThe number from highest to lowest are: "); { for(x=0;x<=4;x++) for(y=0;y<=1;y++) { for(a=0;a<=4;a++) for(b=0;b<=1;b++) { if(num[x][y]>=num[a][b]){low=num[x][y]; num[x][y]=num[a][b]; num[a][b]=low; } } } for(x=0;x<=4;x++) for(y=0;y<=1;y++){ printf("%d",num[x][y]); } getch(); } the first number inputed and the last number inputed is not sorting.... or could you give me other ways to sort this 10 numbers......pls help me