Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jbstew32

macrumors regular
Original poster
Jan 15, 2007
146
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 :)
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
One potential problem I see is you're using rowCount but you never initialized it to 0.
 

iShater

macrumors 604
Aug 13, 2002
7,027
470
Chicagoland
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?

One potential problem I see is you're using rowCount but you never initialized it to 0.

He does at the beginning of the code, outside the loops.
 

jbstew32

macrumors regular
Original poster
Jan 15, 2007
146
1
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.
 

jbstew32

macrumors regular
Original poster
Jan 15, 2007
146
1
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
 

themoonisdown09

macrumors 601
Nov 19, 2007
4,319
18
Georgia, USA
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

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;
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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 :)

It's already been mentioned, but your problem is rowCount is not initialised to 0.

b e n
 

jbstew32

macrumors regular
Original poster
Jan 15, 2007
146
1
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;
 

jbstew32

macrumors regular
Original poster
Jan 15, 2007
146
1
Why not just do:

Code:
int rowCount = 0, colCount = 0;

?

Seems better to me.



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

jbstew32

macrumors regular
Original poster
Jan 15, 2007
146
1
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.