Code /* array1.cpp CIS 250 David Klick 2005-02-06 Demonstration of arrays, passing arrays to functions, and pointer arithmetic. */ #include using std::cout; using std::endl; float sumArray1(float a[], int len); float sumArray2(float* a, int len); void initArray(float* a, int len); void clearArray(float*, int); void displayArray(int n[], int len); int main(void) { float x[15]; // declares and creates array of 15 floats // initialize an array when it is declared int x2[] = { 10, 8, 6, 4, 2, 0 }; // you can pass arrays to functions by using the array's name // - its equivalent to a pointer to the first array element // - you also have to send the size of the array cout << "Array x2: "; displayArray(x2, 6); float sum = sumArray1(x, 15); // don't forget to initialize your elements cout << "Sum of new array (uninitialized): " << sum << endl; // the functions can use pointers and work just as well clearArray(&x[0], 15); sum = sumArray2(x, 15); cout << "Sum of array after clearing: " << sum << endl; // since arrays are sent to functions by reference, // changes the functions make to the array are not made // to a copy, but the actual array and will not be lost initArray(x, 15); sum = sumArray2(x, 15); cout << "Sum of array after initArray: " << sum << endl; return 0; } float sumArray1(float arr[], int len) { float sum = 0.0f; for (int i=0; i= 0) n[len] = 0.0f; } void displayArray(int* n, int len) { for (int i=0; i0 ? ", " : "") << n[i]; } cout << endl; }