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

need a code that counts the total number of Q so that it can be divided by the a

ID: 645410 • Letter: N

Question

need a code that counts the total number of Q so that it can be divided by the array size and calculate the fill percentage. basically the code should go near the end when the main function is located

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define ALIVE true
#define DEAD false
using namespace std;
const int ROWS = 24;
const int COLS = 79;


void Display(bool grid[ROWS][COLS])
{
for(int i = 0; i < ROWS; i++)
{
for(int j = 1; j < COLS; j++)
{
if(grid[i][j] == true)
{
cout << "Q";
}
else{
cout << " ";
}
}
cout << endl;
}
cout << endl;
}


void CopyGrid (bool grid[ROWS][COLS],bool grid2[ROWS][COLS]){
for(int a =0; a < ROWS; a++){
for(int b = 0; b < COLS; b++){grid2[a][b] = grid[a][b];}

}

}
void savePattern( int array1[ROWS][COLS], int ROWS, int COLS)
{
ofstream outFile("GliderGun.txt", ios::out);
int total = 0;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
total += array1[i][j]; // count the number of live cells
}
}
outFile << total << endl; // output the count of point coordinates
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (array1[i][j]) outFile << i << ' ' << j << endl; // output the coordinates in pairs
}
}
}
void liveOrDie(bool grid[ROWS][COLS])
{

bool grid2[ROWS][COLS] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < ROWS-1; a++)
{
for(int b = 1; b < COLS-1; b++)
{
int neighbors = 0;
for(int c = -1; c < 2; c++)
{
for(int d = -1; d < 2; d++)
{
if(!(c == 0 && d == 0))
{
if(grid2[a+c][b+d]) {++neighbors;}
}
}
}

if(neighbors < 2) {grid[a][b] = DEAD;}
else if(neighbors == 3) {grid[a][b] = ALIVE;}
else if(neighbors > 3) {grid[a][b] = DEAD;}

}

}
}

int main()

{

int array [ROWS][COLS];
int n = sizeof(array) / sizeof(array[0]);
double fillPercentage;
fillPercentage = n;

bool grid[ROWS][COLS] = {false};

ifstream in;
in.open("glidergun.txt");
int num, i1, i2;
if(in.is_open()){
in >> num;
while(in >> i1 >> i2){
grid[i1][i2] = true;
}
}
else{
cout << "Can not find glidergun.txt in your directory" << endl;
return 0;
}

for (int i = 1; i <= 60; ++i)
{
Display(grid);
liveOrDie(grid);
Sleep(1000);
cout << fillPercentage << " cycle #" << i << " Author: ";
}

system("PAUSE");
return 0;

}

Explanation / Answer

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ALIVE true
#define DEAD false
using namespace std;
const int ROWS = 24;
const int COLS = 79;


void Display(bool grid[ROWS][COLS])
{

int count=0;
for(int i = 0; i < ROWS; i++)
{
for(int j = 1; j < COLS; j++)
{
if(grid[i][j] == true)
{
cout << "Q";

count++;
}
else{
cout << " ";
}
}
cout << endl;
}
cout << endl;
}


void CopyGrid (bool grid[ROWS][COLS],bool grid2[ROWS][COLS]){
for(int a =0; a < ROWS; a++){
for(int b = 0; b < COLS; b++){grid2[a][b] = grid[a][b];}

}

}
void savePattern( int array1[ROWS][COLS], int ROWS, int COLS)
{
ofstream outFile("GliderGun.txt", ios::out);
int total = 0;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
total += array1[i][j]; // count the number of live cells
}
}
outFile << total << endl; // output the count of point coordinates
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (array1[i][j]) outFile << i << ' ' << j << endl; // output the coordinates in pairs
}
}
}
void liveOrDie(bool grid[ROWS][COLS])
{

bool grid2[ROWS][COLS] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < ROWS-1; a++)
{
for(int b = 1; b < COLS-1; b++)
{
int neighbors = 0;
for(int c = -1; c < 2; c++)
{
for(int d = -1; d < 2; d++)
{
if(!(c == 0 && d == 0))
{
if(grid2[a+c][b+d]) {++neighbors;}
}
}
}

if(neighbors < 2) {grid[a][b] = DEAD;}
else if(neighbors == 3) {grid[a][b] = ALIVE;}
else if(neighbors > 3) {grid[a][b] = DEAD;}

}

}
}

int main()

{

int array [ROWS][COLS];
int n = sizeof(array) / sizeof(array[0]);
double fillPercentage;
fillPercentage = n;

bool grid[ROWS][COLS] = {false};

ifstream in;
in.open("glidergun.txt");
int num, i1, i2;
if(in.is_open()){
in >> num;
while(in >> i1 >> i2){
grid[i1][i2] = true;
}
}
else{
cout << "Can not find glidergun.txt in your directory" << endl;
return 0;
}

for (int i = 1; i <= 60; ++i)
{
Display(grid);
liveOrDie(grid);
Sleep(1000);
cout << fillPercentage << " cycle #" << i << " Author: ";
}

system("PAUSE");
return 0;

}