简体   繁体   中英

2 Dimensional Array sorting in c

Here is a segment of my (incomplete) code

int rows(int board[][9]){
    int badUnits = 0, i = 0, n = 9, j, z = 0;
    int (*temp)[9];

    //Sort each row of 2d array
    for (z; z < n; z++){
        for (i; i < n; i++){
            for (j = i; j < n; j++){
                if (board[z][i] > board[z][j]){
                    temp = board[z][i];
                    board[z][i] = board[z][j];
                    board[z][j] = temp;
                }
            }
        }
    }

    printf ("%d\n", temp[1][0]);
    printf ("%d\n", temp[1][1]);

    return badUnits;
}

The function takes a 9*9 array.

I get a segmentation fault when the print statements are executed. I believe my sort code is correct because it is similar to what I use for 1d arrays and I think everything else is correctly assigned.

So the culprit would be my temp variable. I have gone through and tried to assign values to it, tried to change the type, and have taken into account that the 2d array decays into a pointer but is not actually a pointer.

The conclusion I am left with is that this is a dynamic allocation issue. Can someone please lend a hand and assist me in fixing this? I have exhausted my knowledge base and am stuck.

To clarify: I decided to print the temp variable because I thought it would lend some information. The main problem was that the swap was not working, and I was still left with an unsorted array when I originally attempted to print out the board[][]. I know that board is what I am SUPPOSED to be printing.

Thank you for any help!

You assign an int value to temp

 temp = board[z][i]; // Temp now is a what ever value was at 
                     // That location in the array e.g. 42

You then treat temp as if it was the address in memory of an integer array

 temp[1][1] // treat temp as a pointer and find the integer 
            // 10 integers further along then temp.

Also sometime temp will not have been initialised (never assigned to) in this case your going to get unexpected behaviour depending on what the last value stored where temp is now (Lets call it a random number).

Did you mean to output the values in board?

printf ("%d\n", board[1][0]);
printf ("%d\n", board[1][1]);

One thing to notice is that the variable temp will only get assigned to if a swap occurs, if the sorting algorithm was correct that is still a situation that could occur with a input corresponding to a sorted board.

But more importantly, the variable temp is used as an int during the swap. Later that integer value is interpreted as a pointer in the expressions temp[1][0] and temp[1][1] , which in all likelihoods is not a valid address. You may want to change temp to be declared as:

int temp;

And figure out exactly what you would like to print. If it is whatever one of the two swapped values was (for the last swapped pair in the loop), then you would print it as:

printf("%d", temp);

Else, you would have to add logic according to what you really want to do.

Note that a single pass of this algorithm would not perform a complete sort, but I guess that's one of the reason why you said the provided code was not complete.

Something like this?

#include <stdio.h>
#include <stdlib.h>


void printArray(int** arr, int w, int h) {
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j) {
      printf("%d ", arr[i][j]);
    }

    printf("\n");
  }
}

void sortArray(int** arr, int w, int h) {
  for (int row = 0; row < h; ++row) {
    for (int col = 0; col < w; ++col) {

      for (int i = 0; i < w; ++i) {
        if (arr[row][i] > arr[row][col]) {
          int tmp = arr[row][col];

          arr[row][col] = arr[row][i];
          arr[row][i] = tmp;
        }
      }
    }
  }
}

int main() {
  int w = 9, h = 9;
  int** arr = (int**)malloc(sizeof(int*) * w);
  for (int i = 0; i < w; ++i) {
    arr[i] = (int*)malloc(sizeof(int) * h);

    for (int j = 0; j < h; ++j) {
      arr[i][j] = rand() % 10;
    }
  }

  printf("Unsorted:\n");
  printArray(arr, w, h);

  sortArray(arr, w, h);

  printf("Sorted:\n");
  printArray(arr, w, h);

  for (int j = 0; j < h; ++j) {
    free(arr[j]);
  }
  free(arr);

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM