Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C

Idiom #26 Create a 2-dimensional array

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

const int m = 2;
const int n = 3;
double x[m][n];

This works when the values of m and n are known at compile time.
#include <stdlib.h>
double **x=malloc(m*sizeof(double *));
int i;
for(i=0;i<m;i++)
	x[i]=malloc(n*sizeof(double));

This uses dynamic allocation.
X : array (1 .. M, 1 .. N) of Float := (others => (others => 1.0));

New implementation...