Programming of 3 x 7 trick The puzzle is:


  
  The deck contains 21 unique numbers
  The numbers should be laid up in rows of 7 (3 rows in total)
  ask the player to memorize a number to start the game
  once the game starts, rearrange the numbers in random in all rows.
  ask the player to select the row the number is in.
  once the player selects, rearrange the numbers for a second time
  
  this time, it is not random but sort the numbers in bundles per row
  stack all the bundles on top of each other, keeping the row selected to be in the centre.
  rearrange it back into rows of 7 such that first number in stack goes to row 1, second to row 2, third to row 3, 4th to row 1 again and so on.
  
  again ask the player to select the row the number is in.
  repeat step 6
  again ask the player to select the row the number is in.
  point to the middle number of the row selected as the answer.
  
  
  You can use any programming language to solve this.


I'd like feedback on this.

#include "stdafx.h"
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#define numRows 3
#define numCols 7
#define TotalNum (numRows*numCols)

void random_rearrange_num(int p[][numCols]) ;
void display(int p[][numCols]) ;
void start();
int row_select(int count);
void stackup(int p[][numCols],int *s,int count);
void back_to_array(int p[][numCols],int *s);


int main()
{  
    //for random numbers
    srand(time(NULL)); 

    //Step 1
    int arr[numRows][numCols] = {{0,1,2,3,4,5,6},{7,8,9,10,11,12,13},{14,15,16,17,18,19,20}};
    int stack[TotalNum] = {0};
    int row = 0;

    //Step 2
    display(arr);

    //Step 3
    start();    

    //step 4
    random_rearrange_num(arr);

    //Step 5 (first time)
    display(arr);
    row = row_select(row);

    //Step 6
    stackup(arr,stack,row);
    back_to_array(arr,stack);

    //Step 7 (second time)
    display(arr);
    row = row_select(row);

    //Step 8:repeat step 6
    stackup(arr,stack,row);
    back_to_array(arr,stack);

    //Step 9 (third time)
    display(arr);
    row = row_select(row);

    //Step 10 and Step 11
    printf("The answer is : %i",arr[row][3]); 

    return 0;
}
void random_rearrange_num(int p[][numCols])
{
  int temp = 0,k= 0,l = 0;
  for(int i = numRows-1 ; i &gt; 0 ; i--)
  {
    for (int j = numCols-1;j&gt;0; j--)
    {
          k = 0 + rand()/(RAND_MAX/(2-0+1)+1);
          l= 0 + rand()/(RAND_MAX/(6-0+1)+1);
          temp = p[i][j]; 
          p[i][j] = p[k][l];
          p[k][l] = temp;
    }
  }
}

void display(int p[][numCols])
{
       //printf("\n");
       for (int i = 0; i&lt; numRows;i++)
       {
          for ( int j = 0;j&lt; numCols;j++)
          {
            printf("%8i",p[i][j]);
          }
          printf("\n");
       }
}

void start()
{
        printf("\n");
        printf("Please memorize a number(between 0 to 20) and press enter to continue");
        char c=getchar();
        while (c != '\n')
            c=getchar(); 
        printf("\n"); 
}
int row_select(int count)
{
     printf("\n");
     printf("Please select the row the number is in(between 1 to 3):");
     scanf_s("%i",&amp;count);
     return count-1;
}


void stackup(int p[][numCols],int *s,int count)
 {         
    int k = 0,t= 0;  
    int middle = (numRows-1)/2;  
    for (int i = 0; i&lt;numRows; i++)
    {        
        t=i;      
        if (i==middle || i==count)  
            i = (i==count)?middle:count;  
        for (int j = 0; j&lt;numCols; j++)    
        {
            s[k++] = p[i][j];
        }
        i=t;    
    } 
} 
void back_to_array(int p[][numCols],int *s)
{
  int k = 0;
  for (int i = 0; i&lt; numCols;i++)
       {
          for ( int j = 0;j&lt;numRows;j++)
          {
             p[j][i] = s[k++];
          }

       }
}
Your random_rearrange_num function has a bias.  If you want an equal chance of getting every permutation, implement something like the Fisher-Yates shuffle.  Your implementation is actually a common implementation error.  You can read about it here.

I would implement it as follows: (my C is mighty rusty and I don't have a compiler on hand to verify syntax or correctness)

void random_rearrange_num(int p[][numCols])
{
  int r, k, l, temp;
  for (int i = numRows * numCols - 1; i &gt; 1; i--)
  {
    r = rand() % i; //select from only the remaining numbers
    k = r / numRows;
    l = r % numRows;
    temp = p[i][j]; 
    p[i][j] = p[k][l];
    p[k][l] = temp;
  }
}