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)%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.