Please anyone who can help me by changing the following code in to C programing
ID: 3752834 • Letter: P
Question
Please anyone who can help me by changing the following code in to C programing instead of C++
#include <iostream>
using namespace std;
const int ROWS = 40;
const int COLS = 30;
int futureBoard[ROWS][COLS];
void GameBoard(int board[ROWS][COLS]);
void SwitchBoard (int board[ROWS][COLS]);
void NextGeneration(int board[ROWS][COLS]);
int main()
{
int board[ROWS][COLS] = {0};
board[1][2] = 1;
board[2][3] = 1;
board[3][1] = 1;
board[3][2] = 1;
board[3][3] = 1;
// Displaying the board
cout << "Original Generation" << endl;
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
if (board[r][c] == 0)
cout << "|_|";
else
cout << "|*|";
}
cout << endl;
}
cout << endl;
cout << "print upto 50 generation" << endl;
for (int l = 0; l<50; l++)
{
cout << "Next Generation" << endl;
NextGeneration(board);
GameBoard(board);
SwitchBoard(board);
} return 0;
}
void GameBoard(int board[ROWS][COLS])
{
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
if (board[r][c] == 0)
cout << "|_|";
else
cout << "|*|";
}
cout << endl;
}
}
void SwitchBoard (int board[ROWS][COLS])
{
futureBoard[ROWS][COLS] = {0};
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
board[r][c]= futureBoard[r][c];
}
}
}
void NextGeneration(int board[ROWS][COLS])
{
// Loop through every cell
for (int r = 1; r < ROWS - 1; ++r)
{
for (int c = 1; c < COLS - 1; ++c)
{
// finding no Of Neighbours that are alive
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours += board[r + i][c + j];
// The cell needs to be subtracted from
// its neighbours as it was counted before
aliveNeighbours -= board[r][c];
// Implementing the Rules of Life
// Cell is lonely and dies
if ((board[r][c] == 1) && (aliveNeighbours < 2))
futureBoard[r][c] = 0;
// Cell dies due to over population
else if ((board) && (aliveNeighbours > 3))
futureBoard[r][c] = 0;
// A new cell is born
else if ((board[r][c] == 0) && (aliveNeighbours == 3))
futureBoard[r][c] = 1;
// Remains the same
else
futureBoard[r][c] = board[r][c];
}
}
}
Explanation / Answer
Answer:
Conversion from C++ to C.
Main changes are
Header file change. Changed header file from <iostream to stdio.h> and removed using namespace std.
Changed from const int of ROWS and COLS to
#define ROWS 40
#define COLS 30
otherwise, it will give error "Variably modified 'types' at file scope".So we have to change from const int to #define in C.
3. Changed all cout to printf, for printing
And Build and executed the program both are giving the same result.
The code in C for the above program as follows. You can directly copy & paste can use it.
/*Changed header file from <iostream to stdio.h> and removed using namespace std.*/
#include <stdio.h>
/*Changed from const int to #define otherwise it will give Variably modified 'types' at file scope */
#define ROWS 40
#define COLS 30
int futureBoard[ROWS][COLS];
void GameBoard(int board[ROWS][COLS]);
void SwitchBoard(int board[ROWS][COLS]);
void NextGeneration(int board[ROWS][COLS]);
int main()
{
int board[ROWS][COLS] = { 0 };
board[1][2] = 1;
board[2][3] = 1;
board[3][1] = 1;
board[3][2] = 1;
board[3][3] = 1;
// Displaying the board
printf("Original Generation "); /*Changed all cout to printf, for printing*/
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
if (board[r][c] == 0)
printf("|_|");
else
printf("|*|");
}
printf(" ");
}
printf(" ");
printf("print upto 50 generation ");
for (int l = 0; l < 50; l++)
{
printf("Next Generation ");
NextGeneration(board);
GameBoard(board);
SwitchBoard(board);
}
return 0;
}
void GameBoard(int board[ROWS][COLS])
{
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
if (board[r][c] == 0)
printf("|_|");
else
printf("|*|");
}
printf(" ");
}
}
void SwitchBoard(int board[ROWS][COLS]) {
futureBoard[ROWS][COLS];
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
board[r][c] = futureBoard[r][c];
}
}
}
void NextGeneration(int board[ROWS][COLS])
{
// Loop through every cell
for (int r = 1; r < ROWS - 1; ++r)
{
for (int c = 1; c < COLS - 1; ++c)
{
// finding no Of Neighbors that are alive
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours += board[r + i][c + j];
// The cell needs to be subtracted from
// its neighbors as it was counted before
aliveNeighbours -= board[r][c];
// Implementing the Rules of Life
// Cell is lonely and dies
if ((board[r][c] == 1) && (aliveNeighbours < 2))
futureBoard[r][c] = 0;
// Cell dies due to over population
else if ((board) && (aliveNeighbours > 3))
futureBoard[r][c] = 0;
// A new cell is born
else if ((board[r][c] == 0) && (aliveNeighbours == 3))
futureBoard[r][c] = 1;
// Remains the same
else
futureBoard[r][c] = board[r][c];
}
}
}