Thread: 2D Array sorting

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    2D Array sorting

    I'm attempting to sort a 2D array in a pa rticular way but I'm stuck. The details are:

    Part 1:
    Write a function that fills up a square board, randomly, with an integer 0: : : 9. Use a:
    #define N 20
    to define the size of the board. Write another function to print the board. The board may look
    something like:


    36753562912709360626
    18792023759228973612
    93194784503610632061
    55476569374525474430
    78688431492068926649
    50487172722610615949
    09177115977673656394
    81293908850963856115
    98481030444476317596
    21785741859753883189
    64333860488897764303
    09254059469224775481
    28936802105110850646
    25862847240629908131
    10340391969338056640
    04626756987282996027
    61321599149107587048
    04296104222055290283
    80409196254499360502
    94351743146942264128


    Write a function to ‘mutate’ the board. Mutating is done like this:
    • Choose two random locations which are horizontally adjacent (next to each other leftright).
    • Swap these two numbers on the board if the left one is greater than the right one, numerically.
    • Choose two random locations which are vertically adjacent (next to each other up-down).
    • Swap these two numbers on the board if the upper one is greater than the lower one,
    numerically.
    • Repeat the above steps (N*N*N) times.


    Now print out the board. It should look something like :


    00000000000001111224
    00000001111111233456
    00000111112222244456
    00001122222333445666
    00112222223333555667
    01112223333334556678
    01112223334445556779
    01122333344445556789
    01223334444455666789
    01223344445556666789
    01223344455666667789
    01224444456666777889
    01234455566677777889
    01234555666677788899
    01234555666778888899
    12234566677788888999
    12345567777888889999
    12445677788888899999
    34446678889999999999
    46678899999999999999

    Ensure that if you change the size of your array, by changing your #define that the program
    still operates correctly.


    Part 2:
    Adapt the code above, so that the algorithm is:
    • Choose two numbers at random locations on the board.
    • Check that of these two numbers, the one closest to the centre of the array is numerically
    less than the number furthest away from the centre. If not, swap them.
    • Repeat the above steps (N*N*N*N) times.
    Once again randomise the array initially, and ensure that after changing your #define the
    program still works correctly.
    When N = 21, the array may look something like:


    999998887777788899999
    999987666656666788999
    998876554444555678899
    998665443333444566889
    987654333222233456789
    876543322112223345678
    865443211111112334568
    765432111000011234568
    765422100000001234567
    764321100000001223467
    764321100000001123457
    764322100000001223467
    765422100000001224567
    865432110000011234568
    865432211111122334568
    876543322212223345678
    987654333222233456789
    988665444333344567889
    998876555444455678899
    999887666656666789999
    999998887777788899999




    Now for my code, I am still stuck on part 1 as I can't figure out the way to successfully manipilate the board and select a pair of horizontal or vertical locations.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    
    
    #define N 20
    
    
    void arrayPrint(int a[][N]);
    void arraySort(int a[][N]);
    
    
    int main(void)
    {
       int board[N][N] = {
          0
       };
       int i, j;
    
    
       srand(time(NULL));
    
    
       for (i = 0; i < N; i++) {
          for (j = 0; j < N; j++) {
             board[i][j] = rand() % 10;
          }
       }
    
    
       printf("Unsorted:\n");
       arrayPrint(board);
       printf("\nSorted:\n");
       arraySort(board);
    
    
       return 0;
    }
    
    
    void arraySort(int a[][N])
    {
       int i, j, k;
    
    
       for (k = 0; k < N; k++) {
          for (i = 0; i < N; i++) {
             for (j = i + 1; j < N; ++j) {
                if (a[k][i] > a[k][j]) {
                   int swap = a[k][i];
                   a[k][i] = a[k][j];
                   a[k][j] = swap;
                }
             }
          }
       }
       arrayPrint(a);
    }
    
    
    void arrayPrint(int a[][N])
    {
       int i, j;
       for (i = 0; i < N; i++) {
          for (j = 0; j < N; j++) {
             printf("%d", a[i][j]);
          }
          printf("\n");
       }
    }
    any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    For the horizontal swaps:
    Select a random row from 0 to N - 1.
    Select a random col from 0 to N - 2.
    If a[row][col] > a[row][col + 1], swap them.

    For the vertical swaps:
    Select a random row from 0 to N - 2.
    Select a random col from 0 to N - 1.
    If a[row][col] > a[row + 1][col], swap them.

    Do the above N * N * N times.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-04-2014, 09:51 AM
  2. Replies: 2
    Last Post: 04-18-2013, 02:21 PM
  3. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  4. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  5. C Sorting Array
    By nixonbw in forum C Programming
    Replies: 18
    Last Post: 02-28-2008, 07:17 AM

Tags for this Thread