Understanding Dynamic Multidimensional Arrays

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 3983 Views - Last Post: 05 August 2012 - 12:44 PM Rate Topic: -----

#16 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,226
  • Joined: 25-December 09

Re: Understanding Dynamic Multidimensional Arrays

Posted 05 August 2012 - 08:44 AM

So what ever did you enter?

Jim
Was This Post Helpful? 0
  • +
  • -

#17 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: Understanding Dynamic Multidimensional Arrays

Posted 05 August 2012 - 08:47 AM

jimblumberg,

I entered an integer value such as 10...

Thanks
Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,226
  • Joined: 25-December 09

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
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3643
  • View blog
  • Posts: 11,405
  • Joined: 05-May 12

Re: Understanding Dynamic Multidimensional Arrays

Posted 05 August 2012 - 11:25 AM

No. He's accessing 0..2 as the last index:
				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.
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3643
  • View blog
  • Posts: 11,405
  • Joined: 05-May 12

Re: Understanding Dynamic Multidimensional Arrays

Posted 05 August 2012 - 11:34 AM

The problem is due to:
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[] );


Was This Post Helpful? 0
  • +
  • -

#21 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3643
  • View blog
  • Posts: 11,405
  • Joined: 05-May 12

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?

View PostDevonZ, 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().
Was This Post Helpful? 0
  • +
  • -

#22 DevonZ  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 73
  • Joined: 04-August 12

Re: Understanding Dynamic Multidimensional Arrays

Posted 05 August 2012 - 12:44 PM

View Postjimblumberg, 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

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 -
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2