Changeset 86bd7c1f for src/examples/sum.c
- Timestamp:
- May 27, 2015, 8:33:19 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 00cc023
- Parents:
- 52ac3b4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/sum.c
r52ac3b4 r86bd7c1f 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 : Richard C. Bilson 10 // Created On : Wed May 27 17:56:53 2015 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:43:46 2015 13 // Update Count : 4 14 // 15 1 16 extern "C" { 2 17 int printf( const char *, ... ); 3 18 } 4 19 5 20 context sumable( type T ) { 6 7 8 9 21 const T 0; 22 T ?+?( T, T ); 23 T ?++( T * ); 24 T ?+=?( T *, T ); 10 25 }; 11 26 12 27 forall( type T | sumable( T ) ) 13 28 T sum( int n, T a[] ) { 14 T total;// instantiate T, select 015 16 17 total = total + a[i];// select +18 29 T total; // instantiate T, select 0 30 total = 0; 31 for ( int i = 0; i < n; i += 1 ) 32 total = total + a[i]; // select + 33 return total; 19 34 } 20 35 … … 27 42 28 43 int main() { 29 30 31 32 33 si += i;34 ai[i] = i;35 36 37 44 const int size = 10, low = 0, High = 10; 45 int si = 0, ai[10]; // size 46 int i; 47 for ( i = low; i < High; i += 1 ) { 48 si += i; 49 ai[i] = i; 50 } 51 printf( "sum from %d to %d is %d, check %d\n", 52 low, High, sum( size, ai ), si ); 38 53 39 // 40 // 41 // 42 // 54 // char ci[10]; 55 // char c = sum( size, ci ); 56 // float fi[10]; 57 // float f = sum( size, fi ); 43 58 44 double sd = 0.0, ad[10];// size45 46 double d = i / (double)size;47 sd += d;48 ad[i] = d;49 50 51 59 double sd = 0.0, ad[10]; // size 60 for ( i = low; i < High; i += 1 ) { 61 double d = i / (double)size; 62 sd += d; 63 ad[i] = d; 64 } 65 printf( "sum from %g to %g is %g, check %g\n", 66 low / (double)size, High / (double)size, sum( size, ad ), sd ); 52 67 } 53 68 54 69 // Local Variables: // 55 // compile-command: "../../bin/cfa sum.c" // 70 // tab-width: 4 // 71 // compile-command: "cfa sum.c" // 56 72 // End: //
Note: See TracChangeset
for help on using the changeset viewer.