I have an assignment I'm working on which involves creating a PPM image file. I understand how to do the project and it'd take me about 10 minutes to do in PHP. However, I'm new to C++ and I'm trying to understand arrays; more specifically, dynamic multidimensional arrays.
I've created a separate project to experiment with arrays and I was wondering if someone could look through my code and tell me what I've done wrong... When I try to run the following code, the console window crashes.
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int res[2] = { 5, 5 };
int ***a;
**a = new int[res[0]]; // Columns
for (int i = 0; i < res[0]; i++) {
*a[i] = new int[res[1]]; // Rows
for (int j = 0; j < res[1]; j++) {
a[i][j] = new int[3]; // Cell Array
a[i][j][0] = i;
a[i][j][1] = j;
a[i][j][2] = 3;
}
}
for ( int i = 0; i < res[0]; i++ )
{
for ( int j = 0; i < res[1]; i++ )
{
cout << "[ ";
for ( int k = 0; k < 3; k++ )
{
cout << a[i][j][k];
if ( k < 2 )
cout << ", ";
}
cout << " ]\n";
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
Basically, I am trying to figure out how to create an array with a structure similar to:
Array
- Column 1
- - Row 1
- - - Red
- - - Green
- - - Blue
- - Row 2
- - - Red
- - - Green
- - - Blue
- Column 2
- - Row 1
- - - Red
- - - Green
- - - Blue
- - Row 2
- - - Red
- - - Green
- - - Blue
You get the idea... array[0 to MAX_COLUMNS-1][0 to MAX_ROWS-1][0 to 2]
Thanks!
PS: I'm assuming my problem is with my scarce understanding of pointers, but I'm trying to learn! Please be try to be as specific and detailed as possible in your response! Code examples to demonstrate what you're trying to explain are very helpful as I'm a very visual learner.

New Topic/Question
Reply



MultiQuote






|