[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 |
---|
[784deab] | 12 | // Last Modified On : Mon Jan 4 07:40:24 2016 |
---|
| 13 | // Update Count : 126 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[097e2b0] | 16 | #include "fstream.h" |
---|
[a0d9f94] | 17 | |
---|
| 18 | context sumable( type T ) { |
---|
[86bd7c1f] | 19 | const T 0; |
---|
| 20 | T ?+?( T, T ); |
---|
| 21 | T ?+=?( T *, T ); |
---|
[097e2b0] | 22 | T ++?( T * ); |
---|
| 23 | T ?++( T * ); |
---|
[a0d9f94] | 24 | }; |
---|
| 25 | |
---|
| 26 | forall( type 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; |
---|
[a0d9f94] | 32 | } |
---|
| 33 | |
---|
[bdd516a] | 34 | // Required to satisfy sumable as char does not have addition. |
---|
| 35 | const char 0; |
---|
[097e2b0] | 36 | char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion |
---|
| 37 | char ++?( char *op ) { *op += 1; return *op; } |
---|
| 38 | char ?++( char *op ) { char temp = *op; *op += 1; return temp; } |
---|
[bdd516a] | 39 | |
---|
[784deab] | 40 | int main( void ) { |
---|
[6e7e2b36] | 41 | const int low = 5, High = 15, size = High - low; |
---|
[097e2b0] | 42 | |
---|
| 43 | ofstream *sout = ofstream_stdout(); |
---|
| 44 | |
---|
| 45 | char s = 0, a[size]; |
---|
| 46 | char v = low; |
---|
[6e7e2b36] | 47 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
[097e2b0] | 48 | s += v; |
---|
| 49 | a[i] = v; |
---|
[86bd7c1f] | 50 | } |
---|
[784deab] | 51 | sout | "sum from " | low | " to " | High | " is " |
---|
| 52 | | (int)sum( size, a ) | ", check " | (int)s | endl; |
---|
[86bd7c1f] | 53 | |
---|
[097e2b0] | 54 | int s = 0, a[size]; |
---|
| 55 | int v = low; |
---|
| 56 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 57 | s += (int)v; |
---|
| 58 | a[i] = (int)v; |
---|
| 59 | } |
---|
[784deab] | 60 | sout | "sum from " | low | " to " | High | " is " |
---|
| 61 | | sum( size, (int *)a ) | ", check " | (int)s | endl; |
---|
[86bd7c1f] | 62 | |
---|
[097e2b0] | 63 | double s = 0.0, a[size]; |
---|
[6e7e2b36] | 64 | double v = low / 10.0; |
---|
| 65 | for ( int i = 0; i < size; i += 1, v += 0.1 ) { |
---|
[097e2b0] | 66 | s += (double)v; |
---|
| 67 | a[i] = (double)v; |
---|
| 68 | } |
---|
[784deab] | 69 | sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " |
---|
| 70 | | sum( size, (double *)a ) | ", check " | (double)s | endl; |
---|
[097e2b0] | 71 | |
---|
| 72 | float s = 0.0, a[size]; |
---|
| 73 | float v = low / 10.0; |
---|
| 74 | for ( int i = 0; i < size; i += 1, v += 0.1f ) { |
---|
| 75 | s += (float)v; |
---|
| 76 | a[i] = (float)v; |
---|
[86bd7c1f] | 77 | } |
---|
[784deab] | 78 | sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " |
---|
| 79 | | sum( size, (float *)a ) | ", check " | (float)s | endl; |
---|
[a0d9f94] | 80 | } |
---|
[42dcae7] | 81 | |
---|
| 82 | // Local Variables: // |
---|
[86bd7c1f] | 83 | // tab-width: 4 // |
---|
[784deab] | 84 | // compile-command: "cfa sum.c fstream.o iostream.o iterator.o" // |
---|
[42dcae7] | 85 | // End: // |
---|