Thread: kernel iteration through for loop

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    19

    kernel iteration through for loop

    Just wondering is there a way of generating dynamic variable in C++

    For example could you write a function where you define a kernel size i.e. 3×3 5×5 7×7 and the function
    dynamically generate the kernel rather that hand coding it as below
    Code:
    array[row-1][col-1]
    array[row-1][col]
    array[row-1][col+1]
    
    array[row][col-1]
    array[row][col]
    array[row][col+1]
    
    array[row+1][col-1]
    array[row+1][col]
    array[row+1][col+1]
    This kernel would then be used to iterate through a for loop to calculate the average around row col value


    May not be that clear, and I do apologise

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're right, that wasn't particularly clear! Don't know if this will help, or even if its on the right track:
    Code:
     #include <iostream>
     #include <iomanip>
     
     int main(void)
     {
       int array[3][3];
       
       for (int i = 0; i < 3; i++)
       {
         for (int j = 0; j < 3; j++)
         {
           array[i][j] = i * 10 + j;
         }
       }
       
       for (int i = 0; i < 3; i++)
       {
         for (int j = 0; j < 3; j++)
         {
           std::cout <<"array[" <<i <<"][" <<j <<"] = " 
                     <<std::setw(2) <<std::setfill('0') 
                     <<array[i][j] <<std::endl;
         }
       }
       
       
       return(0);
     }
     
      /*
      Output:
     
      array[0][0] = 00
      array[0][1] = 01
      array[0][2] = 02
      array[1][0] = 10
      array[1][1] = 11
      array[1][2] = 12
      array[2][0] = 20
      array[2][1] = 21
      array[2][2] = 22
     
      */
    If you want dynamic sized arrays, use new/delete, or an STL container (eg vector)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    19
    Here is some of the code I've written

    Code:
    if(BoundryKernal==3){
         std::cout<<"\n Kernal size 3";
          for (int plane=0; plane<1; ++plane) {
    	 for (int col=1; col<numCols-1; ++col) {
    			 for (int row=1; row<numRows-1; ++row) {
    
                             SpatialSumCell[index][col][row]=(array[index][col-1][row-1]+array[index][col-1][row]+array[index][col-1][row+1]+array[index][col][row-1]+array[index][col][row]+array[index][col][row+1]+array[index][col+1][row-1]+array[index][col+1][row]+array[index][col+1][row+1])/9;
                             			 }
             }
          }//end if statement boundrykernal==3
       }//end if
    What i was wondering is it possible to dynamically create these array[][] varibles for example a 5 or 7 cell kernel using C++ while also being able to control the indexing on the array[][] i.e

    Code:
     array[col-?][row-?]
    Last edited by sononix; 08-11-2004 at 01:47 AM.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Im not exactully not sure what you are trying to do but maybe you want to look into the STL
    heres a link
    http://www.msoe.edu/eecs/ce/courseinfo/stl/index.htm
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>What i was wondering is it possible to dynamically create these array[][] varibles
    Yes, of course.
    Code:
    int **array = new int*[rows];
    for (int x = 0; x < rows; x++) {
      array[x] = new int[cols];
    }
    This creates a simulated array of arrays using dynamic memory. To release the memory you perform the same operations in reverse using delete.
    Code:
    for (int x = 0; x < rows; x++) {
      delete [] array[x];
    }
    delete [] array;
    However, managing your own memory is error prone. A better soution is to use the standard vector container given to you by the standard C++ library.
    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
      std::vector< std::vector<int> > v;
    
      v.push_back(std::vector<int>(5, 10));
      v.push_back(std::vector<int>(5, 20));
      v.push_back(std::vector<int>(5, 30));
      for (int x = 0; x < 3; x++) {
        for (int y = 0; y < 5; y++) {
          std::cout << v[x][y] << ' ';
        }
        std::cout << std::endl;
      }
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Kernel bug? (attn matsp)
    By brewbuck in forum Linux Programming
    Replies: 7
    Last Post: 04-13-2009, 10:31 AM
  2. SOS - Can a monolithic kernel be used in a micro-kernel style OS?
    By sean in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-20-2008, 09:30 AM
  3. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  4. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  5. The "continue" statement in JAVA
    By Hexxx in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-26-2004, 06:19 PM