View Full Version : Arrays with C++
jbstew32
Nov 14, 2008, 01:09 PM
I'm writing a program that does image manipulation. I have a block of code that looks like this:
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
Nov 14, 2008, 02:08 PM
One potential problem I see is you're using rowCount but you never initialized it to 0.
iShater
Nov 14, 2008, 02:19 PM
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.
themoonisdown09
Nov 14, 2008, 02:23 PM
He does at the beginning of the code, outside the loops.
He only assigned colCount, not rowCount.
int rowCount,colCount=0;
jbstew32
Nov 14, 2008, 02:26 PM
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.
themoonisdown09
Nov 14, 2008, 02:28 PM
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.
How is the pixelValue array being populated? Does it have values in it?
jbstew32
Nov 14, 2008, 03:07 PM
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
Nov 14, 2008, 03:11 PM
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.
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
Nov 14, 2008, 03:30 PM
I'm writing a program that does image manipulation. I have a block of code that looks like this:
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
Nov 14, 2008, 03:51 PM
hahaha wow.
int rowCount,colCount;
rowCount = 0;
that works....I don't get why i had to do that instead of
int rowCount,colCount = 0;
Cromulent
Nov 14, 2008, 03:52 PM
Why not just do:
int rowCount = 0, colCount = 0;
?
Seems better to me.
themoonisdown09
Nov 14, 2008, 03:53 PM
hahaha wow.
that works....I don't get why i had to do that instead of
because saying
int rowCount,colCount = 0;
is initializing colCount but not rowCount. You could do this instead...
int rowCount = 0,colCount = 0;
Edit: Cromulent, you beat me to it.
jbstew32
Nov 14, 2008, 03:53 PM
Why not just do:
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...
Cromulent
Nov 14, 2008, 03:54 PM
exactly!! if i do it the "better" way it doesn't work!
Yes it does.
themoonisdown09
Nov 14, 2008, 03:55 PM
exactly!! if i do it the "better" way it doesn't work! I thought that was supposed to initialize both variables to zero...
You might want to re-read Cromulent's post. Both are being initialized there, you're way was only initializing one of them.
jbstew32
Nov 14, 2008, 03:58 PM
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
themoonisdown09
Nov 14, 2008, 04:00 PM
thanks a lot for all the quick response. I was really frustrated at something so simple! :D
In programming, it's always the simple problems that you end up overlooking.
Cromulent
Nov 14, 2008, 04:00 PM
It's always the simple problems that you end up overlooking.
The number of times a missing semi colon has had me scratching my head... especially in for loops.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.