1

I need to sort the columns of my 2D array based on the first row of the array. This is my 2d array before sorting:

cargo

AFADA
DAFGF
DXAFA
DDFFX
GFXF 

and this is the image of my 2D array:

acrgo

FAADA
ADFGF
XDAFA
DDFFX
FGXF

The correct output looks like this

acgor

FADAA
ADGFF
XDFAA
DDFXF
FGF X

. This is the code of my function to sort the array using insertion sort algorithm

void sort(char** newTable, int row, int col) {
    for (int top = 1; top < col; col++) {
        char item = newTable[0][top];
        int i = top;
        while (i > 0 && item < newTable[0][i - 1]) {
            for (int j = 0; j < row + 1; j++) {
                char temp = newTable[j][i];
                newTable[j][i] = newTable[j][i - 1];
                newTable[j][i - 1] = temp;
            }
            i--;
         }
         newTable[0][i] = item;
     }
 }

I called the function like this

    sort(newTable, row, strlen(key));

here, key is the string 'cargo'

The definition of newTable:

char** newTable = (char**)calloc(row + 1, sizeof(char*));
    for (int i = 0; i < row + 1; i++) {
        newTable[i] = (char*)calloc(strlen(key), sizeof(char));
    }
13
  • I am trying to sort the first row alphabetically and shift the columns below it accordingly Commented Jun 10, 2020 at 12:49
  • If the spacing in the last line is to be ignored, it is better to say that in the post, perhaps just to the right of the text of that last line //ignore space or just don't show the space in your data: FGFX. Commented Jun 10, 2020 at 13:05
  • Yeah, i fixed that Commented Jun 10, 2020 at 13:09
  • Can you also include a short snippet showing how you call this function, i.e. show the input, and how you read the results. By the way, to get results you will need to pass the address of the 2D array to allow changes made in the function to be retrieved, requiring char** newTable to become char ***newTable in the prototype. Then called as (for example) sort(&some2DArray, 6,5); Commented Jun 10, 2020 at 13:11
  • i added the snippet on how i called the function. I think that there is some error in my implementation of the sorting algorithm cos it is sorting it partially correct Commented Jun 10, 2020 at 13:21

1 Answer 1

1

newTable is indeed an array of pointers and not a 2D array, so using char **newTable makes sense.

The major error is close to a typo: in the first for loop of sort, you increment col when you want to increment top. And the last line newTable[0][i] = item; is useless.

This should work (even if the loop for (int j = 0; j < row + 1; j++) suggests that row is not the number of rows in newTable but only the number of *additional rows):

void sort(char** newTable, int row, int col) {
    for (int top = 1; top < col; top++) {
        char item = newTable[0][top];
        int i = top;
        while (i > 0 && item < newTable[0][i - 1]) {
            for (int j = 0; j < row + 1; j++) {
                char temp = newTable[j][i];
                newTable[j][i] = newTable[j][i - 1];
                newTable[j][i - 1] = temp;
            }
            i--;
         }
     }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Will this actually allow caller to see the changes made in the array? Doesn't caller have to pass the address of, therefore requring prototype to use char ***array ?
@ryyker: You have to pass a pointer if you want to build a brand new array in the function. Here you just change elements inside the really same array, so you do not need an additional indirection level.

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.