home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / cpluspls / vir_v203.zip / ARRAYS.CPP < prev    next >
C/C++ Source or Header  |  1992-07-11  |  6KB  |  262 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <alloc.h>
  5. #include "..\lib\xarray.h"
  6.  
  7.  
  8. /*
  9.     Define a class called anything as an example of caching arrays of a
  10.     class
  11. */
  12.  
  13. struct anything
  14. {
  15.     int colour_of_socks;
  16.     char name[10];
  17.     int age;
  18.     float cash;
  19. };
  20.  
  21.  
  22. /*
  23.     XARRAY <class or type>
  24.     is the way to make a class or type an extended array.
  25.  
  26.     The letter x is prefixed to the type of class
  27.  
  28.     Therefore class 'anything' becomes 'xanything'
  29. */
  30.  
  31. XARRAY (anything);
  32.  
  33. #define any 30000
  34. void do_anything()
  35. {
  36.     xanything me(any);
  37.     int i;
  38.     printf ("Anything is a class of size %d bytes\n",sizeof (anything));
  39.     printf ("The following test is on an array of anything\n");
  40.     printf ("The array has %ld elements\n", (long) any);
  41.     printf ("Press any key to begin the test\n");
  42.     getch();
  43.     start_timer();
  44.     printf ("Making Anything\n");
  45.     for (i=0;i<=any;i++)
  46.     {
  47.         me[i].colour_of_socks=i;
  48.         sprintf (me[i].name,"%d",i);
  49.         me[i].age=2*i;
  50.         me[i].cash=5.5*(float)i;
  51.     }
  52.     printf ("Checking anything\n");
  53.     for (i=0;i<=any;i++)
  54.     {
  55.         if (me[i].colour_of_socks!=i) goto ER;
  56.         if (me[i].age!=2*i) goto ER;
  57.         if (me[i].cash!=5.5*(float)i) goto ER;
  58. //        printf ("%s ",me[i].name);
  59.     }
  60.     printf ("\n");
  61.     stop_timer();
  62.     return;
  63. ER:
  64.     printf ("Error checking anything\n");
  65. }
  66.  
  67. void fillaaarray();
  68.  
  69. void fillxxfloatarray (int x, int y, xxfloat arr, int a)
  70. {
  71.     long i,j;
  72.     for (i=0;i<x;i++)
  73.     {
  74.         for (j=0;j<y;j++)
  75.         {
  76.             arr[i][j]=i*180+j+a;
  77.         }
  78.     }
  79. }
  80.  
  81. /* Global floating point arrays */
  82. xxdouble aa;
  83. xxfloat bb;
  84.  
  85.  
  86. void accumulate (xfloat arr, int x)
  87. {
  88.     int i;
  89.     float j=0;
  90.     for (i=0;i<x;i++) j+=arr[i];
  91.     printf ("Addition of all elements in the column is %f\n",j);
  92. }
  93.  
  94. void atest ()
  95. {
  96.     long i,j,l;
  97.     /*Declaring two dimensional floating arrays,
  98.             =>   xxfloat <variable>(<x size>,<y size>); */
  99.     xxfloat cc(180,180);//, dd (360,180);
  100.     /*Constructing global two dimensional arrays*/
  101.     /* These constructed two dimensional arrays will not go out of
  102.         scope so will need to be destroyed explictly to free memory */
  103.     bb.construct(180,180);
  104.     aa.construct(180,180);
  105.     /*Declaring one dimensional floating arrays,
  106.             =>    xfloat <variable>(<size>);  */
  107.     xfloat a(65000);//, b(65000), c(65000) ,d(65000);
  108.     printf ("\nThe following test checks three arrays of the size that will\n");
  109.     printf ("be shown. Array aa and bb are global arrays. aa is tested\n");
  110.     printf ("via a separate module\n");
  111.     printf ("Press a key to check arrays\n");
  112.     getch();
  113.     start_timer();
  114.     printf ("Filling Arrays\n");
  115.     printf ("double array aa(180,180)\n");
  116.     fillaaarray();
  117.     printf ("bb(180,180)\n");
  118.     fillxxfloatarray (180,180,bb,1);
  119.     printf ("cc(180,180)\n");
  120.     fillxxfloatarray (180,180,cc,2);
  121. //    printf ("dd(360,180)\n");
  122. //    fillxxfloatarray (360,180,dd,3);
  123.     printf ("Checking Arrays\n");
  124.     printf ("double array aa(180,180)\n");
  125.     for (i=0;i<180;i++)
  126.     {
  127.         for (j=0;j<180;j++)
  128.         {
  129.             if (aa[i][j]!=i*180+j) goto ERR2;
  130.         }
  131.     }
  132.     printf ("bb(180,180)\n");
  133.     for (i=0;i<180;i++)
  134.     {
  135.         for (j=0;j<180;j++)
  136.         {
  137.             if (bb[i][j]!=i*180+j+1) goto ERR2;
  138.         }
  139.     }
  140.     printf ("cc(180,180)\n");
  141.     for (i=0;i<180;i++)
  142.     {
  143.         for (j=0;j<180;j++)
  144.         {
  145.             if (cc[i][j]!=i*180+j+2) goto ERR2;
  146.         }
  147.     }
  148. /*    printf ("dd(360,180)\n");
  149.     for (i=0;i<360;i++)
  150.     {
  151.         for (j=0;j<180;j++)
  152.         {
  153.             if (dd[i][j]!=i*180+j+3) goto ERR2;
  154.         }
  155.     }*/
  156.     stop_timer();
  157. /*
  158.     An example of passing a column of array cc,
  159.     cc is of type xxfloat,
  160.     cc[10] is of type xfloat
  161. */
  162.     printf ("passing column 10 of cc array to accumulate routine\n");
  163.     accumulate (cc[10],180);
  164.     printf ("\nThe following test checks an array size 65000\n");
  165.     printf ("Press any key to start the test\n");
  166.     getch();
  167.     start_timer();
  168.     printf ("Filling Arrays\n");
  169.     printf ("a(65000)\n");
  170.     for (i=0;i<65000;i++)
  171.     {
  172.         a[i]=(float)i;
  173.     }
  174. /*    printf ("b(65000)\n");
  175.     for (i=0;i<65000;i++)
  176.     {
  177.         b[i]=(float)i;
  178.     }
  179.     printf ("c(65000)\n");
  180.     for (i=0;i<65000;i++)
  181.     {
  182.         c[i]=(float)i;
  183.     }
  184.     printf ("d(65000)\n");
  185.     for (i=0;i<65000;i++)
  186.     {
  187.         d[i]=(float)i;
  188.     }*/
  189.     printf ("Checking Arrays\n");
  190.     printf ("a(65000)\n");
  191.     for (i=0;i<65000;i++)
  192.     {
  193.         if (a[i]!=i) goto ERR;
  194.     }
  195. /*    printf ("b(65000)\n");
  196.     for (i=0;i<65000;i++)
  197.     {
  198.         if (b[i]!=i) goto ERR;
  199.     }
  200.     printf ("c(65000)\n");
  201.     for (i=0;i<65000;i++)
  202.     {
  203.         if (c[i]!=i) goto ERR;
  204.     }
  205.     printf ("d(65000)\n");
  206.     for (i=0;i<65000;i++)
  207.     {
  208.         if (d[i]!=i) goto ERR;
  209.     }*/
  210.     stop_timer();
  211.     aa.destroy();
  212.     bb.destroy();
  213.     return;
  214. ERR:
  215.     printf ("Error in large array %ld %f\n",i,a[i]);
  216.     exit(-1);
  217.  
  218. ERR2:
  219.     printf ("Error in 2d array %ld, %ld, %f, %f \n",i,j,aa[i][j],(float)(i*180+j));
  220. }
  221.  
  222. #define SZ 983040L
  223.  
  224. void btest()
  225. {
  226.     long i,j,l;
  227.     xchar huge_array(SZ);
  228.     printf ("Memory available %ld\n",coreleft());
  229.     printf ("\nThe following test checks and arrays of size %ld bytes\n",SZ);
  230.     printf ("Press a key to test the huge array\n");
  231.     getch();
  232.     start_timer();
  233.  
  234.     printf ("Filling huge array\n");
  235.     for (i=0;i<SZ;i++)
  236.         huge_array[i]=(char)i;
  237.     printf ("Checking huge array\n");
  238.     for (i=0;i<SZ;i++)
  239.         if (huge_array[i]!=(char)i) goto ERR;
  240.     printf ("hugh array ok\n");
  241.     stop_timer();
  242.     return;
  243. ERR:
  244.     printf ("Error in hugh array");
  245.     exit(-1);
  246. }
  247.  
  248. void main()
  249. {
  250.  
  251. /* Declared to work with restricted version */
  252.      __block_size=65536;
  253.      __blocks=16;
  254.  
  255.     long i,j,l;
  256.     printf ("Memory available %ld\n",coreleft());
  257.     do_anything();
  258.     atest();
  259.     btest();
  260.     printf ("Tests finished press any key to stop the program\n");
  261.     getch();
  262. }