









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










//Example 1:passing array elements to a function
#include
int main() { const int size = 5; double x[size] = {1.8, 2.2, 3.4, 5.1, 6.7}, y[size] = {2.0, 4.5, 1.3, 4.0, 5.5}, z[size]; add_array(size , x, y, z); cout << "Content of array z is: \n"; for (i = 0; i < size; i++) cout << "z[" << i << "] is " << z[i] << endl; return 0; }
// Add a[i] and b[i] and store the sum in c[i] void add_array(int size, double a[], double b[], double c[] ) { int i; // Add a[i] and b[i] and store result in c[i] for (i=0; i < size; i++) c[i] = a[i] + b[i]; } // Note that the function does not return any value
What if the array size is not in a const int? If we have an array named arr. The size of the array can be passed to the function using the following (sizeof (arr) / sizeof(arr[0]) The sizeof operator finds the total number of bytes for a variable.
What if the array size is not in a const int?