Well the full thing is this
Code:
#include <stdio.h>
#include <clib.h>
#include <stdlib.h>
int main(void)
{
char repeat;
int matrix[100][100],r,c,total;
do
{
FILE *game;
printf("************************************\n* Welcome to the Game of Life. *\n************************************\n");
if((game=fopen("matrix100x100.txt", "r")) != NULL) //finds and reads matrix file//
{
printf ("File open\n\n");
for ( r = 0 ; r < 100 ; r++ ) //controls rows//
{
for ( c = 0 ; c < 100 ; c++ ) //controls columns//
{
fscanf( game, "%d", &matrix[r][c] );//put values into Matrix from file//
printf(" %d", matrix[r][c]); //print current Matrix Values//
}
printf("\n"); //Give a new line at each row//
}
}
else printf ("File could not be opened\n");
printf ("\nThis is the Original Generation\n\nPress a Key to create Second Generation\n");
getch(); //Stops programme until user inputs a value//
system("cls"); //Clears Screen//
printf("************************************\n* Welcome to the Game of Life. *\n************************************\n");
printf ("Updating Matrix\n\n");
for (r = 0 ; r < 100 ; r++ ) //increments rows//
{
for ( c = 0 ; c < 100 ; c++ ) //increments Columns//
{
total = matrix[r-1][c-1]+matrix[r][c-1]+matrix[r+1][c-1]
+matrix[r-1][c]+matrix[r+1][c]+matrix[r-1][c+1]+matrix[r][c+1]
+matrix[r+1][c+1]; //sums neighbouring cells//
if (matrix[r][c]==0 && total==3 || matrix[r][c]==1 && total==2 || matrix[r][c]==1 && total==3)
{
matrix[r][c]=1; //sets value to alive if fitting rules//
}
else if (matrix[r][c]==1 && total>3 || matrix[r][c]==1 && total<2)
{
matrix[r][c]=0; //sets value to dead if fitting rules//
}
printf(" %d", matrix[r][c]); //prints new matrix values//
}
printf("\n");
}
game = fopen ( "matrix100x100.txt", "w" );
if ( game == NULL )
{
printf ( "File open failure" );
}
fprintf ( game, "%d", matrix[r][c]);
fclose (game); //closes file//
printf ("\nDo you want to run it again? Y/N\n"); //option to re-run programme//
repeat=getch();
system ("cls"); //Clears Screen//
}
while (repeat=='y'); //if user types 'y', programme repeats//
}
And ive attatched the txt file. didnt want to bung everything up with too much code, but there you go!
Thanks