| dritail (11) | |
|
#include <iostream> #include <iomanip> #include <fstream> #include <cmath> using namespace std; //Declaring Function Varibles const int MAX_ARRAY_SIZE=50; //FUNCTION//forprinting initiail board and title void PrintGen(char lifeBoard[][MAX_ARRAY_SIZE], ostream& outStream, int numRowsInBoard, int numColsInBoard, int generationNum) { if(generationNum==0) { outStream<<"Initial board"<<endl; } else { outStream << "LIFE game board generation "<<generationNum << endl; } for(int countrow=0;countrow<numRowsInBoard; countrow++) { for(int countcol=0;countcol<numColsInBoard; countcol++) { outStream<< lifeBoard[countrow][countcol]; outStream<<" "; } outStream<< endl; } outStream<<endl<<endl<<endl; } //FUNCTION// for printing NextGen void NextGen(char lifeBoard[][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard) { char nextboard[50][50]={0}; for(int countcol=0; countcol<numColsInBoard; countcol++) { for(int countcol2=0;countcol2<numColsInBoard; countcol2++) { nextboard[countcol][countcol2]='.'; } } //Checking neighbours for (int countrows=1; countrows<numRowsInBoard-1;countrows++) { for(int countcols=1; countcols<numColsInBoard-1; countcols++) { //Declaring Varibles int beside; //initializing neighbour beside=0; if(lifeBoard[countrows+1][countcols+1]=='X') { beside++; } if(lifeBoard[countrows+1][countcols-1]=='X') { beside++; } if(lifeBoard[countrows+1][countcols]=='X') { beside++; } if(lifeBoard[countrows][countcols-1]=='X') { beside++; } if(lifeBoard[countrows][countcols+1]=='X') { beside++; } if(lifeBoard[countrows-1][countcols-1]=='X') { beside++; } if(lifeBoard[countrows-1][countcols]=='X') { beside++; } if(lifeBoard[countrows-1][countcols+1]); { beside++; } if(beside==2 || beside==3) { if(beside==3 && lifeBoard[countrows][countcols]=='X') { nextboard[countrows][countcols]='X'; } if(beside==2 && lifeBoard[countrows][countcols]=='X') { nextboard[countrows][countcols]='X'; } if(beside==3 && lifeBoard[countrows][countcols]=='.') { nextboard[countrows][countcols]=='X'; } if(beside<2 || beside>3) { nextboard[countrows][countcols]='.'; } } } for(int colinboard=0; colinboard<numColsInBoard;colinboard++) { for(int colinboard2=0;colinboard2<numColsInBoard;colinboard2++) { lifeBoard[colinboard][colinboard2]=nextboard[colinboard][colinboard2]; } } } } // // // // // // //Main Body int main() { //Declaring Varibles int row; int column; int gens; int generationNum; int minarray; int maxarray; ifstream inputStream; ofstream outputStream; char inf[99]; char outf[99]; char Lifeboard[50][50]={0}; //Initializing Varibles row=0; column=0; gens=0; generationNum=1; minarray=0; maxarray=50; //Prompting for file name cout<<"Enter input file name:"; cin>>inf; inputStream.open(inf); //Check if file was opened if(inputStream.fail()) { cerr<<"ERROR : input file not opened correctly."<<endl; return 1; } //Prompting for output cout << "Enter the name of the output file: "; cin >> outf; outputStream.open(outf); if (outputStream.fail()) { cerr << "Error, output file not opened correctly." << endl; return 1; } //Checking for rows if(!(inputStream>>row)) { cerr<<"ERROR: Cannot read number of rows."<<endl; return 1; } //Checking valid number of rows if (row<=0 || row >49) { cerr<<"ERROR: Read an illegal number of rows for the board"<<endl; return 1; } //Checking for columns if(!(inputStream>>column)) { cerr<<"ERROR: Cannot read number of columns."<< endl; return 1; } //Checking for valid number of columns if(column<=0|| column>49) { cerr<<"ERROR: Read an illegal number of columns for the board."<<endl; return 1; } //Checking for generations if(!(inputStream>>gens)) { cerr<<"ERROR: Cannot read the number of generations."<<endl; return 1; } //Checking for valid number of generations if (gens<0) { cerr<<"ERROR: Read an illegal number of generations."<<endl; return 1; } //Readng for valid board for( int countrow=0;countrow<row;countrow++) { for(int countcol=0;countcol<column;countcol++) { inputStream>> Lifeboard[countrow][countcol]; //no data in file if(inputStream.eof()) { cerr<<"ERROR; Not enough data in the input file"<< endl; return 1; } if(Lifeboard[countrow][countcol]!='.'&& Lifeboard[countrow][countcol] !='X') { cerr<<"Error : Input data for initial board is incorrect"<< endl; } return 1; } } //Checking boarders for(int rowboarder=0;rowboarder<row;rowboarder++) { if(Lifeboard[0][rowboarder] =='X' || Lifeboard[row-1][rowboarder]=='X') { cerr<<"ERROR: Organisims are present in the boarder of the board, please correct your iput file."<< endl; return 1; } } for(int colboarder=0; colboarder<column;colboarder++) { if(Lifeboard[colboarder][0]=='X' || Lifeboard[colboarder][column-1] == 'X') { cerr<<"ERROR: Organisms are present in the boarder of the board, please correct your input file."<<endl; return 1; } } //Printing initial board PrintGen(Lifeboard, cout, row, column, 0); for(int currentgen=0;currentgen<=gens;currentgen++) { PrintGen(Lifeboard,outputStream, row, column, currentgen); NextGen(Lifeboard,row,column); } PrintGen(Lifeboard,cout,row,column,gens); return 0; } When i input my file, the outfile is ALWAYS empty. I would of expected some sort of output even if the output was wrong. Can you guys see where it went wrong? | |
|
|
|
| SuperSonic (69) | |
| I'm having trouble understanding what this code is suppossed to do. Can you explain it or put comments in it? I could help you if you did that. | |
|
|
|
| dritail (11) | |
|
Your main program should do each of the following 1. Request the name of the input file 2. Open the input file and check that it was opened correctly If it did not open correctly print the following error message and terminate the program ERROR: input file not opened correctly 3. Request the name of the output file, but used the error message 4. Open the output file and check that it was opened correctly If it did not open correctly print the following error message and terminate the program ERROR: output file not opened correctly 5. Read the dimensions of the game board from the input file 6. Check that the dimensions read were actually read and that if they were read the dimensions are within the specified range If one of the dimensions could not be read or if one or more dimensions is out of range print the appropriate message or messages given below and terminate the program ERROR: Read an illegal number of rows for the board ERROR: Read an illegal number of columns for the board ERROR: Cannot read number of rows ERROR: Cannot read number of columns 7. Read the number of generations to be calculated from the input file. Check if the generations was read and if the value read was within the acceptable range. Use the following error messages if generations is not read or is out of range ERROR: Cannot read the number of generations ERROR: Read an illegal number of generations 8. Read the initial configuration of the game board from the input file into array myLifeBoard[ ]. Place the .s or Xs, one character into each element of the 2-D array representing the life game board. · You should check that the input data contains only .s and Xs. If it contains any other characters you should print an error message and terminate the program, the error message should say which element of the array was being filled when the error occurred. That is the message should be ERROR: Input data for initial board is incorrect Location (8, 9) is not valid The numbers in the brackets will give the element for which the error occurred · You should be sure you have actually read enough data (that all your reads read a value) If one of your reads fails print the following error message ERROR: Not enough data in the input file · You should check to make sure that all data on the edges of the game board are .s and if any are not you should print the following error message and terminate the program. ERROR: organisms are present in the border of the board, please correct your input file 9. Use the PrintGen function to print the title and the initial configuration of the game board into the output file 10. Use the PrintGen function to print the title and the initial configuration of the game board to the screen 11. For each calculated generation requested your main program should: a. Use the NextGen function to calculate the locations of the organisms for the next generation. b. Print the results for the next generation (the one you just calculated using NextGen) into the output file using the PrintGen Function. c. If this is the first generation calculated or the last generation calculated, print the results for the generation to the screen using the PrintGen Function. Your PrintGen function should 1. Have the following prototype void PrintGen(char lifeBoard[ ][MAX_ARRAY_SIZE], ostream& outStream, int numRowsInBoard, int numColsInBoard, int generationNum); We have not yet discussed type ostream, it is a more general type that includes variable of types ofstream and cout and cerr. 2. Print the game board for this iteration to outStream a. Print a header to label the game board. The header should look like i. LIFE game board generation 123 ii. The number will indicate which generation’s game board is illustrated below the title. The example title would be for generation 123 b. Print the game board to outStream. When you print out the array print an X for each array location containing an organism, and a . for any location not containing an organism. Print one row of the game board per line. You should print one space between each successive X or . (This will enable any screen in the output file to be used later as part of an input file) c. Print three blank lines to outStream Your NextGen function should 1. Have the following prototype void NextGen (char lifeBoard[ ][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard); 2. Within the function, NextGen, a second array, nextGenBoard, will be declared. It will have size nexGenBoard[numRowsInBoard][numColumnsInBoard]. 3. Based on the locations of the organisms in The array lifeBoard, determine the locations of organisms for the next generation and place those organisms into the array nextGenBoard. To determine the locations of organisms for the next generation a. First for each row in the lifeBoard array (except the first row and the last row) i. For each element in a row in the lifeBoard (except the first element and the last element) 1. Count the number of organisms (Xs) in adjacent elements of the 2-D array. If we are considering lifeBoard[Z][Q], then the adjacent elements are lifeBoard[Z+1][Q+1], lifeBoard[Z+1][Q], lifeBoard[Z+1][Q-1], lifeBoard[Z][Q-1], lifeBoard[Z][Q+1], lifeBoard[Z-1][Q-1], lifeBoard[Z-1][Q] and lifeBoard[Z-1][Q-1] 2. If the element has exactly three neighbors (three adjacent elements containing organisms) then the element nextGenBoard[Z][Q] will be an X 3. If the element lifeBoard[Z][Q] contains an X and has two neighbors then nexGenBoard[Z][Q] will be an X 4. Otherwise nextGenBoard[Z][Q] will be a . 4. Once the entire next generation is determined copy the contents of nextGenBoard into the array lifeBoard and then return to the calling program. | |
|
|
|
| SamuelAdams (321) | |||
|
You don't write to your file anywhere. See example below. Lines added have // Test at end.
| |||
|
Last edited on
|
|||
| SamuelAdams (321) | |
|
After looking at this again, I think I see the problem. Looks like your trying to output to outSteam outStream<< lifeBoard[countrow][countcol];but your output file is defined as ofstream outputStream;I don't have your infile to work to test with, changing your outStream to outputStream might work. or change ofstream outputStream;to ofstream outStream; | |
|
Last edited on
|
|