- #1
inha
- 576
- 1
c questions: game of life, splitting a program
I'm doing the game of life as an exercise project for a c course. I've got a few questions regarding splitting the program into smaller pieces which is required for the project.
I've split it into these pieces:
printer.c: Used to print the matrix representing the current generation. Pretty useless considering it's not a too big piece of code.
calculator.c: Recieves the matrix representing the current generation and the indices of the element to be dealt with as an argument. Then calculates the sum of the surrounding elements and returns the state of the element in the next generation (1 for alive, 0 for dead). I'm using a "spherical geometry" to deal with the border effects and that's taken into consideration here by going through all the special cases too.
main.c: Generates the initial state (50x50 matrix) with the random number generator. I'm going to have a loop that goes through the previous generation with the function calculator. The next generation is stored into a temporary matrix element by element as the loop progeresses. When a state is returned by calculator it is compared to the previous state and if they're different a "change counter" is incremented and the loop is terminated if the change counter is too small (static state reached, I'm going to determine a proper number via a few trial runs once I get the whole thing done). When a new generation is ready it's printed and made the new initial state for the next run of the loop.
So I have 3 .c files. From what I've gathered I have to write .h files for each of those and they're supposed to include the declarations of the functions in their .c files. So would calculator.h be just like this since there's only the function calcultor in there:
?
I know I have to also somehow link the files to each other by using "#include"s in the .c files. So main.c would have #include "calculator.h" and #include "printer.h", correct? And calculator.c would have #include "calculator.h" and printer.c #include "printer.h"? Also I'm supposed to write the makefile for the whole thing. would that be just
?
I suppose the whole thing could be done a lot more efficiently and elegantly but the main purpose here is to produce a working program so I kept this at a level I'm comfortable at.
I'm doing the game of life as an exercise project for a c course. I've got a few questions regarding splitting the program into smaller pieces which is required for the project.
I've split it into these pieces:
printer.c: Used to print the matrix representing the current generation. Pretty useless considering it's not a too big piece of code.
calculator.c: Recieves the matrix representing the current generation and the indices of the element to be dealt with as an argument. Then calculates the sum of the surrounding elements and returns the state of the element in the next generation (1 for alive, 0 for dead). I'm using a "spherical geometry" to deal with the border effects and that's taken into consideration here by going through all the special cases too.
main.c: Generates the initial state (50x50 matrix) with the random number generator. I'm going to have a loop that goes through the previous generation with the function calculator. The next generation is stored into a temporary matrix element by element as the loop progeresses. When a state is returned by calculator it is compared to the previous state and if they're different a "change counter" is incremented and the loop is terminated if the change counter is too small (static state reached, I'm going to determine a proper number via a few trial runs once I get the whole thing done). When a new generation is ready it's printed and made the new initial state for the next run of the loop.
So I have 3 .c files. From what I've gathered I have to write .h files for each of those and they're supposed to include the declarations of the functions in their .c files. So would calculator.h be just like this since there's only the function calcultor in there:
Code:
int calculator(table[50][50], int i, int j)
?
I know I have to also somehow link the files to each other by using "#include"s in the .c files. So main.c would have #include "calculator.h" and #include "printer.h", correct? And calculator.c would have #include "calculator.h" and printer.c #include "printer.h"? Also I'm supposed to write the makefile for the whole thing. would that be just
Code:
CC=gcc -ansi -pedantic -Wall
gameoflife: main.o calculator.o printer.o
$(CC) -o gameoflife main.o calculator.o printer.o
calculator.o: calculator.c calculator.o
$(CC) -c calculator.c
printer.o: printer.c printer.o
$(CC) -c printer.c
main.o: main.c calculator.h printer.h
$(CC) -c main.c
?
I suppose the whole thing could be done a lot more efficiently and elegantly but the main purpose here is to produce a working program so I kept this at a level I'm comfortable at.
Last edited: