4D array

S

Stephen Tyndall

Hey. I was attempting a four-dimensional array and I couldn't get it to
work. The code below gave me 23 errors:

// BEGIN CODE
int main() {

int nArray[2][2][2][2] = {

{ { {1,2} , {3,4} } , { {5,6} , {7,8} } }

{ { {9,10} , {11,12} } , { {13,14} , {15,16} } }

};

cout << "nArray[0][0][0][0]: " << nArray[0][0][0][0] << endl;

cout << "nArray[1][0][1][0]: " << nArray[1][0][1][0] << endl;

cout << "nArray[1][1][0][1]: " << nArray[1][1][0][1] << endl;

return 0;

}

// END CODE

I'm fairly certain that the code should work; it was an expansion on a
working 3D array I had just finished, and I've gone over it several times.
I'm pretty sure I'll never need a 4D array, but I'd like to know why this
one doesn't work.
 
J

jota

int nArray[2][2][2][2] = {
{ { {1,2} , {3,4} } , { {5,6} , {7,8} } }, //<-- forgot a comma-sign to
separate the first half and the second half
{ { {9,10} , {11,12} } , { {13,14} , {15,16} } }
//JT
 
T

Thomas Matthews

Stephen said:
Hey. I was attempting a four-dimensional array and I couldn't get it to
work. The code below gave me 23 errors:

// BEGIN CODE
int main() {

int nArray[2][2][2][2] = {

{ { {1,2} , {3,4} } , { {5,6} , {7,8} } }
You are missing a ',' at the end of the above line.
{ { {9,10} , {11,12} } , { {13,14} , {15,16} } }

};

cout << "nArray[0][0][0][0]: " << nArray[0][0][0][0] << endl;

cout << "nArray[1][0][1][0]: " << nArray[1][0][1][0] << endl;

cout << "nArray[1][1][0][1]: " << nArray[1][1][0][1] << endl;

return 0;

}

Here is my code that works:
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ cat junk.cpp
#include <iostream>
using namespace std;

int main() {

int nArray[2][2][2][2] = {

{ { {1, 2} , { 3, 4} } , { { 5, 6} , { 7, 8} } },

{ { {9,10} , {11,12} } , { {13,14} , {15,16} } }

};

cout << "nArray[0][0][0][0]: " << nArray[0][0][0][0] << endl;

cout << "nArray[1][0][1][0]: " << nArray[1][0][1][0] << endl;

cout << "nArray[1][1][0][1]: " << nArray[1][1][0][1] << endl;

return 0;

}


TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ g++ -o junk junk.cpp

TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ ./junk
nArray[0][0][0][0]: 1
nArray[1][0][1][0]: 11
nArray[1][1][0][1]: 14

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
V

Victor Bazarov

Stephen said:

Hey yourself.
I was attempting a four-dimensional array and I couldn't get it to
work. The code below gave me 23 errors:

What 23 errors?
// BEGIN CODE
int main() {

int nArray[2][2][2][2] = {

{ { {1,2} , {3,4} } , { {5,6} , {7,8} } }

{ { {9,10} , {11,12} } , { {13,14} , {15,16} } }

};

cout << "nArray[0][0][0][0]: " << nArray[0][0][0][0] << endl;

It seems that 'cout' and 'endl' are undefined at this point. Are
you sure you didn't forget

#include <iostream>
using std::cout;
using std::endl;

at the beginning of your code?
cout << "nArray[1][0][1][0]: " << nArray[1][0][1][0] << endl;

cout << "nArray[1][1][0][1]: " << nArray[1][1][0][1] << endl;

return 0;

}

// END CODE

I'm fairly certain that the code should work; it was an expansion on a
working 3D array I had just finished, and I've gone over it several times.
I'm pretty sure I'll never need a 4D array, but I'd like to know why this
one doesn't work.

What particularly "doesn't work"?

V
 
S

Stephen Tyndall

The only thing wrong was a comma at the end of the second line of array
code. I only put the code for main() in the original message. At the end
of the message, I wrote that the three-dimensional array I programmed before
worked perfectly fine. That would seem to imply that the problem is with
the array.

Victor Bazarov said:
Stephen said:

Hey yourself.
I was attempting a four-dimensional array and I couldn't get it to
work. The code below gave me 23 errors:

What 23 errors?
// BEGIN CODE
int main() {

int nArray[2][2][2][2] = {

{ { {1,2} , {3,4} } , { {5,6} , {7,8} } }

{ { {9,10} , {11,12} } , { {13,14} , {15,16} } }

};

cout << "nArray[0][0][0][0]: " << nArray[0][0][0][0] << endl;

It seems that 'cout' and 'endl' are undefined at this point. Are
you sure you didn't forget

#include <iostream>
using std::cout;
using std::endl;

at the beginning of your code?
cout << "nArray[1][0][1][0]: " << nArray[1][0][1][0] << endl;

cout << "nArray[1][1][0][1]: " << nArray[1][1][0][1] << endl;

return 0;

}

// END CODE

I'm fairly certain that the code should work; it was an expansion on a
working 3D array I had just finished, and I've gone over it several times.
I'm pretty sure I'll never need a 4D array, but I'd like to know why this
one doesn't work.

What particularly "doesn't work"?

V
 
Joined
Jan 9, 2009
Messages
1
Reaction score
0
This 4D works.
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
int zahlen[5][5][5][5];
srand(time(NULL));
for (register char x=0;x<5;++x)
{
for (register char y=0;y<5;++y)
{
for (register char z=0;z<5;++z)
{
for (register char w=0;w<5;++w)
{
zahlen[x][y][z][w]=rand()%9;
}
}
}
}
for (register char x=0;x<5;++x)
{
for (register char y=0;y<5;++y)
{
for (register char z=0;z<5;++z)
{
for (register char w=0;w<5;++w)
{
cout<<zahlen[x][y][z][w]<<",";
}
cout<<endl;
}
cout<<endl<<endl;
}
cout<<endl<<endl<<endl;
}
getch();
return 0;
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top