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 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Wed May 27 17:56:53 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Jan 4 07:40:24 2016 |
---|
13 | // Update Count : 126 |
---|
14 | // |
---|
15 | |
---|
16 | #include "fstream.h" |
---|
17 | |
---|
18 | context sumable( type T ) { |
---|
19 | const T 0; |
---|
20 | T ?+?( T, T ); |
---|
21 | T ?+=?( T *, T ); |
---|
22 | T ++?( T * ); |
---|
23 | T ?++( T * ); |
---|
24 | }; |
---|
25 | |
---|
26 | forall( type T | sumable( T ) ) |
---|
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 + |
---|
31 | return total; |
---|
32 | } |
---|
33 | |
---|
34 | // Required to satisfy sumable as char does not have addition. |
---|
35 | const char 0; |
---|
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; } |
---|
39 | |
---|
40 | int main( void ) { |
---|
41 | const int low = 5, High = 15, size = High - low; |
---|
42 | |
---|
43 | ofstream *sout = ofstream_stdout(); |
---|
44 | |
---|
45 | char s = 0, a[size]; |
---|
46 | char v = low; |
---|
47 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
48 | s += v; |
---|
49 | a[i] = v; |
---|
50 | } |
---|
51 | sout | "sum from " | low | " to " | High | " is " |
---|
52 | | (int)sum( size, a ) | ", check " | (int)s | endl; |
---|
53 | |
---|
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 | } |
---|
60 | sout | "sum from " | low | " to " | High | " is " |
---|
61 | | sum( size, (int *)a ) | ", check " | (int)s | endl; |
---|
62 | |
---|
63 | double s = 0.0, a[size]; |
---|
64 | double v = low / 10.0; |
---|
65 | for ( int i = 0; i < size; i += 1, v += 0.1 ) { |
---|
66 | s += (double)v; |
---|
67 | a[i] = (double)v; |
---|
68 | } |
---|
69 | sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " |
---|
70 | | sum( size, (double *)a ) | ", check " | (double)s | endl; |
---|
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; |
---|
77 | } |
---|
78 | sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is " |
---|
79 | | sum( size, (float *)a ) | ", check " | (float)s | endl; |
---|
80 | } |
---|
81 | |
---|
82 | // Local Variables: // |
---|
83 | // tab-width: 4 // |
---|
84 | // compile-command: "cfa sum.c fstream.o iostream.o iterator.o" // |
---|
85 | // End: // |
---|