Ok I have to sort my 2D array, with user choice which column to sort and by ascending or descending. All of this is from a text file. This is my code so far:
#include <stdio.h>
#include <stdlib.h>
#define NLines 13
int main() {
FILE *readfile;
int FirstArray[NLines][7];
int i, j, k, l, n1, n2, n3, n4, n5, n6, n7, n8, n9;
char choice;
if ((readfile = fopen("cubs-batting-ws-more-names.txt", "r")) == NULL) {
printf("The file failed to open, sorry.");
}
printf("Statistics chart of some of the Cubs players from game 7 of the World Series.\n\n");
printf("Definition of symbols. player (1), games played (2), at-bats (3),\n");
printf("runs scored(4), hits collected (5), home runs hit (6), runs batted in (7),");
printf("batting average(8)and on-base-percentage-plus-slugging (9)\n\n");
do {
printf(" (1) (2) (3) (4) (5) (6) (7)\n");
printf("__________________________________________________________________\n");
for (i = 0; i < NLines; i++) {
for (j = 0; j < 7; j++) {
fscanf(readfile, "%d", &FirstArray[i][j]);
printf("%3d ", FirstArray[i][j]);
}
printf("\n");
}
printf("\nPlease enter a number (in the parenthesis) \n"
"for the column you would like to sort, or 0 to exit. ");
scanf("%d", &k);
printf("\nWould you like to sort in ascending or descending order? ");
scanf(" %c", &choice);
l = k - 1;
switch (choice) {
case 'a':
case 'b':
for (j = 0; j < 7; j++) {
for (i = 0; i < NLines; i++) {
if (FirstArray[i][l] >= FirstArray[i + 1][l]) {
n1 = FirstArray[i][0];
FirstArray[i][0] = FirstArray[i + 1][0];
FirstArray[i + 1][0] = n1;
n2 = FirstArray[i][1];
FirstArray[i][1] = FirstArray[i + 1][1];
FirstArray[i + 1][1] = n2;
n3 = FirstArray[i][2];
FirstArray[i][2] = FirstArray[i + 1][2];
FirstArray[i + 1][2] = n3;
n4 = FirstArray[i][3];
FirstArray[i][3] = FirstArray[i + 1][3];
FirstArray[i + 1][3] = n4;
n5 = FirstArray[i][4];
FirstArray[i][4] = FirstArray[i + 1][4];
FirstArray[i + 1][4] = n5;
n6 = FirstArray[i][5];
FirstArray[i][5] = FirstArray[i + 1][5];
FirstArray[i + 1][5] = n6;
n7 = FirstArray[i][6];
FirstArray[i][6] = FirstArray[i + 1][6];
FirstArray[i + 1][6] = n7;
n8 = FirstArray[i][7];
FirstArray[i][7] = FirstArray[i + 1][7];
FirstArray[i + 1][7] = n8;
}
}
}
break;
case 'D':
case 'd':
for (i = 0; i < NLines; i++) {
if (FirstArray[i][l] <= FirstArray[i + 1][l]) {
n1 = FirstArray[i][0];
FirstArray[i][0] = FirstArray[i + 1][0];
FirstArray[i + 1][0] = n1;
n2 = FirstArray[i][1];
FirstArray[i][1] = FirstArray[i + 1][1];
FirstArray[i + 1][1] = n2;
n3 = FirstArray[i][2];
FirstArray[i][2] = FirstArray[i + 1][2];
FirstArray[i + 1][2] = n3;
n4 = FirstArray[i][3];
FirstArray[i][3] = FirstArray[i + 1][3];
FirstArray[i + 1][3] = n4;
n5 = FirstArray[i][4];
FirstArray[i][4] = FirstArray[i + 1][4];
FirstArray[i + 1][4] = n5;
n6 = FirstArray[i][5];
FirstArray[i][5] = FirstArray[i + 1][5];
FirstArray[i + 1][5] = n6;
n7 = FirstArray[i][6];
FirstArray[i][6] = FirstArray[i + 1][6];
FirstArray[i + 1][6] = n7;
n8 = FirstArray[i][7];
FirstArray[i][7] = FirstArray[i + 1][7];
FirstArray[i + 1][7] = n8;
}
}
break;
default:
printf("invalid choice.\n");
}
}
while (k != 0);
if (fclose(readfile) == EOF) {
printf("The file failed to Close!! Sorry!!");
}
return 0;
}
when I run my code to sort column one it sorts the first column and first row fine, but everything else does not follow as it should. The file is players data, so all of the rows need to stay accordingly. When I try to sort anything else, it does not do it at all. I have tried everything I know to fix this and nothing is working. I would appreciate any and all help you can give me.
l, it looks too similar to1in the fixed width fonts used for coding.while (k != 0);?case 'b':should becase 'A':.