Changes in src/examples/sum.c [86bd7c1f:843054c2]
- File:
-
- 1 edited
-
src/examples/sum.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/examples/sum.c
r86bd7c1f r843054c2 1 //2 // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo3 //4 // The contents of this file are covered under the licence agreement in the5 // file "LICENCE" distributed with Cforall.6 //7 // sum.c --8 //9 // Author : Richard C. Bilson10 // Created On : Wed May 27 17:56:53 201511 // Last Modified By : Peter A. Buhr12 // Last Modified On : Wed May 27 18:43:46 201513 // Update Count : 414 //15 16 1 extern "C" { 17 int printf( const char *, ... );2 int printf( const char *, ... ); 18 3 } 19 4 20 5 context sumable( type T ) { 21 const T 0;22 T ?+?( T, T );23 T ?++( T * );24 T ?+=?( T *, T );6 const T 0; 7 T ?+?( T, T ); 8 T ?++( T * ); 9 T ?+=?( T *, T ); 25 10 }; 26 11 27 12 forall( type T | sumable( T ) ) 28 13 T sum( int n, T a[] ) { 29 T total;// instantiate T, select 030 total = 0;31 for ( int i = 0; i < n; i += 1 )32 total = total + a[i];// select +33 return total;14 T total; // instantiate T, select 0 15 total = 0; 16 for ( int i = 0; i < n; i += 1 ) 17 total = total + a[i]; // select + 18 return total; 34 19 } 35 20 … … 42 27 43 28 int main() { 44 const int size = 10, low = 0, High = 10;45 int si = 0, ai[10]; // size46 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 );29 const int size = 10, low = 0, High = 10; 30 int si = 0, ai[10]; // size 31 int i; 32 for ( i = low; i < High; i += 1 ) { 33 si += i; 34 ai[i] = i; 35 } 36 printf( "sum from %d to %d is %d, check %d\n", 37 low, High, sum( size, ai ), si ); 53 38 54 // char ci[10];55 // char c = sum( size, ci );56 // float fi[10];57 // float f = sum( size, fi );39 // char ci[10]; 40 // char c = sum( size, ci ); 41 // float fi[10]; 42 // float f = sum( size, fi ); 58 43 59 double sd = 0.0, ad[10];// size60 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 );44 double sd = 0.0, ad[10]; // size 45 for ( i = low; i < High; i += 1 ) { 46 double d = i / (double)size; 47 sd += d; 48 ad[i] = d; 49 } 50 printf( "sum from %g to %g is %g, check %g\n", 51 low / (double)size, High / (double)size, sum( size, ad ), sd ); 67 52 } 68 53 69 54 // Local Variables: // 70 // tab-width: 4 // 71 // compile-command: "cfa sum.c" // 55 // compile-command: "../../bin/cfa sum.c" // 72 56 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.