Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream>
using namespace std;
int main()
{
	//Allocate a four-dimensional 3x2x4x3x5 array of ints
	int***** ip4array=new int****[3];
	for(int i=0;i<3;++i)
	{
		ip4array[i]=new int***[2];
		for(int j=0;j<2;++j)
		{
			ip4array[i][j]=new int**[4];
			for(int k=0;k<4;++k)
			{
				ip4array[i][j][k]=new int*[3];
				for(int l=0;l<3;l++)
				{
					ip4array[i][j][k][l]=new int[5];
				}
			}
		}

	}
	//fill the array
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<2;++j)
		{
			for(int k=0;k<4;++k)
			{
				for(int l=0;l<3;++l)
				{
					for(int m=0;m<5;++m)
					{
					ip4array[i][j][k][l][m]=i+j+k+l+m;
				}
				}
			}
		}
	}
	//output the array
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<2;++j)
		{
			for(int k=0;k<4;++k)
			{
				for(int l=0;l<3;++l)
				{
					for(int m=0;m<5;++m)
					{
					cout<<ip4array[i][j][k][l][m]<<" ";
					}
					cout<<endl;
				}
				cout<<endl;
			}
			cout<<endl;
		}
		cout<<endl;
	}
	cout<<endl;
	//Deletiing the arrays
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<2;++j)
		{
			for(int k=0;k<4;++k)
			{
				for(int l=0;l<3;++l)
				{
				delete ip4array[i][j][k][l];
			}
				delete ip4array[i][j][k];
			}
			delete ip4array[i][j];
		}
		delete [] ip4array[i];
	}
	delete[] ip4array;
	system("pause");
	return 0;

}


What I have tried:

Program is showing bug and i am not able to have 5-dimensional instead it is creating 8 dimensional please can somebody??????????
Posted
Comments
Sergey Alexandrovich Kryukov 15-Feb-16 17:37pm    
All those arrays are not really 5- or 8-dimensional. :-)
What bug?
—SA
nv3 15-Feb-16 18:32pm    
Looks to me that you are creating a so-called ragged array (google it!). And from first sight it looks your code is correct, except that you should use delete[] in all your loops at the end, not only the j-loop.
Mustafa_ub2016 15-Feb-16 21:01pm    
i started using a five pointer int***** ip5array the base of our array ten i had allocated an array of four pointer int****[3]; and set the five pointer to the point to the beginning of this array.

the next loop allocates a pointer for each of the triple pointer:
i.e; ip5array[i]=new int***[2];
it is followed by array ip5array[i][j]=new int**[2];
then ip5array[i][j][k]=new int*[2];
and finally ip5array[][][][]
nv3 16-Feb-16 3:17am    
Yes, I understand what you did. And what bug are you talking about? We need more precise information in order to help you.
Philippe Mori 15-Feb-16 21:03pm    
Why not use std::vector<std::vector<std::vector<std::vector<std::vector<int>>>>> instead?

Did you try ?
C++
int ip4array[3][2][4][3][5];
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-16 23:03pm    
5ed :-)
—SA
Patrice T 15-Feb-16 23:08pm    
Thank you
Please see my comment to the question. First of all, you could use the debugger, just to learn what's going on, but probably you have some confusion about the concept. This can help you: Multidimensional Arrays — C++ Tutorials.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900