Arrays with C++

Discussion in 'Mac Programming' started by jbstew32, Nov 14, 2008.

  1. jbstew32 macrumors regular

    Joined:
    Jan 15, 2007
    #1
    I'm writing a program that does image manipulation. I have a block of code that looks like this:

    Code:
    	int i,j;
    	int rowCount,colCount=0;
    	for(i=h0;i<h1;i++) {
    		colCount = 0;
    		for(j=v0;j<v1;j++) {
    			grid1[rowCount][colCount] = pixelValue[i][j];
                            cout << "value: " << grid1[rowCount][colCount] <<endl;
    			colCount++;	
    		}
    		
    		rowCount++;
    	}
    
    
    

    basically, my cout statement outputs the correct values, so grid1 stores the values correctly at that point.

    Once I'm outside of the for loops though, my grid1 doesn't have any values! It's all zero's! it's like it's been reset to what it was before the nested for loops.

    I just don't get it. It's probably some stupid reason that I'm just too tired to see. what is causing this, and how do i fix it?



    haha oh and don't hate me for my same-line braces :)
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    One potential problem I see is you're using rowCount but you never initialized it to 0.
     
  3. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #3
    Where is your definition of the grid array, and when you say "outside the for loops", is it immediately after or in a totally different part of the code?

    He does at the beginning of the code, outside the loops.
     
  4. themoonisdown09 macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #4
    He only assigned colCount, not rowCount.

    Code:
    int rowCount,colCount=0;
    
     
  5. jbstew32 thread starter macrumors regular

    Joined:
    Jan 15, 2007
    #5
    Code:
    
    	int grid1[grid1Rows][grid1Cols];
    		
    	//create each grid with pixelValue data
    	int i,j;
    	int rowCount,colCount=0;
    	for(i=h0;i<h1;i++) {
    		colCount = 0;
    		for(j=v0;j<v1;j++) {
    			grid1[rowCount][colCount] = pixelValue[i][j];
                            cout << "value: " << grid1[rowCount][colCount] <<endl;
    			colCount++;	
    		}
    		
    		rowCount++;
    	}
    	
    	for(i=0; i<grid1Rows; i++) 
    		for(j=0; j<grid1Cols; j++) 
    			cout << grid1[i][j] <<endl;
    
    

    if I do something like this, the first cout inside the loop will give me correct numbers, but the cout in the bottom set of for loops will spit out 0's.
     
  6. themoonisdown09 macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #6
    How is the pixelValue array being populated? Does it have values in it?
     
  7. jbstew32 thread starter macrumors regular

    Joined:
    Jan 15, 2007
    #7
    It's created from a .pgm image file. I have 1500 other lines of code that use it and it works flawlessly. I've narrowed the problem to this little block of code.

    my grid1 values being assigned are not staying assigned for some reason
     
  8. themoonisdown09 macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #8
    Maybe try to manually populate that array and check to see if it still prints out 0's.

    Code:
    int i,j;
    
    for(i = 0; i < grid1Rows; i++) 
        for(j = 0; j < grid1Cols; j++) 
            grid1[i][j] = i + j;
    
    for(i = 0; i < grid1Rows; i++)
        for(j = 0; j < grid1Cols; j++) 
            cout << grid1[i][j] << endl;
    
     
  9. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #9
    It's already been mentioned, but your problem is rowCount is not initialised to 0.

    b e n
     
  10. jbstew32 thread starter macrumors regular

    Joined:
    Jan 15, 2007
    #10
    hahaha wow.
    Code:
    	int rowCount,colCount;
    	rowCount = 0;
    
    
    that works....I don't get why i had to do that instead of


    Code:
    	int rowCount,colCount = 0;
    
     
  11. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #11
    Why not just do:

    Code:
    int rowCount = 0, colCount = 0;
    ?

    Seems better to me.
     
  12. themoonisdown09 macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #12
    because saying

    Code:
    int rowCount,colCount = 0;
    
    is initializing colCount but not rowCount. You could do this instead...

    Code:
    int rowCount = 0,colCount = 0;
    
    Edit: Cromulent, you beat me to it.
     
  13. jbstew32 thread starter macrumors regular

    Joined:
    Jan 15, 2007
    #13


    exactly!! if i do it the "better" way it doesn't work! I thought that was supposed to initialize both variables to zero...
     
  14. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #14
    Yes it does.
     
  15. themoonisdown09 macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #15
    You might want to re-read Cromulent's post. Both are being initialized there, you're way was only initializing one of them.
     
  16. jbstew32 thread starter macrumors regular

    Joined:
    Jan 15, 2007
    #16
    ohhhh sorry! my mistake guys. I read it too quickly :)


    hmm, who knows how long I've been carrying this misconception about initializing variables....

    ha


    thanks a lot for all the quick response. I was really frustrated at something so simple! :D
     
  17. themoonisdown09 macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #17
    In programming, it's always the simple problems that you end up overlooking.
     
  18. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #18
    The number of times a missing semi colon has had me scratching my head... especially in for loops.
     

Share This Page