The homework assignment is to get the sum of each entry in the grid plus its neighbors, then put that into a new grid based on some rules that can be seen in. That part works fine. I can get the first two grids to come up right, but we need it to work for any number of grids forCode:int sumOfNeighbors. I feel like I've tried it a million ways and can't get past the second generation unless I code it for a specific number of grids, which isn't the way we are supposed to do it. I tried making a tempGrid to move grid2 into grid1, but grid1 still always comes back as the first grid?Code:int gen = 'number of grids here'

Example output would be:

Gen 115 7 4

5 39 27

15 12 63adds up to -> 66 97 77

93 187 152

71 161 141Gen 2<-rules applied to sums makes gen 2 63 94 74

90 1 1

68 1 138adds up to-> 248 323 170

317 530 309

160 299 141Gen 3<-rules applied to sums makes gen 3 1 1 0

1 0 1

0 1 1

Any nudge in the right direction would be much appreciated! Thanks in advance

Code:#include<stdio.h>#include <stdlib.h> #include <time.h> #define ROWS 3 #define COLS 3 int seed = 29; int high = 40; int gen = 4; int grid1[ROWS][COLS]; int grid2[ROWS][COLS]; int tempGrid[ROWS][COLS]; int size = sizeof grid1 / sizeof *grid1; int sumOfNeighbors(int grid[][COLS], int row, int col, int count){ int i, j, sum = 0; for(i = row - 1; i <= row + 1; i ++){ for(j = col - 1; j <= col + 1; j++){ if( i >= 0 && i <= size - 1 && j >= 0 && j <= size - 1){ sum += grid[i][j]; } } } //printf("\n\nSUM: %d\n\n", sum); //If the sum is % 10 -> self = 0 if(sum % 10 == 0){ grid[i][j] = 0; } //if the sum is under 50 -> add 3 to self else if(sum < 50){ grid[i][j] = sum + 3; } // if sume is over 50 and less than 150 -> substract 3 from self (can't be less than 0) else if(sum > 50 && sum < 150){ if(sum <= 0){ grid[i][j] = 0; } else{ grid[i][j] = sum - 3; } } // if the sum is over 150 -> self = 1 else{ grid[i][j] = 1; } sum = grid[i][j]; return sum; } void printArray(int grid[][COLS], int row, int col){ for(int i = 0; i < row; i++){ printf("\n"); for(int j = 0; j < col; j++){ printf("%3d ", grid[i][j]); } } printf("\n\n"); } void fillGrid(int grid[][COLS], int row, int col){ srand(seed); for(int i = 0; i < row; i++){ for(int j = 0; j < col; j++){ grid[i][j] = rand() % high; } } } void createNewGrid(int grid[][COLS], int row, int col){ int count = 1; int k, i, j, num = 0; for(k = 0; k < gen; k++){ for(i = 0; i < ROWS; i++){ for(j = 0; j < COLS; j++){ num = sumOfNeighbors(grid, i, j, count); grid2[i][j] = num; tempGrid[i][j] = grid2[i][j]; //printf("TEMP GRID BELOW\n"); //printArray(tempGrid, ROWS, COLS); } } if(count % 2 == 1){ printf("----GEN %d----\n", count); printArray(grid, ROWS, COLS); } else{ printf("----GEN %d----\n", count); printArray(grid2, ROWS, COLS); } count ++; } for(i = 0; i < ROWS; i++){ for(j = 0; j < COLS; j++){ grid1[i][j] = tempGrid[i][j]; //printf("GRID 1 BELOW\n"); //printArray(grid1, ROWS, COLS); } } } int main(){ /* int count = 1; int num = 0; */ fillGrid(grid1, ROWS, COLS); createNewGrid(grid1, ROWS, COLS); }