Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Multi-dimensional-array/C/multi-dimensional-array-1.c
Normal file
37
Task/Multi-dimensional-array/C/multi-dimensional-array-1.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*Single dimensional array of integers*/
|
||||
int a[10];
|
||||
|
||||
/*2-dimensional array, also called matrix of floating point numbers.
|
||||
This matrix has 3 rows and 2 columns.*/
|
||||
|
||||
float b[3][2];
|
||||
|
||||
/*3-dimensional array ( Cube ? Cuboid ? Lattice ?) of characters*/
|
||||
|
||||
char c[4][5][6];
|
||||
|
||||
/*4-dimensional array (Hypercube ?) of doubles*/
|
||||
|
||||
double d[6][7][8][9];
|
||||
|
||||
/*Note that the right most number in the [] is required, all the others may be omitted.
|
||||
Thus this is ok : */
|
||||
|
||||
int e[][3];
|
||||
|
||||
/*But this is not*/
|
||||
|
||||
float f[5][4][];
|
||||
|
||||
/*But why bother with all those numbers ? You can also write :*/
|
||||
int *g;
|
||||
|
||||
/*And for a matrix*/
|
||||
float **h;
|
||||
|
||||
/*or if you want to show off*/
|
||||
double **i[];
|
||||
|
||||
/*you get the idea*/
|
||||
|
||||
char **j[][5];
|
||||
27
Task/Multi-dimensional-array/C/multi-dimensional-array-2.c
Normal file
27
Task/Multi-dimensional-array/C/multi-dimensional-array-2.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include<stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int hyperCube[5][4][3][2];
|
||||
|
||||
/*An element is set*/
|
||||
|
||||
hyperCube[4][3][2][1] = 1;
|
||||
|
||||
/*IMPORTANT : C ( and hence C++ and Java and everyone of the family ) arrays are zero based.
|
||||
The above element is thus actually the last element of the hypercube.*/
|
||||
|
||||
/*Now we print out that element*/
|
||||
|
||||
printf("\n%d",hyperCube[4][3][2][1]);
|
||||
|
||||
/*But that's not the only way to get at that element*/
|
||||
printf("\n%d",*(*(*(*(hyperCube + 4) + 3) + 2) + 1));
|
||||
|
||||
/*Yes, I know, it's beautiful*/
|
||||
*(*(*(*(hyperCube+3)+2)+1)) = 3;
|
||||
|
||||
printf("\n%d",hyperCube[3][2][1][0]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
66
Task/Multi-dimensional-array/C/multi-dimensional-array-3.c
Normal file
66
Task/Multi-dimensional-array/C/multi-dimensional-array-3.c
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
/*The stdlib header file is required for the malloc and free functions*/
|
||||
|
||||
int main()
|
||||
{
|
||||
/*Declaring a four fold integer pointer, also called
|
||||
a pointer to a pointer to a pointer to an integer pointer*/
|
||||
|
||||
int**** hyperCube, i,j,k;
|
||||
|
||||
/*We will need i,j,k for the memory allocation*/
|
||||
|
||||
/*First the five lines*/
|
||||
|
||||
hyperCube = (int****)malloc(5*sizeof(int***));
|
||||
|
||||
/*Now the four planes*/
|
||||
|
||||
for(i=0;i<5;i++){
|
||||
hyperCube[i] = (int***)malloc(4*sizeof(int**));
|
||||
|
||||
/*Now the 3 cubes*/
|
||||
|
||||
for(j=0;j<4;j++){
|
||||
hyperCube[i][j] = (int**)malloc(3*sizeof(int*));
|
||||
|
||||
/*Now the 2 hypercubes (?)*/
|
||||
|
||||
for(k=0;k<3;k++){
|
||||
hyperCube[i][j][k] = (int*)malloc(2*sizeof(int));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*All that looping and function calls may seem futile now,
|
||||
but imagine real applications when the dimensions of the dataset are
|
||||
not known beforehand*/
|
||||
|
||||
/*Yes, I just copied the rest from the first program*/
|
||||
|
||||
hyperCube[4][3][2][1] = 1;
|
||||
|
||||
/*IMPORTANT : C ( and hence C++ and Java and everyone of the family ) arrays are zero based.
|
||||
The above element is thus actually the last element of the hypercube.*/
|
||||
|
||||
/*Now we print out that element*/
|
||||
|
||||
printf("\n%d",hyperCube[4][3][2][1]);
|
||||
|
||||
/*But that's not the only way to get at that element*/
|
||||
printf("\n%d",*(*(*(*(hyperCube + 4) + 3) + 2) + 1));
|
||||
|
||||
/*Yes, I know, it's beautiful*/
|
||||
*(*(*(*(hyperCube+3)+2)+1)) = 3;
|
||||
|
||||
printf("\n%d",hyperCube[3][2][1][0]);
|
||||
|
||||
/*Always nice to clean up after you, yes memory is cheap, but C is 45+ years old,
|
||||
and anyways, imagine you are dealing with terabytes of data, or more...*/
|
||||
|
||||
free(hyperCube);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue