//ic210 section 4002 //16 oct 2009 //arrays and functions: passing an array to a function and //returning an array from a function #include #include #include using namespace std; int computeSum(int* , int); int* readInts(int, istream&); int main() { ifstream fin("numbers"); /* Open input file, read header, get N = # of int's */ int N; string s; cin >> N; /* Create array A and read N int's from input */ int * A = readInts(N,cin); //compute sum int sum = computeSum(A, N); // Print out the average cout << "The avg is " << sum/double(N) << endl; return 0; } int computeSum(int* A, int N) { int sum = 0; /* Cycle through array summing all the elements */ for(int j = 0; j < N; j++) { sum = sum + A[j]; } return sum; } int* readInts(int N, istream& IN) { int *A = new int[N]; for(int i = 0; i < N; i++) IN >> A[i]; return A; }