Question

C programming Game of life GAME !!!! I need some code for this function : /*...

C programming

Game of life GAME !!!!

I need some code for this function :

/*

* get_grid creates new memory for a "grid".

* x is the height and y is the width.

*/

char** get_grid(int x, int y){

}

/*

* print_grid attempts to print an x height

* by y width grid stored at the location

* provided by grid

*/

void print_grid(int x, int y, char** grid){

}

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

#include <stdio.h>

#include<stdlib.h>

char** get_grid(int x, int y);

void print_grid(int x, int y, char** grid);

/*

* get_grid creates new memory for a "grid".

* x is the height and y is the width.

*/

char** get_grid(int x, int y) {

  

char **ptr;

ptr = (char **)malloc(x * sizeof(char *));

  

for(int i = 0; i < x; i++)

ptr[i] = (char *)malloc(y * sizeof(char *));

  

for (int i = 0; i < x; i++) {

for (int j = 0; j < y; j++) {

if (ptr[i][j] == 0)

printf(".");

else

printf("*");

}

printf("\n");

}

printf("\n");

  

return ptr;

}

/*

* print_grid attempts to print an x height

* by y width grid stored at the location

* provided by grid

*/

void print_grid(int x, int y, char** grid) {

char **future;

future = (char **)malloc(x * sizeof(char *));

  

for(int i = 0; i < x; i++)

future[i] = (char *)malloc(y * sizeof(char *));

  

// Loop through every cell

for (int l = 1; l < x - 1; l++)

{

for (int m = 1; m < y - 1; m++)

{

// 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 += grid[l + i][m + j];

  

// The cell needs to be subtracted from

// its neighbours as it was counted before

aliveNeighbours -= grid[l][m];

  

// Implementing the Rules of Life

  

// Cell is lonely and dies

if ((grid[l][m] == 1) && (aliveNeighbours < 2))

future[l][m] = 0;

  

// Cell dies due to over population

else if ((grid[l][m] == 1) && (aliveNeighbours > 3))

future[l][m] = 0;

  

// A new cell is born

else if ((grid[l][m] == 0) && (aliveNeighbours == 3))

future[l][m] = 1;

  

// Remains the same

else

future[l][m] = grid[l][m];

  

}

}

for (int i = 0; i < x; i++)

{

for (int j = 0; j < y; j++)

{

if (future[i][j] == 0)

printf(".");

else

printf("*");

}

printf("\n");

}

}

int main(int argc, const char * argv[]) {

print_grid(10,10, get_grid(10,10));

  

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C programming Game of life GAME !!!! I need some code for this function : /*...
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
  • C programming Othello BOARD GAME !!!! I need some code for this function : // Returns...

    C programming Othello BOARD GAME !!!! I need some code for this function : // Returns true if the board is fully occupied with discs; else returns false bool isBoardFull(char board[][SIZE]){ // REPLACE THIS WITH YOUR IMPLEMENTATION }

  • Need help in c++ programming to output the lines in my code: if word if found:...

    Need help in c++ programming to output the lines in my code: if word if found:            cout << "'" << word << "' was found in the grid" << endl;            cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • I HAVE THE FOLLOWING CODE IN JAVA FOR A SNAKE GAME, I WANT SOMEONE TO EXPLAIN...

    I HAVE THE FOLLOWING CODE IN JAVA FOR A SNAKE GAME, I WANT SOMEONE TO EXPLAIN EACH PART OF THE CODE, WHAT'S THE FUNCTION OF EACH PART THANK YOU import java.awt.Color; import java.awt.Graphics; public class BodyPart { private int xCoor, yCoor, width, height; public BodyPart(int xCoor, int yCoor, int tileSize) { this.xCoor = xCoor; this.yCoor = yCoor; width = tileSize; height = tileSize; } public void tick() { } public void draw(Graphics g) { g.setColor(Color.BLACK); g.fillRect(xCoor * width, yCoor *...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • I need help solving this basic C++ question. I need help fixing my code.   Question: Design...

    I need help solving this basic C++ question. I need help fixing my code.   Question: Design a class called a box that represents a box. Box classes have variables such as the length of the box, width, and height. 1. Member variables shall be dedicated members. 2. Define the creator of the Box Class. The creator may receive all of the data and may not receive any. 3. Add accessor and creator 4. Add an empty function, which indicates whether...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • I NEED HELP WITH C PROGRAMMING. Answer each of the following. Assume that unsigned integers are...

    I NEED HELP WITH C PROGRAMMING. Answer each of the following. Assume that unsigned integers are stored in 4 bytes and that the starting address of the array is at location 200 600 in memory. i)What address is referenced by vPtr + 5? What value is stored at that location? j) Assuming vPtr points to values[6] , what address is referenced by vPtr -= 4? What value is stored at that location? Also ..... For each of the following, write...

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
ADVERTISEMENT