Thread: sum of neighbors in matrix - can't get past second matrix

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    1

    sum of neighbors in matrix - can't get past second matrix

    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
    Code:
    int sumOfNeighbors
    . 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 for
    Code:
    int gen  = 'number of grids here'
    . 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?

    Example output would be:
    Gen 1
    15 7 4
    5 39 27
    15 12 63
    adds up to -> 66 97 77
    93 187 152
    71 161 141
    Gen 2 <-rules applied to sums makes gen 2
    63 94 74
    90 1 1
    68 1 138
    adds up to-> 248 323 170
    317 530 309
    160 299 141
    Gen 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);
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,347
    Quote Originally Posted by t-hulk
    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?
    From what I see, you only need two grids: the source grid and the destination grid. After you have performed the computations to populate the destination grid, you print it etc, and then you copy the destination grid to the source grid, overwriting it for the next generation.

    So, I would suggest that you rename createNewGrid to computeGrid, such that it looks like this:
    Code:
    void computeGrid(int sourceGrid[][COLS], int destGrid[][COLS], int rows, int cols)
    Also, create a copyGrid function and rename printArray to printGrid. Then in main, you do something like this:
    Code:
    int sourceGrid[ROWS][COLS];
    int destGrid[ROWS][COLS];
    
    fillGrid(sourceGrid, ROWS, COLS);
    
    for (int i = 0; i < gen; ++i)
    {
        populateGrid(sourceGrid, destGrid, ROWS, COLS);
        printGrid(destGrid, ROWS, COLS);
        copyGrid(destGrid, sourceGrid, ROWS, COLS);
    }
    By the way, you should not have any global variables. Global constants are fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-21-2016, 12:20 AM
  2. Replies: 5
    Last Post: 01-06-2016, 01:15 AM
  3. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  4. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 12:35 AM

Tags for this Thread