Changes in src/tests/sum.c [2b9ddf2:09687aa]
- File:
-
- 1 edited
-
src/tests/sum.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/tests/sum.c
r2b9ddf2 r09687aa 11 11 // Created On : Wed May 27 17:56:53 2015 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Fri Jan 26 11:31:02 201814 // Update Count : 2 7113 // Last Modified On : Thu Dec 7 09:13:36 2017 14 // Update Count : 261 15 15 // 16 16 17 17 #include <fstream> 18 #include <stdlib>19 18 20 19 void ?{}( int & c, zero_t ) { c = 0; } … … 29 28 30 29 forall( otype T | sumable( T ) ) // use trait 31 T sum( unsigned int size, T a[] ) {32 T total = 0; // instantiate T from 0 by calling constructor33 for ( unsigned int i = 0; i < size; i += 1 )34 total += a[i]; // select appropriate+30 T sum( unsigned int n, T a[] ) { 31 T total = 0; // instantiate T, select 0 32 for ( unsigned int i = 0; i < n; i += 1 ) 33 total += a[i]; // select + 35 34 return total; 36 35 } // sum … … 56 55 } // for 57 56 sout | "sum from" | low | "to" | High | "is" 58 | sum( size, (unsigned char *)a ) | ", check" | (int)s | endl;57 | sum( size, (unsigned char *)a ) | ", check" | (int)s | endl; 59 58 60 59 int s = 0, a[size], v = low; … … 64 63 } // for 65 64 sout | "sum from" | low | "to" | High | "is" 66 | sum( size, (int *)a ) | ", check" | (int)s | endl;65 | sum( size, (int *)a ) | ", check" | (int)s | endl; 67 66 68 67 float s = 0.0f, a[size], v = low / 10.0f; … … 72 71 } // for 73 72 sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is" 74 | sum( size, (float *)a ) | ", check" | (float)s | endl;73 | sum( size, (float *)a ) | ", check" | (float)s | endl; 75 74 76 75 double s = 0.0, a[size], v = low / 10.0; … … 80 79 } // for 81 80 sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is" 82 | sum( size, (double *)a ) | ", check" | (double)s | endl;81 | sum( size, (double *)a ) | ", check" | (double)s | endl; 83 82 84 83 struct S { int i, j; }; … … 99 98 } // for 100 99 sout | "sum from" | low | "to" | High | "is" 101 | sum( size, (S *)a ) | ", check" | (S)s | endl; 102 103 forall( otype Impl | sumable( Impl ) ) 104 struct GS { 105 Impl * x, * y; 106 }; 107 GS(int) gs; 108 gs.x = anew( size ); // create array storage for field 109 s = 0; v = low; 110 for ( int i = 0; i < size; i += 1, v += 1 ) { 111 s += (int)v; 112 gs.x[i] = (int)v; // set filed array in generic type 113 } // for 114 sout | "sum from" | low | "to" | High | "is" 115 | sum( size, gs.x ) | ", check" | (int)s | endl; // add filed array in generic type 100 | sum( size, (S *)a ) | ", check" | (S)s | endl; 116 101 } // main 117 102
Note:
See TracChangeset
for help on using the changeset viewer.