Hello all. Newbie to programming so please, be nice!
I'm teaching myself C and I'm doing problems & examples from various books I have.
I'm trying to solve a problem involving a 2-dimensional integer array that has the same
number of rows as columns (five for my example), where each cell contains a positive
integer value, and where the sum of each row/column/diagonal is equivalent. I'm using
the values 1 through 25 as input from the user to identify the starting position. The rules
for the problem are as follows:
1. Start at any cell and place a 1 there,
2. Move to the next cell by moving to the right one column and up two rows; if you move
past an edge wrap around to the other side,
3. If the new cell is NOT occupied, place the next number there; otherwise, go back to the
previous cell, drop down to the cell under that one and place the next number there,
4. Repeat steps 2 and 3 until the table is filled.
I've put a lot of time into this problem and I thought I came up with a pretty good solution.
However, the program does not fill the table completely and I can't seem to figure out why.
I was hoping someone here could offer a suggestion. I'm a bit frustrated now.
TIA!
Chris//CMS
Code:
#include <stdio.h>
#define SIZE 25
int main() {
int x = 0; /* counter used in loop initialized to zero */
int y = 0; /* counter used in loop initialized to zero */
int start_row = 0; /* position of starting point (row) initialized to zero */
int start_column = 0; /* position of starting point (column) initialized to zero */
int old_row = 0; /* position of starting point (row) initialized to zero */
int old_column = 0; /* position of starting point (column) initialized to zero */
int table[5][5] = {0}; /* five-element, two-dimensional integer array initialized to zero */
/* prompt and read in starting position (row) */
printf(" ENTER STARTING POSITION -->\n");
printf(" ENTER STARTING ROW NUMBER [ENTER 1 THROUGH 5]: ");
scanf("%d", &start_row);
fflush(stdin);
while (start_row < 1 || start_row > 5) {
printf(" INVALID ENTRY!\n");
printf(" ENTER STARTING ROW NUMBER [ENTER 1 THROUGH 5]: ");
scanf("%d", &start_row);
fflush(stdin);
}
printf(" STARTING ROW IS %d\n", start_row); /* ERROR CHECK */
/* prompt and read in starting position (column) */
printf(" ENTER STARTING COLUMN NUMBER [ENTER 1 THROUGH 5]: ");
scanf("%d", &start_column);
fflush(stdin);
while (start_column < 1 || start_column > 5) {
printf(" INVALID ENTRY!\n");
printf(" ENTER STARTING COLUMN NUMBER [ENTER 1 THROUGH 5]: ");
scanf("%d", &start_column);
fflush(stdin);
}
printf(" STARTING COLUMN IS %d\n", start_column); /* ERROR CHECK */
/* this subtraction accounts for the difference between the user's input (the
* numbers 1 through 5) and the locations in the table array (0 through 4)
*/
start_row = start_row - 1;
start_column = start_column - 1;
/* this assigns the number one to the first place in the table array */
table[start_row][start_column] = 1;
for (x = 2; x <= SIZE; x++) {
old_row = start_row;
old_column = start_column;
/* calculates new row position where rows are labeled zero through four */
if (start_row >= 2)
start_row = start_row - 2;
else
start_row = start_row + 3;
/* calculates new column position where columns are labeled zero through four */
if (start_column < 4)
start_column++;
else
start_column = start_column - 4;
if (table[start_row][start_column] != 0) {
start_row = old_row;
start_column = old_column;
if (start_row == 4)
start_row++;
else
start_row = start_row - 4;
}
table[start_row][start_column] = x;
}
printf("\n __________________________\n");
/* loops used to iterate and print table */
for (x = 0; x < 5; x++) {
for (y = 0; y < 5; y++)
printf(" | %2d", table[x][y]);
printf(" |\n");
}
printf(" --------------------------\n\n");
return 0;
}