1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
2
Rep Power
0

#### Function Repeating indefinitely

So, I'm trying to do a John Conway's Game of Life program and I'm supposed to make it go through so many cycles. I have managed to make it go indefinitely, but I can't for the life of me make it step after so many iterations. I set up the repeat function so it counts the iterations and stops after so many times. Here's the code

Code:
```#include <stdio.h>
#include <stdlib.h>
void findsum(int sumdata[][10], int sumr, int sumc, int sumpass[][10]);
void findresult(int result[][10], int resdata[][10], int resr, int resc);
void repeat(int repeatsum[][10], int repdata[][10]);

int main (void)
{
int r;
int c;
int data[10][10] =    {1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 1, 1, 1}; //10x10 array of our starting values

printf("\n\n");
int sum[10][10];
int result[10][10];

findsum(data, r, c, sum); //passes to sum function

system("PAUSE");
return 0;
}

//this function adds up the value of each point with its neighbors and passes it to the repeat function
void findsum(int sumdata[][10], int sumr, int sumc, int sumpass[][10])
{

printf("sumdata\n\n");
for(sumr = 0; sumr < 10; sumr ++)
{
for(sumc = 0; sumc < 10; sumc++)
{
printf("%d ", sumdata[sumr][sumc]);
}
printf("\n");
} //prints our initial data from the main function

for(sumr = 0; sumr < 10; sumr ++)
{
for(sumc = 0; sumc < 10; sumc++)
{
sumpass[sumr][sumc] = sumdata[sumr][sumc];
}
} //equates sumpass to sumdata so we can find the sum without messing with our original array

printf("\n\n");
printf("\nsumpass\n\n");
for(sumr = 0; sumr < 10; sumr ++)  //finding the sum of each value and its neighbors
{
for(sumc = 0; sumc < 10; sumc++)
{ //corners have the least number of neighbors
if (sumr == 0 && sumc == 0) //top left corner
sumpass[sumr][sumc] += sumdata[sumr + 1][sumc] + sumdata[sumr][sumc + 1];
if (sumr == 0 && sumc == 9) //top right corner
sumpass[sumr][sumc] += sumdata[sumr + 1][sumc] + sumdata[sumr][sumc - 1];
if (sumr == 9 && sumc == 9) //bottom right corner
sumpass[sumr][sumc] += sumdata[sumr - 1][sumc] + sumdata[sumr][sumc - 1];
if (sumr == 9 && sumc == 0) //bottom left corner
sumpass[sumr][sumc] += sumdata[sumr - 1][sumc] + sumdata[sumr][sumc + 1];

//each edge of the array has special considerations so we don't look to a value that doesn't exist
if (sumr == 0 && sumc != 0 && sumc != 9) //rest of top row
sumpass[sumr][sumc] += sumdata[sumr][sumc - 1] + sumdata[sumr + 1][sumc] + sumdata[sumr][sumc + 1];
if (sumc == 0 && sumr != 0 && sumr != 9) //rest of left column
sumpass[sumr][sumc] += sumdata[sumr - 1][sumc] + sumdata[sumr + 1][sumc] + sumdata[sumr][sumc + 1];
if (sumr == 9 && sumc != 0 && sumc != 9) //rest of right column
sumpass[sumr][sumc] += sumdata[sumr - 1][sumc] + sumdata[sumr][sumc - 1] + sumdata[sumr][sumc + 1];
if (sumc == 9 && sumr != 0 && sumr != 9) //rest of bottom row
sumpass[sumr][sumc] += sumdata[sumr - 1][sumc] + sumdata[sumr][sumc - 1] + sumdata[sumr + 1][sumc];

//you can look all around these other values in the middle of the array
//without worrying about running off the edge
if (sumr != 0 && sumr != 9 && sumc != 0 && sumc != 9) //everything else
sumpass[sumr][sumc] += sumdata[sumr + 1][sumc] + sumdata[sumr][sumc + 1] + sumdata[sumr - 1][sumc] + sumdata[sumr][sumc - 1];

printf("%d ", sumpass[sumr][sumc]);
}
printf("\n");
}
printf("\n\n");

repeat(sumpass, sumdata); //passing to repeat function
}

//this function allows us to count how many times we've gone through the sum and result functions
//so we can stop after so many iterations
void repeat(int repeatsum[][10], int repdata[][10])
{
int repr;
int repc;
int counter;

for(counter = 0; counter < 5; counter++)
{

findresult(repdata, repeatsum, repr, repc); //passes to result function
printf("\n%d\n", counter); //displays how many iterations we've gone through
}
}

//takes the array with the sums and follows a set of rules to reassign new binary values
void findresult(int result[][10], int resdata[][10], int resr, int resc)
{
int counter;
int repsum[10][10];
int respass[10][10];

for(resr = 0; resr < 10; resr ++)
{
for(resc = 0; resc < 10; resc++)
{
respass[resr][resc] = resdata[resr][resc];
}
} //equates respass with resdata so we don't replace our original data
printf("\n\n");

printf("\nrespass\n\n");
for (resr = 0; resr < 10; resr++)
{
for (resc = 0; resc < 10; resc++)
{
if (resdata[resr][resc] == 0)
respass[resr][resc] = 0;
if (resdata[resr][resc] == 1)
respass[resr][resc] = 0;
if (resdata[resr][resc] == 2)
respass[resr][resc] = result[resr][resc];
if (resdata[resr][resc] == 3 && result[resr][resc] == 0)
respass[resr][resc] = 1;
if (resdata[resr][resc] == 3 && result[resr][resc] == 1)
respass[resr][resc] = 0;
if (resdata[resr][resc] == 4)
respass[resr][resc] = 1;
if (resdata[resr][resc] == 5)
respass[resr][resc] = 0;

printf("%d ", respass[resr][resc]); //replaces each value with a 1 or a 0 and prints result
}

printf("\n");
}
findsum(respass, resr, resc, repsum); //passes to sum function to start all over again
}```

• salem agrees : +++ for getting code tags on your first post - good job
2. Try breaking the recursion.

findsum -> repeat -> findresult -> findsum -> .......

You want a loop in main, something like
Code:
```for ( n = 0 ; n < numIterations ; n++ ) {
fundsum();
findresult();
}```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
2
Rep Power
0
Originally Posted by salem
Try breaking the recursion.

findsum -> repeat -> findresult -> findsum -> .......

You want a loop in main, something like
Code:
```for ( n = 0 ; n < numIterations ; n++ ) {
fundsum();
findresult();
}```
I have that exact code in the repeat function. The purpose of the function is to count the iterations and stop it after so many times.
findsum -> repeat++ = 1 -> findresult ->
findsum -> repeat++ = 2 -> findresult ->
findsum -> repeat++ = 3 -> findresult ->
findsum -> repeat++ = 4 -> findresult ->
findsum -> repeat++ = 5(STOP)

here's my repeat function isolated
Code:
```//this function allows us to count how many times we've gone through the sum and result functions
//so we can stop after so many iterations
void repeat(int repeatsum[][10], int repdata[][10])
{
int repr;
int repc;
int counter;

for(counter = 0; counter < 5; counter++)
{

findresult(repdata, repeatsum, repr, repc); //passes to result function
printf("\n%d\n", counter); //displays how many iterations we've gone through
}
}```
4. Here's your code, reduced to ONE printf per function.
Code:
```#include <stdio.h>
#include <stdlib.h>
void findsum(int sumdata[][10], int sumr, int sumc, int sumpass[][10]);
void findresult(int result[][10], int resdata[][10], int resr, int resc);
void repeat(int repeatsum[][10], int repdata[][10]);

int main (void)
{
int r;
int c;
int data[10][10] =    {1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 1, 1, 1, 1,
0, 0, 1, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 1, 1, 1}; //10x10 array of our starting values

printf("\n\n");
int sum[10][10];
int result[10][10];

findsum(data, r, c, sum); //passes to sum function

system("PAUSE");
return 0;
}

//this function adds up the value of each point with its neighbors and passes it to the repeat function
void findsum(int sumdata[][10], int sumr, int sumc, int sumpass[][10])
{
printf("sumdata\n\n");
///
repeat(sumpass, sumdata); //passing to repeat function
}

//this function allows us to count how many times we've gone through the sum and result functions
//so we can stop after so many iterations
void repeat(int repeatsum[][10], int repdata[][10])
{
int repr;
int repc;
int counter;

for(counter = 0; counter < 5; counter++)
{
findresult(repdata, repeatsum, repr, repc); //passes to result function
printf("\n%d\n", counter); //displays how many iterations we've gone through
}
}

//takes the array with the sums and follows a set of rules to reassign new binary values
void findresult(int result[][10], int resdata[][10], int resr, int resc)
{
printf("\nrespass\n\n");
findsum(respass, resr, resc, repsum); //passes to sum function to start all over again
}```
Look at these two lines
> findresult(repdata, repeatsum, repr, repc); //passes to result function
> printf("\n%d\n", counter); //displays how many iterations we've gone through
When will findresult() return, so you can print the counter.

Better yet, try this
Code:
```        printf("\n%d\n", counter); //displays how many iterations we've gone through
findresult(repdata, repeatsum, repr, repc); //passes to result function```
Do you see anything other than counter=0 printed (no, you won't).

Since repeat() just passes on a call, you have this.
Code:
```//this function adds up the value of each point with its neighbors and passes it to the repeat function
void findsum(int sumdata[][10], int sumr, int sumc, int sumpass[][10])
{
printf("sumdata\n\n");
///
findresult(sumpass, sumdata,sumr,sumc);
}

//takes the array with the sums and follows a set of rules to reassign new binary values
void findresult(int result[][10], int resdata[][10], int resr, int resc)
{
printf("\nrespass\n\n");
findsum(respass, resr, resc, repsum); //passes to sum function to start all over again
}```
Do you see now that there is no way out of this?

You have endless recursion, not a loop!