how can i make this fstream work it suppose to be a glider gun. the txt file has
ID: 644962 • Letter: H
Question
how can i make this fstream work it suppose to be a glider gun. the txt file has this values
36
1 25
2 23
2 25
3 13
3 14
3 21
3 22
3 35
3 36
4 12
4 16
4 21
4 22
4 35
4 36
5 1
5 2
5 11
5 17
5 21
5 22
6 1
6 2
6 11
6 15
6 17
6 18
6 23
6 25
7 11
7 17
7 25
8 12
8 16
9 13
9 14
#include <iostream>
#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 a = 1; a < ROWS; a++){
for(int b = 1; b < COLS; b++){
if(grid[a][b] == true){
cout << "Q";
}
else{
cout << " ";
}
}
}
}
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[][COLS], int h, int w)
{
ofstream outFile("GliderGun.txt", ios::out);
int total = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; 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 < h; i++)
{
for (int j = 0; j < w; 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;
srand(time(0));
bool grid[ROWS][COLS] = {};
for(int i = 0; i < ROWS; ++i){
for(int j = 0; j < COLS; ++j){
grid[i][j] = rand() % 2;
}
}
Display(grid);
grid[ROWS/2][COLS/2] = true;
grid[ROWS/2-1][COLS/2] = true;
grid[ROWS/2][COLS/2+1] = true;
grid[ROWS/2][COLS/2-1] = true;
grid[ROWS/2+1][COLS/2+1] = true;
for (int i = 1; i <= 50; ++i)
{
cout << " " << fillPercentage << " cycle #" << i << " Author: " << endl;
cout << endl;
Display(grid);
liveOrDie(grid);
Sleep(1000);
}
system("PAUSE");
return 0;
}
Explanation / Answer
how can i make this fstream work it suppose to be a glider gun. the txt file has this values
36
1 25
2 23
2 25
3 13
3 14
3 21
3 22
3 35
3 36
4 12
4 16
4 21
4 22
4 35
4 36
5 1
5 2
5 11
5 17
5 21
5 22
6 1
6 2
6 11
6 15
6 17
6 18
6 23
6 25
7 11
7 17
7 25
8 12
8 16
9 13
9 14
#include <iostream>
#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 a = 1; a < ROWS; a++){
for(int b = 1; b < COLS; b++){
if(grid[a][b] == true){
cout << "Q";
}
else{
cout << " ";
}
}
}
}
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[][COLS], int h, int w)
{
ofstream outFile("GliderGun.txt", ios::out);
int total = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; 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 < h; i++)
{
for (int j = 0; j < w; 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;
srand(time(0));
bool grid[ROWS][COLS] = {};
for(int i = 0; i < ROWS; ++i){
for(int j = 0; j < COLS; ++j){
grid[i][j] = rand() % 2;
}
}
Display(grid);
grid[ROWS/2][COLS/2] = true;
grid[ROWS/2-1][COLS/2] = true;
grid[ROWS/2][COLS/2+1] = true;
grid[ROWS/2][COLS/2-1] = true;
grid[ROWS/2+1][COLS/2+1] = true;
for (int i = 1; i <= 50; ++i)
{
cout << " " << fillPercentage << " cycle #" << i << " Author: " << endl;
cout << endl;
Display(grid);
liveOrDie(grid);
Sleep(1000);
}
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.