Logo

Programming-Idioms

History of Idiom 26 > diff from v24 to v25

Edit summary for version 25 by :
Restored version 23

Version 24

2016-02-18, 16:57:58

Version 25

2016-02-18, 17:21:40

Idiom #26 Create a 2-dimensional array

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

Idiom #26 Create a 2-dimensional array

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

Code
X : array (1 .. M, 1 .. N) of Float := (others => (others => 1.0));
Code
X = [{R * 1.0, C * 1.0} || R <- lists:seq(1, M), C <- lists:seq(1, N)].
Code
X = [{R * 1.0, C * 1.0} || R <- lists:seq(1, M), C <- lists:seq(1, N)].
Comments bubble
You don't declare and initialize in Erlang. This is the closest thing.
Comments bubble
You don't declare and initialize in Erlang. This is the closest thing.
Doc URL
http://learnyousomeerlang.com/starting-out-for-real#list-comprehensions
Doc URL
http://learnyousomeerlang.com/starting-out-for-real#list-comprehensions
Demo URL
http://tryerl.seriyps.ru/#id=02f3
Demo URL
http://tryerl.seriyps.ru/#id=02f3
Imports
#include <stdlib.h>
Imports
#include <stdlib.h>
Code
double **x=malloc(m*sizeof(double *));
int i;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double));
}
Code
double **x=malloc(m*sizeof(double *));
int i;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double));
}
Comments bubble
This uses dynamic allocation.
Comments bubble
This uses dynamic allocation.
Code
const m, n = 2, 3
var x [m][n]float64
Code
const m, n = 2, 3
var x [m][n]float64
Comments bubble
m, n must be constant for this syntax to be valid.
Here x is of type [2][3]float64, it is not a slice.
Comments bubble
m, n must be constant for this syntax to be valid.
Here x is of type [2][3]float64, it is not a slice.
Demo URL
http://play.golang.org/p/cAqJGf9q1y
Demo URL
http://play.golang.org/p/cAqJGf9q1y