# include # include int main ( ); void loadArrays ( int n, float *a, float *b ); void add_vectors ( int n, float *a, float *b, float *c ); int main ( ) { float *a; float *b; float *c; int i; int memsize; int n; printf ( "\n" ); printf ( "VECADD:\n" ); printf ( " C version.\n" ); printf ( " Compute C=A+B for vectors A and B.\n" ); // // Set size of vectors; // n = 10; // // Allocate A and B. // memsize = n * sizeof ( float ); a = ( float * ) malloc ( memsize ); b = ( float * ) malloc ( memsize ); c = ( float * ) malloc ( memsize ); // // Load A and B with random floats. // loadArrays ( n, a, b ); // // Call the function that does the work. // add_vectors ( n, a, b, c ); // // Print the results. // printf ( "\n" ); printf ( " i A[i] B[i] C[i]=A[i]+B[i]\n" ); printf ( "\n" ); for ( i = 0; i < n && i < 10; i++ ) { printf ( " %2d %8.2f %8.2f %8.2f\n", i, a[i], b[i], c[i] ); } // // Free CPU memory. // free ( a ); free ( b ); free ( c ); // // Terminate. // printf ( "\n" ); printf ( "VECADD:\n" ); printf ( " Normal end of execution.\n" ); return 0; } void loadArrays ( int n, float *a, float *b ) { int i; srand ( 1 ); for ( i = 0; i < n; i++ ) { a[i] = ( float ) rand ( ) / ( ( float ) RAND_MAX / 100 ); b[i] = ( float ) rand ( ) / ( ( float ) RAND_MAX / 100 ); } return; } void add_vectors ( int n, float *a, float *b, float *c ) { // int i; for ( i = 0; i < n; i++ ) { c[i] = a[i] + b[i]; } return; }