Logo

Programming-Idioms

History of Idiom 27 > diff from v12 to v13

Edit summary for version 13 by :

Version 12

2015-09-04, 21:49:40

Version 13

2015-10-29, 14:05:13

Idiom #27 Create a 3-dimensional array

Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.

Idiom #27 Create a 3-dimensional array

Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.

Code
x = [ [ [ k**(i/j) | k<-[1..p] ] | j<-[1..n] ] | i<-[1..m] ]
Code
x = [ [ [ k**(i/j) | k<-[1..p] ] | j<-[1..n] ] | i<-[1..m] ]
Imports
#include <stdlib.h>
Imports
#include <stdlib.h>
Code
double ***x=malloc(m*sizeof(double *));
int i,j;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double));
	for(j=0;j<n;j++)
	{
		x[i][j]=malloc(p*sizeof(double));
	}
}
Code
double ***x=malloc(m*sizeof(double *));
int i,j;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double));
	for(j=0;j<n;j++)
	{
		x[i][j]=malloc(p*sizeof(double));
	}
}
Comments bubble
Uses dynamic allocation.

If the values of m and n are known at compile time you can also use:

double x[m][n][p];
Comments bubble
Uses dynamic allocation.

If the values of m and n are known at compile time you can also use:

double x[m][n][p];
Code
x = [[[0 for k in xrange(p)] for j in xrange(n)] for i in xrange(m)]
Code
x = [[[0 for k in xrange(p)] for j in xrange(n)] for i in xrange(m)]
Origin
http://stackoverflow.com/a/10668448/871134
Origin
http://stackoverflow.com/a/10668448/871134
Code
double[][][] x = new double[m][n][p];
Code
double[][][] x = new double[m][n][p];
Comments bubble
Initial values are 0.0
m, n, p need not be known at compile time
Comments bubble
Initial values are 0.0
m, n, p need not be known at compile time
Demo URL
http://ideone.com/0RjsYb
Demo URL
http://ideone.com/0RjsYb