Project 4
simulation with conway's rules for life - revisited
due before 4/30 11:59pm, autograded by project 4 zylab, limited to 10 submissions.
We will be using the concepts from project 2 to test the more recent concepts of linked lists, pointers, and file I/O. The initial data points will be stored in a file (specification of the layout of the file below), each grid of values will be dynamically allocated and then referenced via a pointer in a linked list.
More details are:
input file
first line will list the number of time steps to simulate
second line will include number of rows followed by the number of columns in the grid to be simulated
remaining lines in the file will have the positions of the initial cells that are "alive"
dynamically create nodes that use the following struct
struct node {
int rows;
int columns;
int * grid; // a pointer to the grid that should be of size rows*columns
struct node *next_step; // a pointer to the node that holds the grid for the next time step
};
you will create a linked list of nodes where each node is a single time step
the begin pointer should point to the initial grid, and each time step should follow.
no array notation is to be used, so no []'s should appear in your code.
You MUST create the following functions
struct node *createInitialNode(FILE *input, int *numsteps); // read data from file pointer input and return a dynamically created node with the data and the number of time steps to simulate in numsteps void nextStep(struct node *begin); // add a struct node to the end of the list pointed to by begin for the next time step, so use data in the last node in the list to create the new timestep. void printList(struct node *begin); // print out all the grids contained in the list pointed to by begin.
You should use your code from project 2. If you had issues with the functions from project 2, you should see the professor or a TA ASAP!!!!!!
The autograder will test the three functions above (as well as the functionality of your new array creation) and produce a score of 65 points. The remaining 35 points will be for style per @7
Notes:
by using the linked list there is no limit to the number of time steps to simulate, recall project 2 had a max number of time steps.
by using dynamically allocated memory there is no limit to the grid size, recall project 2 had a max grid size.
when grading the style points, if any []'s are seen in the code, a 0 will be given for the autograded test cases.
Example input file sampleinput.txt
contains:
3
10 12
7 7
7 8
7 9
8 7
9 8
#include <stdio.h>
#include <stdlib.h>
struct node
{
int rows;
int columns;
int *grid;
struct node *next_step;
};
struct node *createInitialNode(FILE *input,int *numsteps);
void nextStep(struct node *begin);
void printList(struct node *begin);
int neighbors(struct node *nodeptr,int i,int j);
void printGrid(int *anArray,int rows,int columns);
int main(void)
{
char inputname[30];
printf("Welcome to Conway's game of Life\nPlease enter
filename\n");
scanf ("%[^\n]%*c", inputname); //Reading filename
FILE *fin;
fin=fopen(inputname,"r"); //Opening file
int numsteps;
struct node *head=createInitialNode(fin,&numsteps); //Creating
head node
// loop to get the array state after steps time steps
for(int step=1; step<=numsteps; step++) //Creating linked list
of nodes for each time step
nextStep(head);
// display the grid after steps number of time steps
printList(head); //Printing the grids in the linked list
return EXIT_SUCCESS;
}
// function to return the number of live neighboring cells for a cell at (i,j) at time step
struct node* createInitialNode(FILE *input,int *numsteps)
{
fscanf(input,"%d",numsteps); //Extracting the number of time
steps
int rows,columns,x,y;
fscanf(input,"%d %d",&rows,&columns); //Extracting the
number of rows and columns
struct node *head=(struct node *)malloc(sizeof(struct node));
//Dynamically creating a new node
//Initializing the new node
head->grid=(int*)malloc(columns*rows*sizeof(int)); //Dynamically
creating a new grid
head->columns=columns;
head->rows=rows;
head->next_step=NULL;
//Filling all grid values to 0
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
*(head->grid+columns*i+j)=0;
}
//Extracting the positions of live cells and marking it in the
grid
while(fscanf(input,"%d %d",&x,&y)==2)
*(head->grid+columns*x+y)=1;
return head;
}
void nextStep(struct node *begin)
{
struct node *next,*prev=begin;
next=(struct node *)malloc(sizeof(struct node)); //Creating new
node
int col=begin->columns;
//Initializing the new node
next->columns=begin->columns;
next->rows=begin->rows;
next->next_step=NULL;
next->grid=(int*)malloc(next->columns*next->rows*sizeof(int));
//Creating the grid
//Traversing to the end of linked list
while(prev->next_step!=NULL)
prev=prev->next_step;
prev->next_step=next; //Linking the new node to the end of
linked list
int liveNeighbors;
int r,c;
//Calculating the next state
for(r=0; r<begin->rows; r++)
{
for(c=0; c<begin->columns; c++)
{
liveNeighbors = neighbors(prev,r,c);
if( *(prev->grid+col*r+c) == 1)
{
if(liveNeighbors < 2 || liveNeighbors > 3)
*(next->grid+col*r+c)=0;
else
*(next->grid+col*r+c)=1;
}
else
{
if(liveNeighbors == 3)
*(next->grid+col*r+c)=1;
else
*(next->grid+col*r+c)=0;
}
}
}
return ;
}
void printList(struct node *begin)
{
//Traversing through the linked list
while(begin!=NULL)
{
printGrid(begin->grid,begin->rows,begin->columns);
begin=begin->next_step;
printf("\n");
}
return;
}
int neighbors(struct node *nodeptr,int i,int j)
{
int liveNeighbors=0;
int r=nodeptr->rows,c=nodeptr->columns;
int *anArray=nodeptr->grid;
if((i-1)>=0)
{
if((j-1) >=0)
{
if(*(anArray+c*(i-1)+j-1) == 1)
liveNeighbors++;
}
if(*(anArray+c*(i-1)+j) == 1)
liveNeighbors++;
if((j+1)<(c))
{
if(*(anArray+c*(i-1)+j+1) == 1)
liveNeighbors++;
}
}
if((j-1) >=0)
{
if(*(anArray+c*i+j-1) == 1)
liveNeighbors++;
}
if((j+1)<(c))
{
if(*(anArray+c*i+j+1) == 1)
liveNeighbors++;
}
if((i+1)<r)
{
if(*(anArray+c*(i+1)+j) == 1)
liveNeighbors++;
if((j-1) >=0)
{
if(*(anArray+c*(i+1)+j-1) == 1)
liveNeighbors++;
}
if((j+1)<(c))
{
if(*(anArray+c*(i+1)+j+1) == 1)
liveNeighbors++;
}
}
return liveNeighbors;
}
// function to print the grid at time step
void printGrid(int *anArray,int rows,int columns)
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
printf("%d ",*(anArray+columns*i+j));
}
printf("\n");
}
return ;
}
=========================================
See Output
Thanks, PLEASE RATE if helpful
--------------------------------------------USE BELOW
CODE----------------------
#include <stdio.h>
#include <stdlib.h>
struct node
{
int rows;
int columns;
int *grid;
struct node *next_step;
};
struct node *createInitialNode(FILE *input,int *numsteps);
void nextStep(struct node *begin);
void printList(struct node *begin);
int neighbors(struct node *nodeptr,int i,int j);
void printGrid(int *anArray,int rows,int columns);
int main(void)
{
char inputname[30];
printf("Welcome to Conway's game of Life\nPlease enter
filename\n");
scanf ("%[^\n]%*c", inputname); //Reading filename
FILE *fin;
fin=fopen(inputname,"r"); //Opening file
if(fin==NULL){
printf("ERROR opening filename %s\n",inputname);
return 0;
}
int numsteps;
struct node *head=createInitialNode(fin,&numsteps); //Creating
head node
// loop to get the array state after steps time steps
for(int step=1; step<=numsteps; step++) //Creating linked list
of nodes for each time step
nextStep(head);
// display the grid after steps number of time steps
printList(head); //Printing the grids in the linked list
return EXIT_SUCCESS;
}
// function to return the number of live neighboring cells for a cell at (i,j) at time step
struct node* createInitialNode(FILE *input,int *numsteps)
{
fseek(input, 0, SEEK_END); // goto end of file
if (ftell(input) == 0)
{
printf("ERROR with initial point values");
exit(0);
}
fseek(input, 0, SEEK_SET);
fscanf(input,"%d",numsteps); //Extracting the number of time
steps
int rows,columns,x,y;
fscanf(input,"%d %d",&rows,&columns); //Extracting the
number of rows and columns
struct node *head=(struct node *)malloc(sizeof(struct node));
//Dynamically creating a new node
//Initializing the new node
head->grid=(int*)malloc(columns*rows*sizeof(int)); //Dynamically
creating a new grid
head->columns=columns;
head->rows=rows;
head->next_step=NULL;
//Filling all grid values to 0
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
*(head->grid+columns*i+j)=0;
}
//Extracting the positions of live cells and marking it in the
grid
while(fscanf(input,"%d %d",&x,&y)==2)
*(head->grid+columns*x+y)=1;
return head;
}
void nextStep(struct node *begin)
{
struct node *next,*prev=begin;
next=(struct node *)malloc(sizeof(struct node)); //Creating new
node
int col=begin->columns;
//Initializing the new node
next->columns=begin->columns;
next->rows=begin->rows;
next->next_step=NULL;
next->grid=(int*)malloc(next->columns*next->rows*sizeof(int));
//Creating the grid
//Traversing to the end of linked list
while(prev->next_step!=NULL)
prev=prev->next_step;
prev->next_step=next; //Linking the new node to the end of
linked list
int liveNeighbors;
int r,c;
//Calculating the next state
for(r=0; r<begin->rows; r++)
{
for(c=0; c<begin->columns; c++)
{
liveNeighbors = neighbors(prev,r,c);
if( *(prev->grid+col*r+c) == 1)
{
if(liveNeighbors < 2 || liveNeighbors > 3)
*(next->grid+col*r+c)=0;
else
*(next->grid+col*r+c)=1;
}
else
{
if(liveNeighbors == 3)
*(next->grid+col*r+c)=1;
else
*(next->grid+col*r+c)=0;
}
}
}
return ;
}
void printList(struct node *begin)
{
//Traversing through the linked list
while(begin!=NULL)
{
printGrid(begin->grid,begin->rows,begin->columns);
begin=begin->next_step;
printf("\n");
}
return;
}
int neighbors(struct node *nodeptr,int i,int j)
{
int liveNeighbors=0;
int r=nodeptr->rows,c=nodeptr->columns;
int *anArray=nodeptr->grid;
if((i-1)>=0)
{
if((j-1) >=0)
{
if(*(anArray+c*(i-1)+j-1) == 1)
liveNeighbors++;
}
if(*(anArray+c*(i-1)+j) == 1)
liveNeighbors++;
if((j+1)<(c))
{
if(*(anArray+c*(i-1)+j+1) == 1)
liveNeighbors++;
}
}
if((j-1) >=0)
{
if(*(anArray+c*i+j-1) == 1)
liveNeighbors++;
}
if((j+1)<(c))
{
if(*(anArray+c*i+j+1) == 1)
liveNeighbors++;
}
if((i+1)<r)
{
if(*(anArray+c*(i+1)+j) == 1)
liveNeighbors++;
if((j-1) >=0)
{
if(*(anArray+c*(i+1)+j-1) == 1)
liveNeighbors++;
}
if((j+1)<(c))
{
if(*(anArray+c*(i+1)+j+1) == 1)
liveNeighbors++;
}
}
return liveNeighbors;
}
// function to print the grid at time step
void printGrid(int *anArray,int rows,int columns)
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
printf("%d ",*(anArray+columns*i+j));
}
printf("\n");
}
return ;
}
Project 4 simulation with conway's rules for life - revisited due before 4/30 11:59pm, autograded by...
Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...
implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...
Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...
The language should be in C Specifications Use this structure and constant for the linked list node: #define MAX_STR_LEN 80 struct link_node { char node_str[ MAX_STR_LEN ]; struct link_node *next; }; Your solution should incorporate the following functions // This function is used to compare two nodes. // The return values are: // -1: n1 < n2 // 0: n1 == n2 // +1: n1 > n2 int compare_node( struct link_node *n1, struct link_node *n2 ); // This function...
Language:C++
only numbers 4 and 5 please
2. 1. Use the Node class from the slides to create a linked list of int's. Create a Node pointer called ptrHead that points to the head of the list. Make the list 5 nodes long. Generate random values to populate the list's data. All nodes should be in the heap. Add a function called addNodeFront that takes a pointer to the head of a list as an input parameter and that adds...
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...
Trying to figure out what needs to be in the headers.h file. I have written the print function. Qualities in header file main() needs insertionSort() and mergeSortWrapper() Both insertionSort() and mergeSortWrapper() need print(). Please copy-and-paste the following files (0 Points): insertionSort.c /*--------------------------------------------------------------------------* *---- ----* *---- insertionSort.c ----* *---- ----* *---- This file defines a function that implements insertion ----* *---- sort on a linked-list of integers. ----* *---- ----* *---- ---- ---- ---- ---- ---- ---- ---- ---- ----* *----...
This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone book using a linked list to keep people's names and phone numbers organized in alphabetical order. 1. Define a structure to hold the contact information including: name, phone number, a pointer to the next node in the list. Example: struct ContactNode { string name; string phoneNumber; ContactNode *next; } 2. Define a class containing the structure and the appropriate methods to update and retrieve...
CS 215 Program Design, Abstraction, and Problem Solving Programming Project #3 INTRODUCTION The goal of this programming project is to enable the student to practice designing a program that solves a problem using a class and a linked-list and developing a C++ program to implement the solution. PROJECT TASKS 1. Read the problem definition below and then analyze it before designing a solution. You will produce a document (external documentation) of this definition, analysis, and design. 2. Write a C++...
Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...