Data structure---c Implements sequential storage representation of arrays (can be run)

Source: Internet
Author: User

Sequential storage representation of arrays//Xin Yang # include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <time.h># Define OK 1#define ERROR 0#define underflow 2#define max_array_dim 8typedef int status;typedef int elemtype;typedef Struc   t {elemtype *base;     The entity int of the array is dim; Array dimension int *bounds;//According to the following bound should be bounds, the length of the array dimensions int *constants;//array image function constant base address}array;//array initialization status Initarr Ay (Array *a, int Dim,...)     {int elemtotal=1,i;    Va_list ap;    if (Dim < 1 | | Dim > Max_array_dim) Return ERROR;    A->dim = Dim;    A->bounds = (int*) malloc (dim*sizeof (int)); if (!    A->bounds) return ERROR;       Va_start (Ap,dim);        for (i=0;i<dim;i++) {A->bounds[i] = Va_arg (ap,int);        if (a->bounds[i]<0) return underflow;    elemtotal*=a->bounds[i]; } va_end (AP);    A->base = (elemtype*) malloc (elemtotal*sizeof (elemtype)); if (!    A->base) return ERROR;    A->constants= (int*) malloc (dim*sizeof (int.)); if (! A->constants) return ERROR;    A-&GT;CONSTANTS[DIM-1] = 1;    for (i=dim-2;i>=0;--i) a->constants[i]=a->bounds[i+1]*a->constants[i+1]; return OK;}       The destruction of the array status Destroyarray (array *a) {free (a->base);    Free (a->bounds);    Free (a->constants); return OK;}       Locate the address of a status Locate (Array a,va_list ap,int *off) {int ind, I;    *off=0;        for (i=0;i<a.dim;++i) {ind=va_arg (ap,int); if (ind<0| |        Ind>=a.bounds[i]) return ERROR;    *off+=a.constants[i]*ind; } return OK; Assign value to array astatus Assign (array *a,elemtype e,...)    {va_list ap;    Status result;    int i,j,k;    int off;    Va_start (ap,e);    if ((Result=locate (*a,ap,&off)) <0) return result;    * (A->base+off) =e;    Va_end (AP); return OK;} Assigns a value to the element specified in array A estatus value (array a,elemtype *e,...)    {int off;    Status result;    Va_list ap; Va_start (ap,e); if ((Result=locate (A,ap,&off)) <0) return result;*e=* (A.base+off); Va_start (ap,e); return OK;} int main () {inT i,j,k; Array A; Elemtype e; a.dim=3; Initarray (&a,a.dim,2,2,2); printf ("This is a%d-dimensional array!\n", A.dim); printf ("Size of each dimension of the array: \ n"); for (i=0;i<a.dim;i++) printf ("%d Dimension size:%5d\n", i + 1, a.bounds[i]); printf ("Base of function constants: \ n"); for (i=0;i<a.dim;i++) printf ("%d base address:%5d\n", i + 1,a.constants[i]); printf ("\ n Enter any value to continue the program! \ n "); GetChar (); printf ("Randomly generate a set of data: \ n"); Srand (Time (0)); for (i=0;i<a.bounds[0];i++) for (j=0;j<a.bounds[1];j++) for (k=0;k<a.bounds[2];k++) Assign (&a,ra nd ()%100,i,j,k); printf ("The member of Array A is:: \ n");        for (i=0;i<a.bounds[0];i++) {for (j=0;j<a.bounds[1];j++) {printf (");           for (k=0;k<a.bounds[2];k++) {Value (a,&e,i,j,k);           printf ("%5d", e);       } printf (")");   } printf ("\ n"); } printf ("an equivalent one-dimensional array is: \ n"); for (i=0; i<a.bounds[0]*a.bounds[1]*a.bounds[2]; i++) printf ("%5d", A.base[i]); printf ("\ n"); Destroyarray (&a); GetChar (); return 0;}





Data structure---c Implements sequential storage representation of arrays (can be run)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.