0% found this document useful (0 votes)
3 views

Lecture17 arraysAsParms

Uploaded by

Govind
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture17 arraysAsParms

Uploaded by

Govind
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

PASSING ARRAY VALUES TO

FUNCTIONS

The Ohio State University 1


Multiple Options
 Pass individual values by “pass by value”
 Pass individual values by “pass by reference”
 Pass entire variable length arrays
 Pass entire fixed length arrays

The Ohio State University 2


Passing Array Values Into a Function
 Pass By Value with array elements
void incv(double x1, double y1, double x2, double y2);
. . .
int main () {
double p[2] = { 3, 0 };
Displays:
double q[2] = { 0, 4 };
[3, 0]

double d = incv(p[0], p[1], q[0], q[1]);


cout << “[“ << p[0] << “, “ << p[1] << “]”;
}
void incv (double a, double b, double c, double d) {
a++; b++; c++; d++;
}

The Ohio State University 3


Passing Array Values Into a Function
 Pass By Reference with array elements
void inc(double & x, double & y);
Displays:
int main () { [4, 1]
double p[2] = { 3, 0 };

inc(p[0], p[1]);
cout << "[" << p[0] << ", " << p[1] << "] ";
}

void inc(double & x, double & y)


{
x++;
y++;
}

The Ohio State University 4


PASSING VARIABLE LENGTH
ARRAYS TO FUNCTIONS
(PREFERRED WAY)

The Ohio State University 5


Array as Function Parameter
 Pass the entire array to the function Use empty []

void print_array(int array[], int size);


int main()
{ Need size
const int ARR_SIZE(6);
int list[] = { 5, 3, 2, 1, 3, 5 };
print_array(list, ARR_SIZE);
}
void print_array(int array[], int size)
{
for (int i = 0; i < size; i++) {
cout << " " << array[i];
}
cout << endl;
}

The Ohio State University 6


How Is An Array Passed?
void print_array(int array[], int size);

 Is it using pass by value or pass by reference for


the array?
 Notice that we don’t see the & character used

 Arrays are always passed by reference … why???


 Making a copy of a large array for a function call would be
expensive!
 Always passed a pointer to the original array.

The Ohio State University 7


Warning: Pass By Reference
list is now
int main() [5, 10, 2, 1, 3, 5]
{
const int ARR_SIZE(6);
int list[] = { 5, 3, 2, 1, 3, 5 };
print_array(list, ARR_SIZE);
}

void print_array(int array[], int size)


{
for (int i = 0; i < size; i++) {
cout << " " << array[i];
}
cout << endl;
array[1] = 10; // Dangerous!
}

The Ohio State University 8


Safer: Pass By Reference
int main()
{
const int ARR_SIZE(6);
int list[] = { 5, 3, 2, 1, 3, 5 };
print_array(list, ARR_SIZE);
}

void print_array(const int array[], int size)


{
for (int i = 0; i < size; i++) {
cout << " " << array[i];
}
The compiler will
cout << endl;
array[1] = 10; not allow this!
} Why not?

The Ohio State University 9


Another Warning
int main()
{
const int ARR_SIZE(6);
int list[] = { 5, 3, 2, 1, 3, 5 };
print_array(list, ARR_SIZE);
}

void print_array(const int array[], int size)


{
size = 100; // I should never change size
for (int i = 0; i < size; i++) {
cout << " " << array[i];
}
cout << endl;
}

The Ohio State University 10


Better
int main()
{
const int ARR_SIZE(6);
int list[] = { 5, 3, 2, 1, 3, 5 };
print_array(list, ARR_SIZE);
}

void print_array(const int array[], const int size)


{
size = 100;
for (int i = 0; i < size; i++) {
cout << " " << array[i];
}
cout << endl; The compiler will
} not allow this
either!

The Ohio State University 11


arrayAverage1().cpp
// find the average of the elements in an array

#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 };

const int SIZE_B(8);


int array_B[SIZE_B] = { 2, 6, 9, 0, 6, 4, 5, 1 };

double sum(0.0);

The Ohio State University 12


arrayAverage1().cpp (cont)
. . .
sum = 0.0;
for (int i = 0; i < SIZE_A; i++)
{
sum = sum + array_A[i];
}
cout << "Average of array A = " << sum/SIZE_A << endl;

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;
}

The Ohio State University 13



sum = 0.0;
for (int i = 0; i < SIZE_A; i++)
{
sum = sum + array_A[i];
}
cout << "Average of array A = " << sum/SIZE_A << endl;
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;

> arrayAverage1.exe
Average of array A = 3.16667
Average of array B = 4.125
>

The Ohio State University 14


Reduce Duplication
. . . Define a function
sum = 0.0;
for (int i = 0; i < SIZE_A; i++)
for the duplicate
{ code:
sum = sum + array_A[i]; ▪ Name?
}
▪ Arguments?
cout << "Average of array A = " << sum/SIZE_A << endl;
▪ Return the
sum = 0.0; average
for (int i = 0; i < SIZE_B; i++)
{
▪ Print average
sum = sum + array_B[i]; in main
} function
cout << "Average of array B = " << sum/SIZE_B << endl;

return 0;
}

The Ohio State University 15


arrayAverage4().cpp
. . .
double compute_average(const int array[],
const int numElements)
. . .
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 };
double average = 0.0;

average = compute_average(array_A, SIZE_A);


cout << "Average of array A = " << average << endl;

average = compute_average(array_B, SIZE_B);


cout << "Average of array B = " << average << endl;

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

for (int i = 0; i < numElements; i++)


{
sum = sum + array[i];
}
average = sum/numElements;

return(average);
}

The Ohio State University 17


A Variation

The Ohio State University 18


Function Calling Another Function
// compute the average of the elements in an array
double compute_average(const int array[], const int numElements)
{
int sum(0);
double average(0.0);

sum = array_sum(array, numElements);


average = double(sum)/numElements; // Note: cast to double

return(average);
}

// compute the sum of the elements in an array


int array_sum(const int array[], const int numElements)
{
int sum(0);
for (int i = 0; i < numElements; i++)
{ sum = sum + array[i]; }

return(sum);
}

The Ohio State University 19


main() can also call array_sum()
. . .
// function prototype
double compute_average(const int array[],
const int numElements);
int array_sum(const int array[], const int numElements);

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);

. . .

The Ohio State University 20


arrayAverage4().cpp (cont)
. . .
sum = array_sum(array_A, SIZE_A);
average = compute_average(array_A, SIZE_A);
cout << "Array 1: sum = " << sum
<< " average = " << average << endl;

sum = array_sum(array_B, SIZE_B);


average = compute_average(array_B, SIZE_B);
cout << "Array 2: sum = " << sum
<< " average = " << average << endl;

return 0;
}

// function definitions
. . .

The Ohio State University 21


...
sum = array_sum(array_A, SIZE_A);
average = compute_average(array_A, SIZE_A);
cout << "Array 1: sum = " << sum
<< " average = " << average << endl;
sum = array_sum(array_B, SIZE_B);
average = compute_average(array_B, SIZE_B);
cout << "Array 2: sum = " << sum
<< " average = " << average << endl;
...

> arrayAverage3.exe
Array 1: sum = 19 average = 3.16667
Array 2: sum = 33 average = 4.125
>

The Ohio State University 22


arrayAverage5().cpp
. . .
const int ARRAY_SIZE(100);
int array[ARRAY_SIZE];
double average(0.0);
int numElements(0);
int x(0);

cout << "Enter list of non-zero integers (ending with 0): ";
cin >> x;
while (x != 0 && numElements < ARRAY_SIZE) {
array[numElements] = x;
numElements++;
cin >> x;
}

average = compute_average(array, numElements);


cout << "Average = " << average << endl;
...

The Ohio State University 23


arrayAverage5().cpp
// function prototypes
double compute_average(int array[], int numElements);
int array_sum(int array[], int numElements);

...
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;
...

double compute_average(int array[], int numElements)


{...}

The Ohio State University 24


Error: What will happen?
// function prototypes
double compute_average(int array[], int numElements);
int array_sum(int array[], int numElements);

...
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;
...

double compute_average(int array[], int numElements)


{...}

The Ohio State University 25


PASSING FIXED LENGTH
ARRAY PARAMETERS

The Ohio State University 26


Remember this problem?
. . .
int main()
{
double px, py, qx, qy, rx, ry;
double dist_pq, dist_pr, dist_qr;

// 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);
. . .

The Ohio State University 27


distArray2D.cpp
// global constants
const int DIMENSION(2); // size of each array

// 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;
...

The Ohio State University 28


New distArray2D.cpp (continued)
...
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): ";

for (int d = 0; d < DIMENSION; d++)


{ cin >> point[d]; }
}

The Ohio State University 30


Fixed Length
Array Parameter
function dist()
double dist(const double point1[DIMENSION],
const double point2[DIMENSION])
{
double distance(0.0);
double sum(0.0); // initialize sum

for (int d = 0; d < DIMENSION; d++)


{
double diff = point2[d] - point1[d];
sum = sum + diff * diff;
}
distance = sqrt(sum);
return distance;
}

The Ohio State University 31


function output_distance()
// NOTE: array parameter is const
void output_distance(const double point1[DIMENSION],
const double point2[DIMENSION],
double distance)
{
cout << "Distance between ";
output_point(point1);
cout << " and ";
output_point(point2);
cout << " = " << distance << endl;
}

The Ohio State University 32


function output_point()
// NOTE: array parameter is const
void output_point(const double point[DIMENSION])
{
cout << "(";
for (int d = 0; d < DIMENSION; d++)
{
cout << point[d];
if (d + 1 < DIMENSION) { cout << ","; }
else { cout << ")"; }
}
}

The Ohio State University 33


Change to (x,y,z) points
. . .
// global constants Must only change DIMENSION
const int DIMENSION(3);
. . .
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);
. . .
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.

 The array parameter to a function may be


declared as:

 Variable length array: Use empty square brackets and


a parameter for the number of elements in the array

 Fixed length array: Specify array with a fixed constant


in the square brackets
○ May use global variable here

The Ohio State University 35

You might also like