PIC 10A Lecture 13

Arrays in functions

You can use both array indexed variables and entire arrays as arguments to functions. Return an array

Indexed variables as arguments

#include<iostream>

using namespace std;



// return the half of x
double half(double x);


// increase x by inc
void increaseBy(double& x, double inc);

int main() {
// declare an array
double array[] = {3.2, 4.3, 7.2}, inc = 2.9;
int size = 3; // size of the array
int i; // index used in loops

// show the elements of the array
cout << "The elements in the array are :\n";
for(i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl<< endl;

// function call
cout << "Half of a[2] is " << half(array[2]) << ".\n";
// show the elements of the array
cout << "The elements in the array after "
<< "function call half(array[2]) :\n";
for(i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl << endl;

// show the elements of the array
// after increaseBy function
cout << "The elements in the array after "
<< "function call increaseBy(array[i],inc) :\n";
for(i = 0; i < size; i++) {
increaseBy(array[i],inc);
cout << array[i] << " ";
}
cout << endl;
return 0;
}

double half(double x) {
return x/2;
}

void increaseBy(double& x, double inc) {
x+=inc;
}
Output
The elements in the array are :
3.2 4.3 7.2

Half of a[2] is 3.6.
The elements in the array after function call half(array[2]) :
3.2 4.3 7.2

The elements in the array after function call increaseBy(array[i],inc) :
6.1 7.2 10.1

Entire arrays as function arguments

#include<iostream>

using namespace std;

// ask the user to input an array with the given size
void fillUp(double a[], int size);

// display the array
void display(double a[], int size);

// find the average
double average(double a[], int size);

int main() {
double score[5];
int size = 5;

// ask the user to input score
fillUp(score, size);

// display the scores
cout << "The scores are : \n";
display(score, size);
cout << endl;

// the average
cout << "The average of all the scores is "
<< average(score,size) << endl;

return 0;
}


void fillUp(double a[], int size) {
int i; //index
cout << "Enter " << size << " numbers:\n";
for(i = 0; i < size; i++) {
cin >> a[i];
}

cout << "The last array index used is "
<< (size - 1) << endl;

}


void display(double a[], int size) {
int i; // index
for(i = 0; i < size; i++) {
cout << a[i] << " ";

}

}



double average(double a[], int size) {
int i; // index
double sum = 0;
for(i = 0; i < size; i++) {
sum+=a[i];

}

return sum/size;
}



Output
Enter 5 numbers:
22.2 35 97.25 87.5 50.3
The last array index used is 4
The scores are :
22.2 35 97.25 87.5 50.3
The average of all the scores is 58.45

Explanation

const parameter modifier

Example
#include<iostream>

using namespace std;

// The max score an array is maxScore
// display the score in 100% scale
void showScore(double score[], int size, double maxScore);

// copy two functions from previous example
// reuse the code, this is something good about writing functions
// as the user to input an array with the given size
void fillUp(double a[], int size);

// display the array
void display(double a[], int size);

int main() {
double score[5];
int size = 5;

// ask the user to input score
fillUp(score, size);

// display the scores
cout << "The scores are : \n";
display(score, size);
cout << endl;

// display the score again
showScore(score, 5, 50);

// display the scores
cout << "After the function call showScore, "
<< "the scores are : \n";
display(score, size);
cout << endl;
return 0;

}



void showScore(double score[], int size, double maxScore) {
int i;
cout << "Scores are : \n";
for(i = 0; i < size; i++) {
score[i] = score[i]/maxScore*100; // change the array accidentally
cout << score[i] <<"% ";
}
cout << endl;
}


void fillUp(double a[], int size) {
int i; //index
cout << "Enter " << size << " numbers:\n";
for(i = 0; i < size; i++) {
cin >> a[i];
}

cout << "The last array index used is "
<< (size - 1) << endl;

}



void display(double a[], int size) {
int i; // index
for(i = 0; i < size; i++) {
cout << a[i] << " ";
}

}

Output
Enter 5 numbers:
22 33 12.3 47.5 40.25
The last array index used is 4
The scores are :
22 33 12.3 47.5 40.25
Scores are :
44% 66% 24.6% 95% 80.5%
After the function call showScore, the scores are :
44 66 24.6 95 80.5

You can change
void showScore(double score[], int size, double maxScore)
To
void showScore(const double score[], int size, double maxScore)

After these changes the above program can't be compiled because you accidentally changed the score.
Here is the new program
#include<iostream>

using namespace std;

// The max score an array is maxScore
// display the score in 100% scale
void showScore(const double score[], int size, double maxScore);

// copy two functions from previous example
// reuse the code, this is something good about writing function
// as the user to input an array with the given size
void fillUp(double a[], int size);

// display the array
void display(double a[], int size);


int main() {
double score[5];
int size = 5;

// ask the user to input score
fillUp(score, size);

// display the scores
cout << "The scores are : \n";
display(score, size);
cout << endl;

// display the score again
showScore(score, 5, 50);

// display the scores
cout << "After the function call showScore, "
<< "the scores are : \n";
display(score, size);
cout << endl;
return 0;
}



void showScore(const double score[], int size, double maxScore) {
int i;
cout << "Scores are : \n";
for(i = 0; i < size; i++) {
cout << score[i]/maxScore*100 <<"% ";
}
cout << endl;
}


void fillUp(double a[], int size) {
int i; //index
cout << "Enter " << size << " numbers:\n";
for(i = 0; i < size; i++) {
cin >> a[i];
}
cout << "The last array index used is "
<< (size - 1) << endl;

}


void display(double a[], int size) {
int i; // index
for(i = 0; i < size; i++) {
cout << a[i] << " ";
}

}

Functions that returns an array

#include<iostream>

using namespace std;

// reverse array a and store it in another array
void reverse(int a[], int size, int reverse[]);

// display the array
void display(int a[], int size);

int main() {
int size = 5;
int a[] = {5,10,15,20,25};
int b[5];

cout << "Before calling the reverse function\n"
<< "array a is : \n";
display(a,size);
cout << endl;
reverse(a,size,b);
cout << "After calling the reverse function\n"
<< "array a is : \n";
display(a,size);
cout << endl;
cout << "array b is : \n";
display(b, size);
cout << endl;
return 0;

}


void reverse(int a[], int size, int reverse[]) {
for(int i = 0; i < size; i++) {
reverse[i] = a[size - 1 - i];
}
}

void display(int a[], int size) {
int i; // index
for(i = 0; i < size; i++) {
cout << a[i] << " ";
}

}
Output
Before calling the reverse function
array a is :
5 10 15 20 25
After calling the reverse function
array a is :
5 10 15 20 25
array b is :
25 20 15 10 5