/* /usr/include/stdio.h, definitions for IO. */ #include #include /* 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; printf("Type a value for in: "); scanf("%d", &in); /* & = the address, the pointer */ /* %d for int, %f for float. \n is newline */ printf("This is how you write: in = %d\n", in); for (k = 0; k < n; k++) { /* for k = 0, ..., n-1 */ a[k] = k + 1; /* k++ means k = k + 1 */ b[k] = -sin(k + 1); /* requires -lm */ } /* Compute the inner product between a and b. */ printf("The inner product is %f\n", ddot(a, b, n)); /* * Send the address of sum. Arrays are passed by reference * automatically. */ sum_array(a, &sum, n); printf("The sum of the array is %f\n", sum); 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 *sum is a double. sum * itself is the address, the pointer to sum. */ { int k; /* k is local to sum_array */ *sum = 0; /* sum = 0 is WRONG; it will give a * Segmentation Fault */ 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 */ }