1

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.

I am attaching a screenshot. Text File output

4
  • Avoid naming a variable l, it looks too similar to 1 in the fixed width fonts used for coding. Commented Dec 11, 2016 at 22:24
  • What is the purpose of the infinite loop while (k != 0);? Commented Dec 11, 2016 at 22:24
  • case 'b': should be case 'A':. Commented Dec 11, 2016 at 22:26
  • You do not sort the array in the descending case, because you have a single loop instead of the nested loops as for the ascending case. Commented Dec 11, 2016 at 22:26

1 Answer 1

1

In your for (i loop, the condition must be i<NLines-1 as you compare element [i] with [i+1].

Currently, looking at your variable declaration (and assuming Intel architecture), FirstArray[i+1] for i==Nlines-1 will use the values of your int i, j, k, l,... as the values, which may even be overwritten when you swap. Then i isn't i anymore and anything can happen, also called Undefined Behaviour.

Suggestion: why not use a loop to swap the elements? Much simpler, cleaner and easier to read and maintain.

Suggestion: you don't need to swap if they are equal. Make the condition just >.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok i did what you said and it worked perfectly. I really appreciate your help. Thank you very much. I don't know how I would have gotten this done without you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.