What I am trying to do is to have the program create 2D array that for each row, no two numbers are the same and only consists numbers between 1-9. The array lists out every possible combinations. Supposely I do not know the number of possible combinations, I had it keep adding new rows until there are no more possiblities left.
The problem with my code is when I print out the completed array, it gives out really, really large numbers. But when I print the array from inside the first loop, it gives correct values. I do not know exactly what happened. Here is the code:
Code:#include <stdio.h> int main(void) { int double_digit[1][2]; int a = 1; int b = 1; int i = -1; int c = 0; int d = 0; int f = 0; for ( a = 1; a < 10; a++) { for ( b = 1 ; b < 10; b++) { if (a != b) { i++; if (i == 0) { double_digit[i][0] = a; double_digit[i][1] = b; } else { int new_array[i+1][2]; for (c = 0; c < i; c++) { new_array[c][0] = double_digit[c][0]; new_array[c][1] = double_digit[c][1]; } int double_digit[i+1][2]; for (d = 0; d < i; d++) { double_digit[d][0] = new_array[d][0]; double_digit[d][1] = new_array[d][1]; } double_digit[i][0] = a; double_digit[i][1] = b; // printf("%d ",double_digit[i][0]); // printf("%d ",double_digit[i][1]); // printf("%d ",a); // printf("%d \n",b); } } } } for (f = 0; f < i+1; f++) { printf("%d %d \n",double_digit[f][0],double_digit[f][1]); } return 0; }



LinkBack URL
About LinkBacks



