Nack - 4 months ago 12

C Question

Being new to C, and this website, I'm unfamiliar with this problem I'm having. I have a 2 dimensional array with [8][8] elements. I'm trying to get the user to enter numbers into the array until finished. The program is far from finished, but I'm stuck on this problem before I can move on. Basically I use a for loop to let the user enter into each element. However, when the first row is complete, it overwrites it's last value onto the first column second row element spot. How can I prevent this from happening: Here's my code:

`#include <stdio.h>`

#include <string.h>

int Check_rules();

void Print_Array(int array[][8], int size)

{

int i, j;

for (i = 0; i <= size; i++)

{

printf("\n");

for (j = 0; j <= size; j++)

{

printf("%d ",array[i][j]);

}

}

printf("\n\n");

}

int main()

{

int size = 8;

int i, j;

int fullArray[size][size];

int grid1[3][3];

int grid2[3][3];

int grid3[3][3];

int grid4[3][3];

int grid5[3][3];

int grid6[3][3];

int grid7[3][3];

int grid8[3][3];

int grid9[3][3];

for (i = 0; i <= size; i++)

{

for (j = 0; j <= size; j++)

fullArray[i][j] = 0;

}

printf("Want to play a game? Enter values 1-9 starting in row 1 column 1, \nand we will work our way from there. Here's the playing board.\nIt's Sudoku, so follow the rules of the game.\n\n");

for (i = 0; i <= size; i++)

{

printf("\n");

for (j = 0; j <= size; j++)

printf("%d ",fullArray[i][j]);

}

printf("\n\n");

int tmp;

char *keeper = (" ");//space for marker

for (i = 0; i <= size; i++)

{

for (j = 0; j <= size; j++)

{

printf("Enter first value(press 0 and ENTER to skip a box, \nand -1 to cancel game): ");

scanf("%d", &tmp);

if(tmp == -1)

return 0;

fullArray[i][j] = tmp;

Print_Array(fullArray,size);

}

}

return 0;

}

If you run this you'll see my problem when you enter the last value in row 1. It overwrites the second row first column element spot?

Answer

Everywhere you have `<= size`

, you actually want `< size`

. This is because C uses 0-based indexes. That means if you have an array with 5 elements, the indexes are 0, 1, 2, 3, 4. In a loop like `for (int i = 0; i <= 5; i++)`

, `i`

would get the values 0, 1, 2, 3, 4, 5. That last one is an invalid index into the array. Using `i < 5`

fixes the problem (ensures `i`

stops before it reaches 5).

Fixed and cleaned up version of your code:

```
#include <stdio.h>
#include <string.h>
void printArray(int size, int array[][size]) {
for (int i = 0; i < size; i++) {
printf("\n");
for (int j = 0; j < size; j++) {
printf("%d ", array[i][j]);
}
}
printf("\n\n");
}
int main() {
int size = 8;
int fullArray[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
fullArray[i][j] = 0;
}
}
printf("Enter values in row 1 column 1, and we will work our way from there. Here's the playing board. \n\n");
printArray(size, fullArray);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("Enter first value (press 0 and ENTER to skip a box, or -1 to cancel game): ");
int number;
scanf("%d", &number);
if(number == -1) {
return 0;
}
fullArray[i][j] = number;
printArray(size, fullArray);
}
}
return 0;
}
```

**EDIT**

To clarify, this is fixed version of the original code in the question. The new code is a bit different, but I think the issue is the same.