Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
|
|
@ -1,37 +0,0 @@
|
|||
/*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];
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
#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;
|
||||
}
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
#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;
|
||||
}
|
||||
31
Task/Multi-dimensional-array/C/multi-dimensional-array.c
Normal file
31
Task/Multi-dimensional-array/C/multi-dimensional-array.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* an array of ten ints */
|
||||
int a[10];
|
||||
|
||||
/* a 2D-array of floats with three rows and two columns */
|
||||
float b[3][2];
|
||||
/*
|
||||
these would be ordered in memory as
|
||||
b[0][0] b[0][1] b[1][0] b[1][1] b[2][0] b[2][1]
|
||||
|
||||
for example:
|
||||
*/
|
||||
b[0][0] = 1.0;
|
||||
b[0][1] = 2.0;
|
||||
b[1][0] = 3.0;
|
||||
b[1][1] = 4.0;
|
||||
b[2][0] = 5.0;
|
||||
b[2][1] = 6.0;
|
||||
/*
|
||||
now these would be stored in memory as:
|
||||
|
||||
+----+----+----+----+----+----+
|
||||
| 1.0| 2.0| 3.0| 4.0| 5.0| 6.0|
|
||||
+----+----+----+----+----+----+
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/* a 3D-array of chars */
|
||||
char c[4][5][6];
|
||||
|
||||
/* etc. */
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
// supports multi-dimensional arrays
|
||||
// support row-major and column major order - we can use both on different arrays.
|
||||
// this is variant type
|
||||
Dim A(0 to 4, 0 to 3, 1 to 2, -1 to 1) = 1
|
||||
A(0,0,1,-1)++
|
||||
Print A(0,0,1,-1)=2
|
||||
// Arrays are values also
|
||||
// here we pass a tuple (one dimension array 9 based)
|
||||
A(4,3,2,1)=(1,2,3,4,5)
|
||||
Print A(4,3,2,1)(2)=3
|
||||
Dim Z(10)
|
||||
Z(3)=A()
|
||||
Print Z(3)(4,3,2,1)(2)=3
|
||||
|
||||
// We can define type: BigInteger, Complex, Decimal, Currency, Double, Single, Long Long, Long, Byte, Date, Boolean, Object
|
||||
Dim B(0 to 4, 0 to 3, 1 to 2, -1 to 1) as byte = 255
|
||||
B(0,0,1,-1)--
|
||||
Print B(0,0,1,-1)=254
|
||||
// redim - by default is row-major
|
||||
Dim B(0 to 5, 0 to 3, 1 to 2, -1 to 1)
|
||||
Print B(0,0,1,-1)=254
|
||||
// OLE type are column major
|
||||
Dim OLE B(0 to 4, 0 to 3, 1 to 2, -1 to 1) as byte = 255
|
||||
B(0,0,1,-1)--
|
||||
// redim the last dimension only
|
||||
Dim B(0 to 4, 0 to 3, 1 to 2, -1 to 5)
|
||||
Print B(0,0,1,-1)=254
|
||||
B(4,3,2,5)=253
|
||||
Print Dimension(B())=4, Dimension(B(),4,1)=5
|
||||
Print B()#pos(253)=279 ' Zero position (trait like one dimension)
|
||||
// we can redim free, but the items change positions..
|
||||
k=len(B())
|
||||
' one dimension
|
||||
DIM B(K)
|
||||
Print B(279)=253, type$(B(279))="Byte"
|
||||
// we can get the actual address
|
||||
Print Varptr(B(279))-Varptr(B(278))=1
|
||||
// another type of arrays
|
||||
// there is no dim, we set index and we get resize
|
||||
Byte z[10]=255
|
||||
d=lambda->{
|
||||
object d[number]
|
||||
= d
|
||||
}
|
||||
object P[2]=d(0)
|
||||
p[2]=d(10)
|
||||
p[1]=d(3)
|
||||
P[2][1]=z // we get the pointer
|
||||
P[2][2]=z[] // we get the copy
|
||||
P[1][1]=z // we get the pointer
|
||||
P[1][2]=z[] // we get the copy
|
||||
p[2][2][2]-=10
|
||||
? p[2][2][2]=245
|
||||
? p[1][2][2]=255
|
||||
DEF TypeVal(x)=type$(x)
|
||||
// these are the Seven arrays (two of them are z):
|
||||
Print len(p[0])=1, type$(p, 0)="RefArray"
|
||||
Print len(p[1])=4, type$(p, 1)="RefArray"
|
||||
Print len(p[2])=11, type$(p, 2)="RefArray"
|
||||
Print TypeVal(p[1][1])="RefArray"
|
||||
Print p[1][1] is z
|
||||
Print TypeVal(p[1][2])="RefArray"
|
||||
Print len(p[1][2])=11, TypeVal(p[1][2][0])="Byte"
|
||||
Print TypeVal(p[2][1])="RefArray"
|
||||
Print p[2][1] is z
|
||||
Print p[1][1] is p[2][1]
|
||||
Print TypeVal(p[2][2])="RefArray"
|
||||
Print not p[1][1] is p[2][2]
|
||||
z[6]-=100
|
||||
Print p[1][1][6]=z[6], p[2][1][6]=z[6]
|
||||
Print len(p[2][2])=11, TypeVal(p[2][2][0])="Byte"
|
||||
byte k[0]
|
||||
// shallow copy
|
||||
k=p[]
|
||||
Print k[2][1] is z
|
||||
// copy
|
||||
k[2][1]=k[2][]
|
||||
// so now array at k[2][1] is a copy, different pointer from z
|
||||
Print not k[2][1] is z
|
||||
|
||||
// Sparse Matrix using a list (has a hash table)
|
||||
g=list:= 1:=100, 10:=300, 500:=40
|
||||
if exist(g, 10) then print eval(g)=300
|
||||
Print g(1)=100, g(10)=300, g(500)=40
|
||||
Print valid(g(20)) = false
|
||||
Print valid(g(10)) = true
|
||||
Append g, 400:=1000
|
||||
// this is a quicksort
|
||||
Sort ascending g as number
|
||||
// Sparse Matrix using a Queue (a list taking same keys)
|
||||
t=queue:=2,3,4,4,5,10:="A",10:="C",10:="B", 3
|
||||
// this is a stable sort
|
||||
sort t as number
|
||||
// Access same keys using the hash table
|
||||
if exist(t, 10) then
|
||||
many=exist(t, 10, 0)
|
||||
for i=1 to many
|
||||
if exist(t, 10, i) then print eval$(t), eval(t!) ' value and position
|
||||
next
|
||||
end if
|
||||
Loading…
Add table
Add a link
Reference in a new issue