Skip to content

Instantly share code, notes, and snippets.

@byyam
Created October 24, 2019 14:59
Show Gist options
  • Save byyam/3ae1326392edd97e3e25ce050b3e09f7 to your computer and use it in GitHub Desktop.
Save byyam/3ae1326392edd97e3e25ce050b3e09f7 to your computer and use it in GitHub Desktop.
loop
#include <stdio.h>
#define ROW 5
#define COLUMN (ROW + 1)
static unsigned ix;
static unsigned iy;
static unsigned ctr = 1;
static unsigned sta = 0;
static int arr[ROW][COLUMN];
static int *ptr_arr[ROW];
static int len[ROW];
void shuffle(int row) {
unsigned num = 1;
for (iy = 0; iy < row; iy++) {
num = num*len[iy];
}
if (sta % num == 0) {
ptr_arr[row] ++;
if (*ptr_arr[row] == 0)
ptr_arr[row] = &arr[row][0];
}
}
void print_out() {
sta ++ ;
printf("[%d/%d]\t", sta, ctr);
for (ix = 0; ix < ROW; ix++) {
printf("%d\t", *ptr_arr[ix]);
}
printf("\n");
for (ix = 0; ix < ROW; ix++) {
shuffle(ix);
}
}
int main(int argc, char const *argv[]) {
/*initialize array, each line end with 0.*/
printf("\ninitialize a int type array.\n");
for (ix = 0; ix < ROW; ix ++) {
for (iy = 0; iy < COLUMN; iy ++) {
arr[ix][iy] = iy + ix*10 + 1;
}
arr[ix][COLUMN - 1] = 0;
}
for (ix = 0; ix < ROW; ix ++) {
for (iy = 0; iy < COLUMN; iy ++)
printf("%d\t", arr[ix][iy]);
printf("\n");
}
printf("\ninitialize a pointer array to point the first element of each row.\n");
for (ix = 0; ix < ROW; ix ++) {
ptr_arr[ix] = &arr[ix][0];
printf("ptr_arr[%d] -> %d\n", ix, *ptr_arr[ix]);
}
/*caculate the loop times.*/
for (ix = 0; ix < ROW; ix++) {
ctr = ctr*(COLUMN - 1);
}
/*get the length of each row.*/
for (ix = 0; ix < ROW; ix++) {
len[ix] = COLUMN - 1;
}
printf("\ninitialize a pointer array to point the first element of each row.\n");
for (ix = 0; ix < ROW; ix ++) {
ptr_arr[ix] = &arr[ix][0];
printf("ptr_arr[%d] -> %d\n", ix, *ptr_arr[ix]);
}
printf("\nprint out result[%d]:\n", ctr);
while(sta != ctr) {
print_out();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment