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

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