Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING CODE BLOCKS C++ ONLY: Optional Assignment ( 4% ) Due: The game of Life, in

ID: 3687821 • Letter: U

Question

USING CODE BLOCKS C++ ONLY:

Optional Assignment ( 4% )

Due:

The game of Life, invented by the mathematician John H. Conway, is intended to model life in a society of organisms. Consider a rectangular array of cells, each of which may contain an organism. If the array is assumed to extend indefinitely in both directions, each cell will have eight neighbors, the eight cells surrounding it.

Birth and deaths occur according to the following rules.

An organism is born in an empty cell that has exactly three neighbors.

An organism dies from isolation if it has fewer than two neighbors.

An organism dies from overcrowding if it has more than three neighbors.

The following display shows the first five generations of a particular configuration of organisms

                                                                              O

                                   O               OOO                 O                      OOO

       OOO                 OOO                                   O O                    O O

          O                    OOO            O O                  O                      OOO

                                                        O                    O                                                                  .

Write a program that will start with the first generation above. Your program must generate the next four generations. This can be done with two functions.

Submit your source code in Blackboard.

Read the first generation from a file named LIFE.txt.

Your source code must be well documented.

Explanation / Answer

#include<iostream.h>

#include<fstream.h>

#include<string>

#include<iomanip>

using namespace std;

const int size=10;

void OrginGet ( ifstream&, int[][size] );

void CopyTempToOrgin (int[][size], int[][size]);

void NeighborsCheck(int[][size], int[][size]);

main()

{

    ifstream   inFile;

    ofstream outFile;

    int orgin [10][size];

    int Temp [10][size];

    int Nog;

    inFile.open("LIFE.txt");

    OrginGet (inFile, orgin);

    cout<<"Welcome to the game of life ";

    cout<<"These are the rules of the game: ";

    cout<<"*An organism is born in any empty cell having exactly three neighbors.";

    cout<<"*An organism dies from isolation if it has fewer than two neighbors.";

    cout<<"*An organism dies from overcrowding if it has more than three neighbors.";

    cout<<"*All other organisms survive.";

    cout<<"please enter the number of generations you want to calculate.";

    cin>>Nog;

    for (int gen=0; gen<Nog ; gen++)

    {

        NeighborsCheck(orgin, Temp);

        cout<<"This is the generation number "<<gen+1<<" :";

        CopyTempToOrgin(orgin, Temp);

    }

    cout<<endl;

}

void OrginGet(ifstream& inFile, int orgin [][size] )

{

    int i, j;

    while(inFile)

    {

        for (i=0; i<size; i++)

        {

            for (j=i; j<size; j++)

                inFile>>orgin[i][j];

        }

    }

}

void NeighborsCheck(int orgin[][size], int Temp[][size])

{

    int temp=0;

    int i, j;

    for (i=0; i<size; i++)

    {

        for (j=0 ; j<size; j++)

        {

            if (orgin [i][j-1] == '1')

                temp++;

            else if (orgin[i-1][j-1] == '1')

                temp++;

            else if (orgin[i-1][j] =='1')

                temp++;

            else if (orgin [i-1][j+1]=='1')

                temp++;

            else if (orgin[i][j+1]=='1')

                temp++;

            else if (orgin[i+1][j+1]=='1')

                temp++;

            else if (orgin[i+1][j]=='1')

                temp++;

            else if (orgin[i+1][j-1]=='1')

                temp++;

            if (temp==3)

            {

                Temp[i][j] =='1';

                if ((temp<=1) || (temp>=4))

                    Temp[i][j] =='0';

            }

        }

    }

}

void CopyTempToOrgin( int orgin[][size], int Temp[][size] )

{

    int x, y;

    for (x=0; x<size ; x++)

    {

        for (y=x; y<size; y++)

            orgin [x][y] = Temp [x][y];

    }

}

#include<iostream.h>

#include<fstream.h>