Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimensional Array Problem 2

Status
Not open for further replies.

PapaGLP

Technical User
Jul 19, 2002
31
US
I'm trying to create a multidimensional array (temp) that has dimensions of inRows x inColums but i get errors of "Expected constant expression" and "Cannot declare array of constant size 0".

void createIntArray(int inRows, int inColumns){

int temp[inRows][inColumns];

}

I tried setting inRows and inColumns to be constant as follows but to no avail. Please give me a hand creating the temp array.

void createIntArray(int inRows, int inColumns){

const int r(inRows), c(inColumns);
int temp[r][c];

}

Thanks
 
You can dynamically allocate the array:

void createIntArray(int inRows, int inColumns){

int **temp;
temp = new int*[inRows];
for (int j=0; j < inRows; j++) {
temp[j] = new int[inColumns];
}
}

The problem is that normaly arrays need constants as their parameters. Basically if the compiler does not know how big to make the array, then it gives an error. Using a const int as a parameter like you did the second time works, but constants need to be declared with constants (such as 1, 5, etc) to work like that.

-Skatanic
 
You will need to allocate your array dynamically. Take a look at the following thread:

thread116-107726

Vincent
 
For a simple way to allocate dynamically a 2D array take a look at the code below:

#include <iostream.h>
#include <stdlib.h>
typedef int Array[100];
void main()
{
Array *y; // create the array
y = new Array[25]; // allocating memory to it 25 by 100

if( y == NULL ) // test if memory have been allocated
exit(1);

for( int i = 0; i < 25; i++ ) // input the array
{
for( int j = 0; j < 100; j++ )
y[j] = 8;
}

for( i = 0; i < 25; i++ ) // output the array
{
for( int k = 0; k < 100; k++ )
cout<<&quot;[&quot;<<i<<&quot;]&quot;<<&quot;[&quot;<<k<<&quot;] = &quot;<<y[k]<<endl;
}
delete [] y; // freeing the memory that has been allocated to the array
}
 
okay, so here's what i have now to create the array temps . . . now i try to assign the value of each of the array entries to be 1 . . . but when i read out the contents of the array, there are values that aren't 1. it seems to me that it may be pulling values from elsewhere in memory but i'm not sure. Does anyone see any glaring errors in this code?

const int NX(512), NY(512);

int **temps;
temps = new int*[NX];
for (int k=0; k < NX; k++) {
temps[k] = new int[NY];
}

for (int j = 0; j < NX; j++){
for (int i = 0; i < NY; i++){
temps[j] = 1;
}
}

Thanks
 
Hi

You need to access the different elements of the array indivually

for (int j = 0; j < NX; j++)
{
for (int i = 0; i < NY; i++)
{
/* Code modified Here */
temps[j] = 1;
}
}

 
now it should work:

void main()
{
const int NX = 512;
const int NY = 512;
int **temps;

temps = new int*[NX];

for (int j=0; j < NX; j++) {
temps[j] = new int[NY];
}

for( int i = 0; i < NX; i++ )
{
for( j = 0; j < NY; j++ )
{
temps[j] = 1;
cout<<temps[j]<<&quot;, &quot;;//just for verification
}
}
}
 
Or you might also do it like this:

#include <iostream.h>
#include <stdlib.h>
typedef int Array[512];
void main()
{
Array *y; // create the array
y = new Array[512]; // allocating memory to it 512 by 512

if( y == NULL ) // test if memory have been allocated
{
cout<<&quot;memory allocation has fail.&quot;<<endl;
exit(1);
}

for( int i = 0; i < 512; i++ ) // input the array
{
for( int j = 0; j < 512; j++ )
y[j] = 1;
}

for( i = 0; i < 512; i++ ) // output the array
{
for( int k = 0; k < 512; k++ )
cout<<&quot;[&quot;<<i<<&quot;]&quot;<<&quot;[&quot;<<k<<&quot;] = &quot;<<y[k]<<endl;
}
delete [] y; // freeing the memory that has been allocated to the array
}
 
okay . . . all of my checks along the way return that the numbers are actually 1 . . . but here's the array when i view it in the HDF Reader i'm using to check my file output . . .

8144208 8146312 8148416 8150520 8152624 8154728
8156832 8158936 8161040 8163144 8165248 8167352
8169456 8171560 8173664 8175768 8177872 8179976
8182080 8184184 8186288 8188392 8190496 8192600
8194704 8196808 8198912 8201016 8203120 8205224

definitely not 1s and the values all increment by 2104 . . . any clues as to why this may be happening? or is it something in the reader that's causing it?
 
okay . . . all of my checks along the way return that the numbers are actually 1 . . . but here's the array when i view it in the HDF Reader i'm using to check my file output . . .

8144208 8146312 8148416 8150520 8152624 8154728
8156832 8158936 8161040 8163144 8165248 8167352
8169456 8171560 8173664 8175768 8177872 8179976
8182080 8184184 8186288 8188392 8190496 8192600
8194704 8196808 8198912 8201016 8203120 8205224

definitely not 1s and the values all increment by 2104 . . . any clues as to why this may be happening? or is it something in the reader that's causing it?
 
Could you post the code that you are using to output it to the file so we can see if the problem might be there?
 
void WriteHDF::createIntArray(int **buffer, int inRows, int inColumns){

int **temp;
temp = new int*[inRows];
for (int k=0; k < inRows; k++) {
temp[k] = new int[inColumns];
}



for (int j = 0; j < inRows; j++){
for (int i = 0; i < inColumns; i++){
int input = buffer[j];
cout<<input;
temp[j] = input;
}
}

IntType dtInt( PredType::NATIVE_INT );
dtInt.setOrder( H5T_ORDER_LE );

hsize_t dimsf[2];
dimsf[0] = inRows;
dimsf[1] = inColumns;

DataSpace dataspace(2, dimsf);
DataSet dataset = getCurrentGroup().createDataSet(getDatasetName(), dtInt, dataspace );
dataset.write( temp, PredType::NATIVE_INT );

delete temp;

}
 
also, i changed the output method a little and got this

9223056 9223328 9223400 9223472 9223544 -33686019
-572662307 -572662307 589833 524546 9222952 9223296
0 0 24 1 1342 -33686019
1 1 1 1 1 1
-33686019 -572662307 589828 -572719102 3080600 3083152
 
oh yea, and when i do the cout<<input; line, the output is all 1s as expected
 
liebnitz,
went back and tried the second way you posted (typedef Array[512]) and it eliminated the problem. thanks for all the help to everyone. i'm sure i'll be around for more help soon :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top