Lecture17 arraysAsParms
Lecture17 arraysAsParms
FUNCTIONS
inc(p[0], p[1]);
cout << "[" << p[0] << ", " << p[1] << "] ";
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const int SIZE_A(6);
int array_A[SIZE_A] = { 5, 3, 2, 1, 3, 5 };
double sum(0.0);
sum = 0.0;
for (int i = 0; i < SIZE_B; i++)
{
sum = sum + array_B[i];
}
cout << "Average of array B = " << sum/SIZE_B << endl;
return 0;
}
> arrayAverage1.exe
Average of array A = 3.16667
Average of array B = 4.125
>
return 0;
}
return 0;
}
The Ohio State University 16
Function compute_average()
// compute the average of the elements in an array
// array[] = array of integers
// numElements = number of elements in the array
double compute_average(const int array[],
const int numElements)
{
double average(0.0);
double sum(0.0); // Always initialize local variables
return(average);
}
return(average);
}
return(sum);
}
int main()
{
const int SIZE_A(6);
int array_A[SIZE_A] = { 5, 3, 2, 1, 3, 5 };
const int SIZE_B(8);
int array_B[SIZE_B] = { 2, 6, 9, 0, 6, 4, 5, 1 };
int sum(0);
double average(0.0);
. . .
return 0;
}
// function definitions
. . .
> arrayAverage3.exe
Array 1: sum = 19 average = 3.16667
Array 2: sum = 33 average = 4.125
>
cout << "Enter list of non-zero integers (ending with 0): ";
cin >> x;
while (x != 0 && numElements < ARRAY_SIZE) {
array[numElements] = x;
numElements++;
cin >> x;
}
...
const int ARRAY_SIZE(100);
int array[ARRAY_SIZE];
double average(0.0);
int numElements(0);
int x(0);
...
average = compute_average(array, numElements);
cout << "Average = " << average << endl;
...
...
const int ARRAY_SIZE(100);
int array[ARRAY_SIZE];
double average(0.0);
int numElements(0); Passing ARRAY_SIZE
int x(0); instead of numElements
...
average = compute_average(array, ARRAY_SIZE);
cout << "Average = " << average << endl;
...
// read input 5
inputCoord(1, px, py); 4
inputCoord(2, qx, qy);
inputCoord(3, rx, ry);
3
// calculate distances
dist_pq = dist(px, py, qx, qy);
dist_pr = dist(px, py, rx, ry);
dist_qr = dist(qx, qy, rx, ry);
. . .
// function prototypes
void inputCoord(int i, double point[DIMENSION]);
double dist(const double point1[DIMENSION], // calculate distance
const double point2[DIMENSION]);
void output_distance(const double point1[DIMENSION],
const double point2[DIMENSION],
double distance);
void output_point(const double point[DIMENSION]);
int main()
{
double p[DIMENSION], q[DIMENSION], r[DIMENSION];
double dist_pq, dist_pr, dist_qr;
...
// read input
inputCoord(1, p);
inputCoord(2, q);
inputCoord(3, r);
// calculate distances
dist_pq = dist(p, q);
dist_pr = dist(p, r);
dist_qr = dist(q, r);
// output distances
output_distance(p, q, dist_pq);
output_distance(p, r, dist_pr);
output_distance(q, r, dist_qr);
return 0;
}
The Ohio State University 29
function input_coord()
// NOTE: Array is passed by reference. NO const.
void inputCoord(int i, double point[DIMENSION])
{
cout << "Enter point " << i << " ("
<< DIMENSION << " floats): ";
// read input
inputCoord(1, p);
inputCoord(2, q);
inputCoord(3, r);
// calculate distances
dist_pq = dist(p, q);
dist_pr = dist(p, r);
dist_qr = dist(q, r);
. . .
The Ohio State University 34
Summary: Passing Arrays to Functions
Arrays are always passed by reference
Decide to use const. Don’t use if you want the array
to change inside the function.