Thread: help reading array

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    11

    Arrow help reading array

    Hello , im trying to read an array[256][256] in 8x8 blocks , i need to break the array into a 8x8 matrix and send it to a funcion. This is how i was trying to do it , but it seems that only one line 8x256 is being read. Please help..



    Code:
    temp[8][8];
    array[256][256];
    
    for(int i=0; i<256; i+=8){
    
    for(int x=i; x<i+8; x++)
    for(int y=i; y<i+8; y++)
    temp[x-i][y-i]=array[x][y];
    
    function(temp);
    
    //.......
    //rest
    //......
    
    }
    Thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The first iteration of your loop results in temp[-1][-1] being accessed. You need to re-think your loops.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    It seems to be correct for me , i may be wrong im a begineer but i test that code with the following

    Code:
    int main(){
    int temp[8][8];
    
    for(int i=0; i<256; i+=8){
    
    for(int x=i; x<i+8; x++)
    for(int y=i; y<i+8; y++)
    temp[x-i][y-i]=y;
    
    for(int x=i; x<i+8; x++){
            cout<<endl;
    for(int y=i; y<i+8; y++)
    cout<<x-i<<"  "<<y-i<<" ";
    getchar();
    }
    
    for(int x=i; x<i+8; x++){
            cout<<endl;
    for(int y=i; y<i+8; y++)
    cout<<temp[x-i][y-i]<<" ";
    getchar();
    }
    
    
    
    
    }
    
    
    
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Quote Originally Posted by hkl01
    Hello , im trying to read an array[256][256] in 8x8 blocks , i need to break the array into a 8x8 matrix and send it to a funcion. This is how i was trying to do it , but it seems that only one line 8x256 is being read. Please help..



    Code:
    temp[8][8];
    array[256][256];
    
    for(int i=0; i<256; i+=8){
    
    for(int x=i; x<i+8; x++)
    for(int y=i; y<i+8; y++)
    temp[x-i][y-i]=array[x][y];
    
    function(temp);
    
    //.......
    //rest
    //......
    
    }
    Thanks

    How about this:


    Code:
    for(int i=0; i<256; i+=8)
    {
         for(int x=0; x<8; x++)
        {
              for(int y=0; y<8; y++)
             {
                   temp[x+i][y]=array[x][y];
             }
        }
    }
    Last edited by pityocamptes; 05-04-2006 at 04:50 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    ok from what i see it you are missing parts of the array ...

    so you read array[0][0] till array[7][7] the first loop
    then you start again from array[8][8] to array[15][15] etc..

    but what about array[7][8-255] ?
    or array[15][16-255] ... ? etc.

    I hope you understand what you missed

    EDIT: what pityocamptes posted is wrong.
    Last edited by neoragexxx; 05-04-2006 at 05:05 PM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    Yes im reading only the first column 8x8 ... but i cant think about how to read the other 31 8x8 matrix , thanks neoragexxx and pityocamptes

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    I think i found the solution can you check it please to be sure ... Thanks!

    Code:
    for(int i=0; i<256; i+=8){
    for(int j=0; j<256; j+=8){
    
    for(int x=i; x<i+8; x++){
            cout<<endl;
    for(int y=j; y<j+8; y++){
    temp[x-i][y-j]=image[x][y];
    cout<<"image["<<x<<"]["<<y<<"]"<<endl;
    }
    }
    getchar();

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Quote Originally Posted by hkl01
    Yes im reading only the first column 8x8 ... but i cant think about how to read the other 31 8x8 matrix , thanks neoragexxx and pityocamptes

    Ok, miss read what you were trying to do. Looks like you might need another for loop to move you through the columns.


    Code:
     
    for(int rowBlock=0; rowBlock<256; rowBlock+=8)  
    {
         for(int colBlock=0; colBlock<256;colBlock+=8)
         {
              for(int row=0; row<8; row++)
              {
                   for(int col=0; col<8; col++)
                   {
                        temp[row+rowBlock][col+colBlock]=array[row][col];
                   }
              }
          }
    }
    Last edited by pityocamptes; 05-04-2006 at 05:22 PM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    pityocamptes and neoragexxx thanks a lot for your help.

    i got the same code as pityocamptes.

    thanks

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    your code is right pityocamptes is just that in the temp[][] i use "-" instead of + because temp is [8][8]

    thanks again pityocamptes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data into a 2D array
    By swgh in forum C Programming
    Replies: 2
    Last Post: 08-17-2007, 03:07 AM
  2. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Reading a mixed Text and decimal file into an array
    By djamie in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 06:25 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM