patterncMinor
Programming of 3 x 7 trick
Viewed 0 times
programmingtrickstackoverflow
Problem
The puzzle is:
You can use any programming language to solve this.
I'd like feedback on this.
```
#include "stdafx.h"
#include
#include
#include
#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 ti
- 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
#include
#include
#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 ti
Solution
Your
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)
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 > 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;
}
}Code Snippets
void random_rearrange_num(int p[][numCols])
{
int r, k, l, temp;
for (int i = numRows * numCols - 1; i > 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;
}
}Context
StackExchange Code Review Q#9419, answer score: 3
Revisions (0)
No revisions yet.