#include <stdio.h>
#include <stdlib.h>
void main()
{
int row = 5, col = 3;
int i, j;
double *ar[row]; //will be your 2d array
for(i=0; i<row; i++) //make it into 2d array
ar[i] = malloc(sizeof(int)*col); //get the column number of array
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
ar[i][j] = (i + j) * 1.0; //filling the array element
}
}
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("[%d][%d]=%.2f ", i, j, ar[i][j]); //print the array after filling it
}
printf("\n");
}
//must free the memorry allocation after using it
for(i=0; i<row; i++)
free(ar[i]);
}
result:
[0][0]=0.00 [0][1]=1.00 [0][2]=2.00
[1][0]=1.00 [1][1]=2.00 [1][2]=3.00
[2][0]=2.00 [2][1]=3.00 [2][2]=4.00
[3][0]=3.00 [3][1]=4.00 [3][2]=5.00
[4][0]=4.00 [4][1]=5.00 [4][2]=6.00
No comments