// Definitions for IO #include #include using namespace std; // Here are the prototypes for our procedures void sum_array(double[], double &, int); double ddot(double[], double[], int); int main() { int k, n, in; double a[100], b[100], sum; // Arrays are indexed from 0, a[0], ..., a[99] n = 100; // endl is newline cout << "Type a value for in: " << endl; cin >> in; cout << "This is how you write: in = " << in << endl; for (k = 0; k < n; k++) { // for k = 0, ..., n-1 a[k] = k + 1; // k++ means k = k + 1 b[k] = -sin(double(k + 1)); } // Compute the inner product between a and b. cout << "The inner product is " << ddot(a, b, n) << endl; // Arrays are passed by reference automatically. // Use a reference parameter for sum. sum_array(a, sum, n); cout << "The sum of the array is " << sum << endl; return 0; // Return status to unix } void sum_array(double a[], double &sum, int n) // void, i.e. not a function. double &sum means that we can return // a value in sum (call by reference) { int k; // k is local to sum_array sum = 0; for (k = 0; k < n; k++) sum += a[k]; // i.e. sum = sum + a[k] } // NOT void here since this is a function (of type double) returning a // result. double ddot(double x[], double y[], int n) { int k; double sum; sum = 0.0; for (k = 0; k < n; k++) sum += x[k] * y[k]; return sum; // return the result }