Thread: Problems with growth program.

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    18

    Problems with growth program.

    This may seem like kind of a tall order but I've been running into problems with a program I've been working on and I've been unable to figure out why some of the things go wrong... particularly when all the conditions/code look right.

    To understand this code properly, I feel I need to explain where the idea came from.
    I saw in a computer lab once about a problem called Eden. How it works is that there a set of boxes/matrix full of zeros with only one 1. It works through the idea that if there's room for that 1 to expand to then those boxes next to it will become a 1, and if it can't expand it dies (becomes 0).

    Being who I am, I decided lets try to develop a way to do this without using if statements, that way this problem can be done easier with larger matrixes.
    Long while later, I figured it out and set it as a function. However, I still had to develop a method for a variable matrix.

    So without further ado, here is my code with comments:
    Code:
     //Models binary growth and decay given that a population dies when it can't expand
    //and will expand to any areas immediately next to it that are empty.
    
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include <iostream>
    
    using namespace std;
    
    int twovar(int s, int v1, int v2, int f)
    {
        f = (s - (v1 + v2)/2 - ((s - 1)*((v1 + v2)&#37;2))); //"abs" caused a problem, why?
    }
    
    int threevar(int s, int v1, int v2, int v3, int f)
    {
        f = (s - (v1 + v2 + v3)/3 - ((s - 1)*((v1 + v2 + v3)%3 - ((v1 + v2 + v3)%3)/2)));
    }
    
    int fourvar(int s, int v1, int v2, int v3, int v4, int f)
    {
        f = (s - (v1 + v2 + v3 + v4)/4 - ((s - 1)*((v1 + v2 + v3 + v4)%4 - ((v1 + v2 + v3 + v4)%4)/3 - ((v1 + v2 + v3 + v4)%4)/2)));
    }
    
    main()
    {
          int r, c, row, col;
          int a[4][4];
          int sa[4][4];
          int refresh, time = 0, stime;
          char any;
          a[1][1] = 1; a[1][2] = 0; a[1][3] = 0; a[1][4] = 0;
          a[2][1] = 0; a[2][2] = 0; a[2][3] = 0; a[2][4] = 0; //error, sets [4][1] and [3][4]
          a[3][1] = 0; a[3][2] = 0; a[3][3] = 0; a[3][4] = 0; //to 4 despite these settings.
          a[4][1] = 0; a[4][2] = 0; a[4][3] = 0; a[4][4] = 0;
          cin >> any;
          stime = clock()/CLOCKS_PER_SEC; 
          /*for (row = 1; row <= 4; ++row)    //my idea as to how the above area 
          {                                   //could be made more generic.
              for (col = 1; col <=4; ++col)   //fails to initialize though....
              {
                  a[row][col] = 0;
              }
          }
          a[1][1] = 1;*/
          do{
          system("cls");
          for (row = 1; row <= 4; ++row)
          {
              for(col = 1; col <= 4; ++col)   //used to output each number of the matrix
              {                               //such that they are seperated by a space
                      cout << a[row][col] << "\t";
              }
              cout << "\n";
          }
          for (row = 1; row <= 4; ++row)
          {
              for(col = 1; col <= 4; ++col)
              {
                  if ((row == 1) && (col == 1))
                  {
                        sa[row][col] = abs(twovar(a[row][col], a[row + 1][col], a[row][col + 1], sa[row][col]));
                  }
                  if ((row == 1) && (col != 1) && (col != 4))
                  {
                          sa[row][col] = abs(threevar(a[row][col], a[row + 1][col], a[row][col - 1], a[row][col + 1], sa[row][col]));
                  }
                  if ((row == 1) && (col == 4))
                  {
                          sa[row][col] = abs(twovar(a[row][col], a[row + 1][col], a[row][col - 1], sa[row][col]));
                  }
                  if ((row != 1) && (row != 4) && (col == 1))
                  {
                          sa[row][col] = abs(threevar(a[row][col], a[row - 1][col], a[row + 1][col], a[row][col + 1], sa[row][col]));
                  }
                  if ((row != 1) && (row != 4) && (col != 1) && (col != 4))
                  {
                          sa[row][col] = abs(fourvar(a[row][col], a[row - 1][col], a[row + 1][col], a[row][col - 1], a[row][col + 1], sa[row][col]));
                  }
                  if ((row != 1) && (row != 4) && (col == 4))
                  {
                           sa[row][col] = abs(threevar(a[row][col], a[row - 1][col], a[row + 1][col], a[row][col - 1], sa[row][col]));
                  }
                  if ((row == 4) && (col == 1))
                  {
                           sa[row][col] = abs(twovar(a[row][col], a[row - 1][col], a[row][col + 1], sa[row][col]));
                  }
                  if ((row == 4) && (col != 1) && (col != 4))
                  {
                           sa[row][col] = abs(threevar(a[row][col], a[row - 1][col], a[row][col - 1], a[row][col + 1], sa[row][col]));
                  }
                  if ((row == 4) && (col == 4))
                  {
                           sa[row][col] = abs(twovar(a[row][col], a[row - 1][col], a[row][col - 1], sa[row][col]));
                  }
              }
          }
          for (row = 1; row <= 4; ++row)
          {
              for(col = 1; col <= 4; ++col)
              {
                  a[row][col] = sa[row][col];
              }
          }
          time = clock()/CLOCKS_PER_SEC - stime;
          cout << "\n \n" << time;
             do{
              refresh = clock()/CLOCKS_PER_SEC - stime - time;
              } while (refresh < 3);
          } while (1 != 0);
          
          
    }
    
    //program fails after first loop.
    //certain values fail to calculate properly, namely [4][2] and [2][4].
    The compilier I'm using is Dev-C++, running on Windows.

    I'm unsure whether it's something wrong with the code or something else so if someone else could give it a try it would be much appreciated.

    Any questions or wish to see previous code known to work, please post or send a message.
    Last edited by teck; 11-12-2007 at 02:52 PM. Reason: Editing code presentation

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think all your code suffers from exactly the same problem, but I will use this section as an example:
    Code:
          int a[4][4];
          int sa[4][4];
    
          a[1][1] = 1; a[1][2] = 0; a[1][3] = 0; a[1][4] = 0;
          a[2][1] = 0; a[2][2] = 0; a[2][3] = 0; a[2][4] = 0; //error, sets [4][1] and [3][4]
          a[3][1] = 0; a[3][2] = 0; a[3][3] = 0; a[3][4] = 0; //to 4 despite these settings.
          a[4][1] = 0; a[4][2] = 0; a[4][3] = 0; a[4][4] = 0;
    In c/c++ an array is declared with a number stating "how many elements you want". In this case 4 in each dimension.
    The index you should use starts with zero. So 4 elements are 0, 1, 2, 3 - there is no element 4.
    In your case, you are not setting element 0, and then setting element 5 that doesn't actually exist in the array.

    It looks like your for-loops are doing the same thing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    lol, figures how a simple thing can make everything else not work. Thanks a bundle. It works now.

    I'll try and remember that next time I ever decide to use strings.

    Thanks.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are repeating your if-statements a little bit more than I would like to see. Perhaps you can use nested if's, e.g:
    Code:
       if (row == 0) {
            if (col == 0) {
                ...
            }
            else if (col == 3)  {
                ...
            } 
            else {
              ...
            }
         } 
         else if (row == 3) {
            ...
         } else {
             .... // row is not on the "edge". 
         }
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    Thanks for the suggestion.

    I'm just working on making it alot more of a variable matrix (and finding simple limitations such as it can't be bigger than 10X10 for some reason, oh well).

    Hope to be able to have it relatively complete soon.
    Last edited by teck; 11-14-2007 at 04:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  3. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 04:26 PM
  4. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM