Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * passingarrays.cpp
- *
- * Created on: Jun 25, 2012
- * Author: Derek
- *
- * Example of passing arrays to functions
- */
- #include <iostream>
- using namespace std;
- void printArray(double[], int);
- void userInputArray(double array[], int size);
- int main()
- {
- double rainfall[5];
- rainfall[0] = 2.3;
- rainfall[1] = 0.3;
- rainfall[2] = 0.0;
- rainfall[3] = 4.1;
- rainfall[4] = 0.5;
- printArray(rainfall, 5);
- userInputArray(rainfall, 5);
- printArray(rainfall, 5);
- }
- // The first formal parameter could also be
- // written as double* array since a pointer
- // to the first element is what is really
- // passed to the array. (Ditto for function
- // below)
- void printArray(double array[], int size)
- {
- for(int i = 0; i < size; i++)
- {
- cout << array[i] << endl;
- }
- }
- void userInputArray(double array[], int size)
- {
- for(int i = 0; i < size; i++)
- {
- cout << "Enter a rainfall amount: " << endl;
- cin >> array[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement