Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,20 +1,116 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int user1 = 0, user2 = 0;
|
||||
int *a1, **array, row;
|
||||
/*
|
||||
assume this file is c.c ,
|
||||
compile and run on linux: cc -Wall -g -DCOMPILE_EXAMPLE c.c -lm -o c && ./c
|
||||
*/
|
||||
|
||||
printf("Enter two integers. Space delimited, please: ");
|
||||
scanf("%d %d",&user1, &user2);
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
a1 = malloc(user1*user2*sizeof(int));
|
||||
array = malloc(user2*sizeof(int*));
|
||||
for (row=0; row<user1; row++) array[row]=a1+row*user2;
|
||||
|
||||
array[user1/2][user2/2] = user1 + user2;
|
||||
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
|
||||
free(array);
|
||||
free(a1);
|
||||
return 0;
|
||||
static void error(int status, char*message) {
|
||||
fprintf(stderr,"\n%s\n",message);
|
||||
exit(status);
|
||||
}
|
||||
|
||||
static void*dwlcalloc(int n,size_t bytes) {
|
||||
void*rv = (void*)calloc(n,bytes);
|
||||
if (NULL == rv)
|
||||
error(1, "memory allocation failure");
|
||||
return rv;
|
||||
}
|
||||
|
||||
void*allocarray(size_t rank,size_t*shape,size_t itemSize) {
|
||||
/*
|
||||
Allocates arbitrary dimensional arrays (and inits all pointers)
|
||||
with only 1 call to malloc. Lambert Electronics, USA, NY.
|
||||
This is wonderful because one only need call free once to deallocate
|
||||
the space. Special routines for each size array are not need for
|
||||
allocation of for deallocation. Also calls to malloc might be expensive
|
||||
because they might have to place operating system requests. One call
|
||||
seems optimal.
|
||||
*/
|
||||
size_t size,i,j,dataSpace,pointerSpace,pointers,nextLevelIncrement;
|
||||
char*memory,*pc,*nextpc;
|
||||
if (rank < 2) {
|
||||
if (rank < 0)
|
||||
error(1,"invalid negative rank argument passed to allocarray");
|
||||
size = rank < 1 ? 1 : *shape;
|
||||
return dwlcalloc(size,itemSize);
|
||||
}
|
||||
pointerSpace = 0, dataSpace = 1;
|
||||
for (i = 0; i < rank-1; ++i)
|
||||
pointerSpace += (dataSpace *= shape[i]);
|
||||
pointerSpace *= sizeof(char*);
|
||||
dataSpace *= shape[i]*itemSize;
|
||||
memory = pc = dwlcalloc(1,pointerSpace+dataSpace);
|
||||
pointers = 1;
|
||||
for (i = 0; i < rank-2; ) {
|
||||
nextpc = pc + (pointers *= shape[i])*sizeof(char*);
|
||||
nextLevelIncrement = shape[++i]*sizeof(char*);
|
||||
for (j = 0; j < pointers; ++j)
|
||||
*((char**)pc) = nextpc, pc+=sizeof(char*), nextpc += nextLevelIncrement;
|
||||
}
|
||||
nextpc = pc + (pointers *= shape[i])*sizeof(char*);
|
||||
nextLevelIncrement = shape[++i]*itemSize;
|
||||
for (j = 0; j < pointers; ++j)
|
||||
*((char**)pc) = nextpc, pc+=sizeof(char*), nextpc += nextLevelIncrement;
|
||||
return memory;
|
||||
}
|
||||
|
||||
#ifdef COMPILE_EXAMPLE
|
||||
|
||||
#include<string.h>
|
||||
#include<math.h>
|
||||
|
||||
#define Z 5
|
||||
#define Y 10
|
||||
#define X 39
|
||||
|
||||
#define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))
|
||||
|
||||
void p_char(void*pv) {
|
||||
char s[3];
|
||||
int i = 0;
|
||||
s[i++] = ' ', s[i++] = *(char*)pv, s[i++] = 0;
|
||||
fputs(s, stdout);
|
||||
}
|
||||
|
||||
void display(void*a,size_t rank,size_t*shape,void(*f)(void*)) {
|
||||
int i;
|
||||
if (0 == rank)
|
||||
(*f)(a);
|
||||
else if (1 == rank) {
|
||||
for (i = 0; i < *shape; ++i)
|
||||
(*f)(a+i);
|
||||
putchar('\n');
|
||||
} else {
|
||||
for (i = 0; i < *shape; ++i)
|
||||
display(((void**)a)[i], rank-1, shape+1, f);
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
int main() { /* character cell 3D graphics. Whoot */
|
||||
char***array;
|
||||
float x,y,z;
|
||||
size_t rank, shape[3], i, j, k;
|
||||
rank = 0;
|
||||
shape[rank++] = Z, shape[rank++] = Y, shape[rank++] = X;
|
||||
array = allocarray(rank, shape, sizeof(char));
|
||||
memset(**array, ' ', X*Y*Z*(sizeof(***array))); /* load the array with spaces */
|
||||
for (i = 0; i < X; ++i) {
|
||||
x = i/(float)X;
|
||||
for (j = 0; j < Y; ++j) {
|
||||
y = j/(float)X;
|
||||
z = x*y*(4*M_PI);
|
||||
z = 5.2*(0.5+(0.276765 - sin(z)*cos(z)*exp(1-z))/0.844087); /* a somewhat carefully designed silly function */
|
||||
/* printf("%g %g %g\n", x, y, z); */
|
||||
k = (int)z;
|
||||
array[BIND(k, 0, Z-1)][j][i] = '@'; /* BIND ensures a valid index */
|
||||
}
|
||||
}
|
||||
display(array, rank, shape, p_char);
|
||||
puts("\nIt is what it is.");
|
||||
free(array);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -2,26 +2,18 @@
|
|||
#include <stdlib.h>
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int user1 = 0;
|
||||
int space_needed;
|
||||
int *a1, **array;
|
||||
int row, col;
|
||||
int user1 = 0, user2 = 0;
|
||||
int *a1, **array, row;
|
||||
|
||||
printf("Enter size of array: ");
|
||||
scanf("%d",&user1);
|
||||
printf("Enter two integers. Space delimited, please: ");
|
||||
scanf("%d %d",&user1, &user2);
|
||||
|
||||
space_needed = (user1+1)*user1/2;
|
||||
a1 = malloc(space_needed);
|
||||
a1 = malloc(user1*user2*sizeof(int));
|
||||
array = malloc(user1*sizeof(int*));
|
||||
for (row=0,offset=0; row<user1; offset+=(user1-row), row++) {
|
||||
array[row]=a1+offset-row;
|
||||
for (col=row; col<user1; col++)
|
||||
array[row][col] = 1+col-row;
|
||||
}
|
||||
for (row=0; row<user1; row++)
|
||||
printf("%d ", array[row][user1-1]);
|
||||
printf("\n");
|
||||
for (row=0; row<user1; row++) array[row]=a1+row*user2;
|
||||
|
||||
array[user1/2][user2/2] = user1 + user2;
|
||||
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
|
||||
free(array);
|
||||
free(a1);
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -1,18 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <alloca.h>
|
||||
#include <stdlib.h>
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int user1 = 0, user2 = 0;
|
||||
int *a1, **array, row;
|
||||
int user1 = 0;
|
||||
int space_needed;
|
||||
int *a1, **array;
|
||||
int row, col, offset;
|
||||
|
||||
printf("Enter two integers. Space delimited, please: ");
|
||||
scanf("%d %d",&user1, &user2);
|
||||
printf("Enter size of array: ");
|
||||
scanf("%d",&user1);
|
||||
|
||||
a1 = alloca(user1*user2*sizeof(int));
|
||||
array = alloca(user1*sizeof(int*));
|
||||
for (row=0; row<user1; row++) array[row]=a1+row*user2;
|
||||
space_needed = (user1+1)*user1/2;
|
||||
a1 = malloc(space_needed * sizeof(*a1));
|
||||
array = malloc(user1 * sizeof(*array));
|
||||
for (row=0,offset=0; row<user1; offset+=(user1-row), row++) {
|
||||
array[row]=a1+offset-row;
|
||||
for (col=row; col<user1; col++)
|
||||
array[row][col] = 1+col-row;
|
||||
}
|
||||
for (row=0; row<user1; row++)
|
||||
printf("%d ", array[row][user1-1]);
|
||||
printf("\n");
|
||||
|
||||
array[user1/2][user2/2] = user1 + user2;
|
||||
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
|
||||
free(array);
|
||||
free(a1);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
#include <alloca.h>
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int user1 = 0, user2 = 0;
|
||||
int *a1, **array, row;
|
||||
|
||||
printf("Enter two integers. Space delimited, please: ");
|
||||
scanf("%d %d",&user1, &user2);
|
||||
|
||||
a1 = alloca(user1*user2*sizeof(int));
|
||||
array = alloca(user1*sizeof(int*));
|
||||
for (row=0; row<user1; row++) array[row]=a1+row*user2;
|
||||
|
||||
array[user1/2][user2/2] = user1 + user2;
|
||||
printf("array[%d][%d] is %d\n",user1/2,user2/2,array[user1/2][user2/2]);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
my ($major,$minor) = prompt("Dimensions? ").comb(/\d+/);
|
||||
my @array := [ for ^$major { [ for ^$minor {'@'} ] } ];
|
||||
@array[ pick 1, ^$major ][ pick 1, ^$minor ] = ' ';
|
||||
my @array = [ '@' xx $minor ] xx $major;
|
||||
@array[ *.rand ][ *.rand ] = ' ';
|
||||
.say for @array;
|
||||
|
|
|
|||
|
|
@ -1,39 +1,33 @@
|
|||
/*REXX program to allocate/populate/display a two-dimensional array. */
|
||||
call bloat
|
||||
/*no more array named A. at this point.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*REXX program allocates/populates/displays a two-dimensional array. */
|
||||
call bloat /*the BLOAT procedure does all allocations.*/
|
||||
/*no more array named @ at this point. */
|
||||
exit /*stick a fork in it, we're all done honey.*/
|
||||
/*─────────────────────────BLOAT subroutine─────────────────────────────*/
|
||||
bloat: procedure; say
|
||||
bloat: procedure; say /*"PROCEDURE" makes this a ··· procedure. */
|
||||
say 'Enter two positive integers (a 2-dimensional array will be created).'
|
||||
pull n m . /*there is no way to pre-allocate an array,*/
|
||||
/*REXX will allocate each element when it's*/
|
||||
/*assigned. */
|
||||
|
||||
a.='~' /*default value for all elements so far. */
|
||||
/*this ensures every element has a value. */
|
||||
do j =1 for n
|
||||
do k=1 for m
|
||||
if random()//7==0 then a.j.k=j'_'k /*populate every 7th random*/
|
||||
pull n m . /*elements are allocated as they're defined*/
|
||||
/*N and M should be verified at this point.*/
|
||||
@.=' · ' /*Initial value for all @ array elements,*/
|
||||
/*this ensures every element has a value.*/
|
||||
do j =1 for n /*traipse through the first dimension [N]*/
|
||||
do k=1 for m /* " " " second " [M]*/
|
||||
if random()//7==0 then @.j.k=j'~'k /*populate every 7th random*/
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
|
||||
do r=1 for n /*display array to the console (row,col). */
|
||||
_=
|
||||
do c=1 for m
|
||||
_=_ right(a.r.c,4) /*display one row at a time, align the vals*/
|
||||
end /*kk*/
|
||||
say _
|
||||
end /*jj*/
|
||||
/*when the RETURN is executed (from a */
|
||||
/*PROCEDURE in this case), the array A. is*/
|
||||
/*"de-allocated", that is, it's no longer */
|
||||
/*defined, and the memory that the array */
|
||||
/*has is now free for other REXX variables.*/
|
||||
|
||||
/*If the BLOAT subroutine didn't have */
|
||||
/*a "PROCEDURE" on that statement, the */
|
||||
/*array "a." would be left intact. */
|
||||
|
||||
/*The same effect is performed by a DROP. */
|
||||
drop a. /*because of the PROCEDURE statement, */
|
||||
return /* the DROP statement is superfluous. */
|
||||
end /*j*/
|
||||
/* [↓] display array to console: row,col */
|
||||
do r=1 for n; _= /*construct one row (or line) at a time. */
|
||||
do c=1 for m /*construct row one column at a time. */
|
||||
_=_ right(@.r.c,4) /*append a nice-aligned column to the line.*/
|
||||
end /*kk*/ /* [↑] an nicely aligned line is built. */
|
||||
say _ /*display one row at a time to the terminal*/
|
||||
end /*jj*/
|
||||
/*╔════════════════════════════════════════════════════════════════════╗
|
||||
║ When the RETURN is executed (from a PROCEDURE in this case), and ║
|
||||
║ array @ is "de─allocated", that is, it's no longer defined, and ║
|
||||
║ the array's storage is now free for other REXX variables. If the ║
|
||||
║ BLOAT subroutine didn't have a "PROCEDURE" on that statement,║
|
||||
║ the array @ would've been left intact. The same effect is ║
|
||||
║ performed by a DROP statement (an example is shown below). ║
|
||||
╚════════════════════════════════════════════════════════════════════╝*/
|
||||
drop @. /*because of the PROCEDURE statement, the*/
|
||||
return /* [↑] DROP statement is superfluous. */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
" Create a two-dimensional array with r rows and c columns.
|
||||
" The optional third argument specifies the initial value
|
||||
" (default is 0).
|
||||
function MakeArray(r, c, ...)
|
||||
if a:0
|
||||
let init = a:1
|
||||
else
|
||||
let init = 0
|
||||
endif
|
||||
|
||||
let temp = []
|
||||
for c in range(a:c)
|
||||
call add(temp, init)
|
||||
endfor
|
||||
|
||||
let array = []
|
||||
for r in range(a:r)
|
||||
call add(array, temp[:])
|
||||
endfor
|
||||
return array
|
||||
endfunction
|
||||
|
||||
let rows = input("Enter number of rows: ")
|
||||
let cols = input("Enter number of columns: ")
|
||||
echo "\n"
|
||||
let array = MakeArray(rows, cols)
|
||||
let array[rows - 1][cols - 1] = rows * cols
|
||||
echo array[rows - 1][cols - 1]
|
||||
unlet array
|
||||
Loading…
Add table
Add a link
Reference in a new issue