LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2003, 06:43 AM   #1
linux_GNUbie
LQ Newbie
 
Registered: Mar 2003
Location: Newark, DE USA
Distribution: Red Hat, Debian, now Gentoo and SUSE
Posts: 13

Rep: Reputation: 0
C Program Error?


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;

}
 
Old 11-30-2003, 08:14 AM   #2
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
Hi!

You have a typo in your code

Code:
...

      if (table[start_row][start_column] != 0) {
         start_row = old_row;
         start_column = old_column;

         if (start_row == 4) /* HERE! Should read:   start_row < 4 */
            start_row++;
         else
            start_row = start_row - 4;
      }

...
Regards
Martin
 
Old 11-30-2003, 08:39 AM   #3
linux_GNUbie
LQ Newbie
 
Registered: Mar 2003
Location: Newark, DE USA
Distribution: Red Hat, Debian, now Gentoo and SUSE
Posts: 13

Original Poster
Rep: Reputation: 0
Cool. Thanks Martin. My mistakes so far have always dealy with simple logic and math.
I was stuck looking at the lines
Code:
      old_row = start_row;
      old_column = start_column;
just inside the for loop. I played around with these lines for a while think my mistake was
somehow associated with them. I moved those lines around inside--and outside--the for
loop. I got different results each time I moved them. I created the variables old_row and
old_column to hold the initial values that identified the starting position, in case the next
position was already occupied; I'd still have the initial values to revert back to. I can't seem
to grasp why I can't move those two lines outside the for loop--say, just before the for loop.
All I'm doing is copying the values from one location in memory to another. I'm not doing
anything to the initial values of start_row or start_column. Yet when I move those two lines
outside/before the for loop I get strange results. Odd!

Thanks! I'll read some more.

Chris//CMS
 
Old 11-30-2003, 09:02 AM   #4
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
Quote:
Originally posted by linux_GNUbie

I was stuck looking at the lines
Code:
      old_row = start_row;
      old_column = start_column;
I can't seem to grasp why I can't move those two lines outside the for loop--say, just before the for loop.
I don't think it's strange. You save the old values of row and column there. But those values are different every time you go through the loop.


By the way. A little comment on your use of hard-coded variables. You make a
Code:
#define SIZE 25
in the beginning of your program. I would recommend that you change that to
Code:
#define MAX_COL 4
#define MAX_ROW 4
and then use those values every time when you loop and test (for example the test where you had the error). By doing that and removing all hard-coded values from the code, it's much easier to let your program calculate the same for, say a 6 * 6 matrix. Plus, it's easier to read and understand a line of code that says
Code:
      if (start_column < MAX_COL)
         start_column++;
than
Code:
      if (start_column < 4)
         start_column++;
Keep up the otherwise good work!

Regards
Martin
 
Old 11-30-2003, 06:49 PM   #5
linux_GNUbie
LQ Newbie
 
Registered: Mar 2003
Location: Newark, DE USA
Distribution: Red Hat, Debian, now Gentoo and SUSE
Posts: 13

Original Poster
Rep: Reputation: 0
Again, thank you Martin, for your time and incite. Does there come a time when
a person asks too many questions?

Chris//CMS
 
Old 12-01-2003, 03:18 AM   #6
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
Quote:
Originally posted by linux_GNUbie
Again, thank you Martin, for your time and incite. Does there come a time when
a person asks too many questions?

Chris//CMS
No problemo. Keep asking! If I don't have the answer someone else may be able to help you.

Programming is like learning a foreign language. You don't get really good at it by only reading the textbooks. You need to practice it for a while.

Regards
Martin
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in program dludenar Fedora 1 10-07-2005 03:20 AM
Program error mdkusr Linux - General 2 03-12-2005 01:19 PM
Weird error in C program ar1 Programming 7 02-09-2005 01:25 PM
Error for a very simple C program pkashyap Linux - Newbie 2 03-11-2004 10:24 AM
BDG Error on C++ program TriggerJ Programming 2 01-26-2004 10:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration