source: src/examples/sum.c@ 7754cde

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 7754cde was cf16f94, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

create temporary return variable for return expressions, remove unnecessary code after temporary-return-variable change, fix missing lvalue qualifiers, change stream operator to |

  • Property mode set to 100644
File size: 2.4 KB
Line 
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 : Sat Nov 21 18:08:18 2015
13// Update Count : 119
14//
15
16extern "C" {
17 int printf( char *, ... );
18}
19#include "fstream.h"
20
21context sumable( type T ) {
22 const T 0;
23 T ?+?( T, T );
24 T ?+=?( T *, T );
25 T ++?( T * );
26 T ?++( T * );
27};
28
29forall( type T | sumable( T ) )
30T 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 +
34 return total;
35}
36
37// Required to satisfy sumable as char does not have addition.
38const char 0;
39char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion
40char ++?( char *op ) { *op += 1; return *op; }
41char ?++( char *op ) { char temp = *op; *op += 1; return temp; }
42
43int main() {
44 const int low = 5, High = 15, size = High - low;
45
46 ofstream *sout = ofstream_stdout();
47
48 char s = 0, a[size];
49 char v = low;
50 for ( int i = 0; i < size; i += 1, v += 1 ) {
51 s += v;
52 a[i] = v;
53 }
54 sout << "sum from " << low << " to " << High << " is "
55 << (int)sum( size, a ) << ", check " << (int)s << endl;
56
57 int s = 0, a[size];
58 int v = low;
59 for ( int i = 0; i < size; i += 1, v += 1 ) {
60 s += (int)v;
61 a[i] = (int)v;
62 }
63 sout << "sum from " << low << " to " << High << " is "
64 << sum( size, (int *)a ) << ", check " << (int)s << endl;
65
66 double s = 0.0, a[size];
67 double v = low / 10.0;
68 for ( int i = 0; i < size; i += 1, v += 0.1 ) {
69 s += (double)v;
70 a[i] = (double)v;
71 }
72 printf( "%g\n", sum( size, (double *)a ) );
73// sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is "
74// << sum( size, (double *)a ) << ", check " << (double)s << endl;
75
76 float s = 0.0, a[size];
77 float v = low / 10.0;
78 for ( int i = 0; i < size; i += 1, v += 0.1f ) {
79 s += (float)v;
80 a[i] = (float)v;
81 }
82 printf( "%g\n", sum( size, (float *)a ) );
83// sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is "
84// << sum( size, (float *)a ) << ", check " << (float)s << endl;
85}
86
87// Local Variables: //
88// tab-width: 4 //
89// compile-command: "cfa sum.c fstream.o iostream.o" //
90// End: //
Note: See TracBrowser for help on using the repository browser.