# Problems with growth program.

• 11-12-2007
teck
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.

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.
• 11-12-2007
matsp
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
• 11-12-2007
teck
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.
• 11-12-2007
matsp
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".     }```
• 11-14-2007
teck
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.