Thread: Complex for-statements

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    Complex for-statements

    I have got this four for-statements in a program of mine:

    Code:
    for (int i = a + 1, j = b + 1; i < 8 || j < 8; i++, j++)
    {
    	field[i][j] = -1;
    	filled++;
    	cout << i << ' ' << j << endl;		// Just to see what happens
    }
    for (int i = a + 1, j = b - 1; i < 8 || j >= 0; i++, j++)
    {
    	field[i][j] = -1;
    	filled++;
    }
    for (int i = a - 1, j = b + 1; i >= 0 || j < 8; i++, j++)
    {
    	field[i][j] = -1;
    	filled++;
    }
    for (int i = a - 1, j = b - 1; i >= 0 || j >= 0; i++, j++)
    {
    	field[i][j] = -1;
    	filled++;
    }
    I have got the random numbers between 0 and 8 and field has the size [8][8].

    I wanted to set from the random numbers a and b the diagonals to -1. I handle the two diagonals in two ways: first diagonal down and then up.

    The problem is that after compiling and running the program crashes. After putting a cout statement in the first for-loop I saw what went wrong. This is the output:
    Code:
    2 5
    3 6
    4 7
    5 8
    6 9
    7 10
    I wasn't expecting that i or j would be greater then eight. Can someone tell me what I am doing wrong.

    I am using BCC5.5.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    || means or, not and. And is &&.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    I now || means or. Else I will have the problem that the statement will be false if I get i = 6 and j = 10. It must stop before it gets there. So it needs an or.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Stop and think for a minute. Consider the i=6, j=9 case you had above. Is (i < 8 || j < 8) true? Of course it is -- since i = 6, i < 8. Do you want it to be true? Of course you don't, because your for-loop doesn't stop. Fortunately for you, (i < 8 && j < 8) will be false in this case, since j<8 will be false.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    A yes. You're right. I always want to swap that to around. In a more normal for-statement I always see it directly that that's wrong. In this case I looked after the crash to that statement and thougt it was right.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, it looks like you ought to be decrementing in some of those loops.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You might want to consider using a function of sorts for this. The loops are all quite similar. You'd just have to pass in parameters describing whether you wanted i and j to increment or decrement. Then you'd probably return the filled value after the loop had finished.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complex Number Class
    By Sephiroth1109 in forum C++ Programming
    Replies: 15
    Last Post: 12-12-2007, 04:46 PM
  2. Why am I getting 'undelcared identifier' ???
    By Bill83 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 01:00 PM
  3. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  4. 2 am complex double conversion woes
    By Roule in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2004, 02:53 PM
  5. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM