Question

C++ there is an issue with my if/else statements, I have tried several different ways to...

C++ there is an issue with my if/else statements, I have tried several different ways to make it match the instructions but each time i get different errors. Need help geting this to match the instructions peoperly.

*******************instructions*****************************
Function: nextGeneration
This function has no parameters.
This function returns an int.
The purpose of this function is to modify the grid to represent the next generation. Here's how you are supposed to do that for this assignment:
Set each element of the count array to the number of live neighbors that the corresponding element in the grid array has.
For each element in the grid array, set it according to the following rules: If the cell was LIVE and has less than two or more than three live neighbors, then change it to DEAD. If the cell was DEAD and has exactly three live neighbors, then change it to LIVE.
Add one to the generation.
Return the value of the generation instance variable.

*************************code ******************************
int automata::nextGeneration() {
for (int r = 0; r < ROWS; r++) {
for ( int c = 0; c < COLS; c++) {
count[ROWS][COLS] = getLiveNeighbors(r,c);
}
}
for (int r = 0; r < ROWS; r++) {
for ( int c = 0; c < COLS; c++) {
if (grid[ROWS][COLS] == DEAD) {
if (count[ROWS][COLS] == 3) {
grid[ROWS][COLS] = LIVE;
} else {
if (count[ROWS][COLS] <= 2) {
grid[ROWS][COLS] = DEAD;
}
if (count[ROWS][COLS] > 3) {
grid[ROWS][COLS] = DEAD;
}
}
}
}
}
generation++;
return generation;
}


************************.h file**************************

#ifndef __AUTOMATA_H__
#define __AUTOMATA_H__

#include <iostream>
using std::ostream;

class automata {
private:
static const char LIVE = '*';
static const char DEAD = ' ';
static const int ROWS = 20;
static const int COLS= 60;

int generation;
char grid[ROWS][COLS];
int count[ROWS][COLS];

public:
automata();
void reset();
void setCell(const int r, const int col);
void clearCell(const int r, const int col);
bool isValidCell(const int r, const int c) const;
int getLiveNeighbors(const int r, const int c) const;
int getGeneration() const;
int nextGeneration();
friend ostream& operator<<(ostream& strm, const automata& brd);
};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have updated some changes in the if statements according to the condition, have included comments for the explanation.

for (int r = 0; r < ROWS; r++) {
for ( int c = 0; c < COLS; c++) {
if (grid[ROWS][COLS] == DEAD) {
if (count[ROWS][COLS] == 3) {
grid[ROWS][COLS] = LIVE;
}}// here you need to got out of grid[ROWS][COLS] == DEAD therefore i added one }

// from here the condition is grid[ROWS][COLS] == LIVE
else {
// in this if statement you need to have condition <2 or >3 according to the question
// || this sign is for or
if (count[ROWS][COLS] < 2 || count[ROWS][COLS] > 3)
{
grid[ROWS][COLS] = DEAD;
}

}
}
}

Hope it clears your doubt :)

Add a comment
Know the answer?
Add Answer to:
C++ there is an issue with my if/else statements, I have tried several different ways to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • Having a rough time getting started with the constructors and the display function of this class,...

    Having a rough time getting started with the constructors and the display function of this class, any advice/assitance is appreciated! Task You will write a class called Grid, and test it with a couple of programs. A Grid object will be made up of a grid of positions, numbered with rows and columns. Row and column numbering start at 0, at the top left corner of the grid. A grid object also has a "mover", which can move around to...

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

  • This program is in C++ which reserves flight seats. It uses a file imported to get...

    This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...

  • I wrote program that have to block all moves for player 1, for game TicTacTOE AI,...

    I wrote program that have to block all moves for player 1, for game TicTacTOE AI, but it is not working. Can someone check what I have not eliminated? #include using namespace std; const int ROWS = 3; const int COLS = 3; int counter = 0; void aITurn(char b[][COLS]); void fillBoard(char board[ROWS][COLS]); void getChoice(char b[][COLS], bool playerToggle); bool gameOver(char b[][COLS]); void showBoard(char board[ROWS][COLS]); int main() {    ///main is complete, nothing to do here    char board[ROWS][COLS];    bool playerToggle =...

  • Can you help us!! Thank you! C++ Write a program that can be used by a...

    Can you help us!! Thank you! C++ Write a program that can be used by a small theater to sell tickets for performances. The program should display a screen that shows which seats are available and which are taken. Here is a list of tasks this program must perform • The theater's auditorium has 15 rows, with 30 seats in each row. A two dimensional array can be used to represent the seats. The seats array can be initialized with...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...

    Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT