C++ Function Parameter Passing an Array to a Function
#include <iostream> using namespace std; const int size = 10; void myfunction(int myarray[], int size) { for (int i=0; i<size; i++) {/*from w ww . j a v a 2s .c o m*/ cout << myarray[i] << endl; } } int main() { int myArray[size]; for (int i=0; i<size; i++) { myArray[i] = i * 2; } myfunction(myArray, size); return 0; }
#include <iostream> double average(double array[], int count); // Function prototype int main()/*from ww w. j ava 2 s .c om*/ { double values[] {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; std::cout << "Average = " << average(values, (sizeof values)/(sizeof values[0])) << std::endl; } // Function to compute an average double average(double array[], int count) { double sum {}; // Accumulate total in here for (int i {} ; i < count ; ++i) sum += array[i]; // Sum array elements return sum / count; // Return average }