[86bd7c1f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // sum.c -- |
---|
| 8 | // |
---|
[097e2b0] | 9 | // Author : Peter A. Buhr |
---|
[86bd7c1f] | 10 | // Created On : Wed May 27 17:56:53 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[0638c44] | 12 | // Last Modified On : Mon May 2 15:07:57 2016 |
---|
| 13 | // Update Count : 198 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[d3b7937] | 16 | #include <fstream> |
---|
[a0d9f94] | 17 | |
---|
[b63e376] | 18 | trait sumable( otype T ) { |
---|
[86bd7c1f] | 19 | const T 0; |
---|
| 20 | T ?+?( T, T ); |
---|
| 21 | T ?+=?( T *, T ); |
---|
[097e2b0] | 22 | T ++?( T * ); |
---|
| 23 | T ?++( T * ); |
---|
[d3b7937] | 24 | }; // sumable |
---|
[a0d9f94] | 25 | |
---|
[b63e376] | 26 | forall( otype T | sumable( T ) ) |
---|
[097e2b0] | 27 | T sum( unsigned int n, T a[] ) { |
---|
| 28 | T total = 0; // instantiate T, select 0 |
---|
| 29 | for ( unsigned int i = 0; i < n; i += 1 ) |
---|
| 30 | total += a[i]; // select + |
---|
[86bd7c1f] | 31 | return total; |
---|
[d3b7937] | 32 | } // sum |
---|
[a0d9f94] | 33 | |
---|
[bdd516a] | 34 | // Required to satisfy sumable as char does not have addition. |
---|
[52f85e0] | 35 | const char 0; |
---|
| 36 | char ?+?( char t1, char t2 ) { return (int)t1 + t2; } // cast forces integer addition, otherwise recursion |
---|
| 37 | char ?+=?( char *t1, char t2 ) { *t1 = *t1 + t2; return *t1; } |
---|
| 38 | char ++?( char *t ) { *t += 1; return *t; } |
---|
| 39 | char ?++( char *t ) { char temp = *t; *t += 1; return temp; } |
---|
[bdd516a] | 40 | |
---|
[784deab] | 41 | int main( void ) { |
---|
[6e7e2b36] | 42 | const int low = 5, High = 15, size = High - low; |
---|
[097e2b0] | 43 | |
---|
[52f85e0] | 44 | char s = 0, a[size], v = low; |
---|
[6e7e2b36] | 45 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
[097e2b0] | 46 | s += v; |
---|
| 47 | a[i] = v; |
---|
[52f85e0] | 48 | } // for |
---|
[90c3b1c] | 49 | sout | "sum from" | low | "to" | High | "is" |
---|
[0638c44] | 50 | | (int)sum( size, a ) | ", check" | (int)s | endl; |
---|
[86bd7c1f] | 51 | |
---|
[52f85e0] | 52 | int s = 0, a[size], v = low; |
---|
[097e2b0] | 53 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 54 | s += (int)v; |
---|
| 55 | a[i] = (int)v; |
---|
[52f85e0] | 56 | } // for |
---|
[90c3b1c] | 57 | sout | "sum from" | low | "to" | High | "is" |
---|
[0638c44] | 58 | | sum( size, (int *)a ) | ", check" | (int)s | endl; |
---|
[86bd7c1f] | 59 | |
---|
[52f85e0] | 60 | float s = 0.0, a[size], v = low / 10.0; |
---|
[097e2b0] | 61 | for ( int i = 0; i < size; i += 1, v += 0.1f ) { |
---|
| 62 | s += (float)v; |
---|
| 63 | a[i] = (float)v; |
---|
[52f85e0] | 64 | } // for |
---|
[90c3b1c] | 65 | sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is" |
---|
[0638c44] | 66 | | sum( size, (float *)a ) | ", check" | (float)s | endl; |
---|
[bd85400] | 67 | |
---|
[52f85e0] | 68 | double s = 0, a[size], v = low / 10.0; |
---|
[d3b7937] | 69 | for ( int i = 0; i < size; i += 1, v += 0.1 ) { |
---|
| 70 | s += (double)v; |
---|
| 71 | a[i] = (double)v; |
---|
[52f85e0] | 72 | } // for |
---|
[90c3b1c] | 73 | sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is" |
---|
[0638c44] | 74 | | sum( size, (double *)a ) | ", check" | (double)s | endl; |
---|
[bd85400] | 75 | |
---|
[52f85e0] | 76 | struct S { int i, j; } 0 = { 0, 0 }, 1 = { 1, 1 }; |
---|
| 77 | S ?+?( S t1, S t2 ) { S s = { t1.i + t2.i, t1.j + t2.j }; return s; } |
---|
| 78 | S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; } |
---|
| 79 | S ++?( S *t ) { *t += 1; return *t; } |
---|
| 80 | S ?++( S *t ) { S temp = *t; *t += 1; return temp; } |
---|
[90c3b1c] | 81 | ofstream * ?|?( ofstream * os, S v ) { return os | v.i | v.j; } |
---|
[52f85e0] | 82 | |
---|
| 83 | S s = 0, a[size], v = { low, low }; |
---|
| 84 | for ( int i = 0; i < size; i += 1, v += (S)1 ) { |
---|
| 85 | s += (S)v; |
---|
| 86 | a[i] = (S)v; |
---|
| 87 | } // for |
---|
[90c3b1c] | 88 | sout | "sum from" | low | "to" | High | "is" |
---|
[0638c44] | 89 | | sum( size, (S *)a ) | ", check" | (S)s | endl; |
---|
[d3b7937] | 90 | } // main |
---|
[42dcae7] | 91 | |
---|
| 92 | // Local Variables: // |
---|
[86bd7c1f] | 93 | // tab-width: 4 // |
---|
[d3b7937] | 94 | // compile-command: "cfa sum.c" // |
---|
[42dcae7] | 95 | // End: // |
---|