So what ever did you enter?
Jim
21 Replies - 1493 Views - Last Post: 05 August 2012 - 12:44 PM
#16
Re: Understanding Dynamic Multidimensional Arrays
Posted 05 August 2012 - 08:44 AM
#17
Re: Understanding Dynamic Multidimensional Arrays
Posted 05 August 2012 - 08:47 AM
jimblumberg,
I entered an integer value such as 10...
Thanks
I entered an integer value such as 10...
Thanks
#18
Re: Understanding Dynamic Multidimensional Arrays
Posted 05 August 2012 - 08:53 AM
So with an array with defined as yourArray[256][256][3] you try to access element yourArray[255][0][10]?
Jim
Jim
#19
Re: Understanding Dynamic Multidimensional Arrays
Posted 05 August 2012 - 11:25 AM
No. He's accessing 0..2 as the last index:
So row == 255, col == 0, color ranges from 0 to 2.
for ( int color = 0; color < 3; color++ )
{
if ( DEBUG )
{
cout << " - " << row << " - " << col << " - " << color << " - " << pixMap[row][col][color];
if ( color < 2 )
cout << " ";
}
So row == 255, col == 0, color ranges from 0 to 2.
#20
Re: Understanding Dynamic Multidimensional Arrays
Posted 05 August 2012 - 11:34 AM
The problem is due to:
The allocated memory never make it out of the function. The function should be:
Or
Or
void drawRectangle( int x, int y, int width, int height, int ***pixMap, int rgb[] )
{
pixMap = new int **[resolution[imgHeight]]; // Rows
for (int row = resolution[imgHeight] - 1; row >= 0 ; row--)
{
pixMap[row] = new int *[resolution[imgWidth]]; // Cols
for (int col = 0; col < resolution[imgWidth]; col++)
{
pixMap[row][col] = new int [3]; // Cell Array
pixMap[row][col][red] = rgb[red];
pixMap[row][col][green] = rgb[green];
pixMap[row][col][blue] = rgb[blue];
}
}
}
The allocated memory never make it out of the function. The function should be:
void drawRectangle( int x, int y, int width, int height, int ****pixMap, int rgb[] );
Or
void drawRectangle( int x, int y, int width, int height, int *** & pixMap, int rgb[] );
Or
int *** drawRectangle( int x, int y, int width, int height, int rgb[] );
#21
Re: Understanding Dynamic Multidimensional Arrays
Posted 05 August 2012 - 11:42 AM
Ahh, but I see that your professor has given you predefined function names... Does that mean that he also predefined the parameters as well?
If you are stuck with the function name and parameters, then you'll have 2 options:
Option 1: Stick a copy of the pixMap pointer into a global. Yuck!
Option 2: Do the allocation for pixMap in main() prior to passing to drawRectangle() and writeToPPM().
DevonZ, on 05 August 2012 - 08:00 AM, said:
However, my professor still makes us structure our programs with pre-defined function names that he's made up and the main goal of this assignment is to gain experience with multidimensional arrays. Hopefully though, as things progress, we're given more flexibility.
If you are stuck with the function name and parameters, then you'll have 2 options:
Option 1: Stick a copy of the pixMap pointer into a global. Yuck!
Option 2: Do the allocation for pixMap in main() prior to passing to drawRectangle() and writeToPPM().
#22
Re: Understanding Dynamic Multidimensional Arrays
Posted 05 August 2012 - 12:44 PM
jimblumberg, on 05 August 2012 - 11:53 AM, said:
So with an array with defined as yourArray[256][256][3] you try to access element yourArray[255][0][10]?
Jim
Jim
jimblumblerg,
Sorry, I was not at my computer when I posted those replies. I was thinking of a different line or something... It did in fact return - 255 - 0 -.
Skydiver,
No, the professor does not define the functions - he simply states the function names that we're to use and what purpose each function is to serve.
Passing the pixMap array by reference (your second example) is what I went with and it worked great.
Thank you both, and sorry for the confusion Jim!
UPDATE/EDIT
Grr, why can't I type anything properly today?
The output was - 255 - 0 - 0 -
|
|

New Topic/Question
Reply




MultiQuote



|