Arrays and Functions
- Things to note about C-style arrays:
- An array is not a type
- An array is a primitive C-style construct that consists of many items
stored consecutively and accessed through a single variable name (and
indexing)
- This is actually done by remembering the starting address of an
array, and computing an offset
- The name of an array acts as a special kind of variable -- a
pointer -- which stores the starting address of the array
- An array can be passed into a function as a parameter
- Because an array is not a single item, the array contents
are not passed "by value" as we are used to with normal
variables
- The normal meaning of "pass by value" is that the actual argument
value is copied into a local formal parameter
variable
- In the case of arrays, just the pointer is copied as a
parameter. We'll see this in more detail when we get to
pointers
- When an array is sent into a function, only its starting address is
really sent
- This means the function will always have access to the actual
array sent in
- Returning an array from a function works similarly, but we need
pointers to use them well (not yet covered)
- Example function:
void PrintArray (int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << ' ';
}
Note that:
- The varibale arr acts as the local array name inside the
function
- There is no number in the brackets. int [] indicates that
this is an array parameter, for an array of type int
- It's usually a good idea to pass in the array size as well, as
another parameter. This helps make a function work for any size
array
- Sample call to the above function:
int list[5] = {2, 4, 6, 8, 10};
PrintArray(list, 5); // will print: 2 4 6 8 10
Using const with array parameters
Here's an
example program illustrating a couple of functions that take array
parameters.
// example of arrays, and functions using arrays
#include
using namespace std;
void PrintArray(const int[] , const int);
void SwapElements(int[] , int, int);
int main()
{
int list1[5] = {2, 6, 9, -10, 12}; // array of 5 integers
int i; // for loop indexing
int list2[20]; // array size 20, uninitialized
cout << "\nlist1 = ";
PrintArray(list1, 5);
cout << "\n";
// compute the sum of the list elements
int total = 0;
for (i = 0; i < 5; i++)
total = total + list1[i];
cout << "Sum of list1 elements = " << total << '\n';
// initialize the array to 2, 4, 6, 8, 10, ...
for (i = 0; i < 20; i++)
list2[i] = 2 * i + 2;
cout << "\nlist2 = ";
PrintArray(list2, 20); // display the contents
cout << '\n';
// create a third list, and get user to enter the data
int list3[10];
cout << "Enter 10 integer values: ";
for (i = 0; i < 10; i++)
cin >> list3[i]; // user enter the items
cout << "\nlist3 = ";
PrintArray(list3, 10); // display the contents
cout << '\n';
// find the largest element of list3
int max = list3[0]; // largest so far
for (i = 1; i < 10; i++)
if (list3[i] > max)
max = list3[i];
cout << "Largest number in list3 is: " << max << '\n';
cout << "Swapping elements 4 and 7\n";
SwapElements(list3, 4, 7);
cout << "\nlist3 = ";
PrintArray(list3, 10);
cout << "\n\n";
return 0;
}
void PrintArray(const int arr[], const int size)
{
int i;
cout << "{ ";
for (i = 0; i < size-1; i++)
cout << arr[i] << ", ";
cout << arr[size-1] << " }"; // print last item
}
void SwapElements(int arr[] , int a, int b)
// swap arr[a] and arr[b]
{
int temp;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
Practice exercises
Try writing the following functions (each takes in one or more arrays as
a parameter) for practice:
- A function called Sum that takes in an array of type
double and returns the sum of its elements
- A function called Average that takes in an integer array and
returns the average of it's elements (as a double)
- A function called Reverse that takes in an array (any type)
and reverses its contents
- A function called Sort that takes in an array of integers and
sorts its contents in ascending order
- A function called Minimum that takes in an array of type
double and returns the smallest number in the array
Examples of some functions written in lecture class, including
Largest, Smallest, Sum, Average
// example of arrays, and functions using arrays
#include
using namespace std;
void PrintArray(const int[] , const int);
void SwapElements(int[] , int, int);
int Sum(const int arr[], const int size);
double Average(const int arr[], const int size);
int Largest(const int arr[], const int size);
int Smallest(const int arr[], const int size);
int main()
{
int list1[5] = {2, 6, 9, -10, 12}; // array of 5 integers
int i; // for loop indexing
int list2[20]; // array size 20, uninitialized
cout << "\nlist1 = ";
PrintArray(list1, 5);
cout << "\n";
// compute the sum of the list elements
cout << "Sum of list1 elements = " << Sum(list1, 5) << '\n';
cout << "Average of list1 elements = " << Average(list1, 5) << '\n';
// initialize the array to 2, 4, 6, 8, 10, ...
for (i = 0; i < 20; i++)
list2[i] = 2 * i + 2;
cout << "\nlist2 = ";
PrintArray(list2, 20); // display the contents
cout << '\n';
cout << "Sum of list2 elements = " << Sum(list2, 20) << '\n';
cout << "Average of list2 elements = " << Average(list2, 20) << '\n';
// create a third list, and get user to enter the data
int list3[10];
cout << "Enter 10 integer values: ";
for (i = 0; i < 10; i++)
cin >> list3[i]; // user enter the items
cout << "\nlist3 = ";
PrintArray(list3, 10); // display the contents
cout << '\n';
// find the largest element of list3
cout << "Largest number in list1 is: " << Largest(list1, 5) << '\n';
cout << "Largest number in list2 is: " << Largest(list2, 20) << '\n';
cout << "Largest number in list3 is: " << Largest(list3, 10) << '\n';
cout << "Smallest number in list1 is: " << Smallest(list1, 5) << '\n';
cout << "Smallest number in list2 is: " << Smallest(list2, 20) << '\n';
cout << "Smallest number in list3 is: " << Smallest(list3, 10) << '\n';
cout << "Swapping elements 4 and 7\n";
SwapElements(list3, 4, 7);
cout << "\nlist3 = ";
PrintArray(list3, 10);
cout << "\n\n";
return 0;
}
void PrintArray(const int arr[], const int size)
{
int i;
cout << "{ ";
for (i = 0; i < size-1; i++)
cout << arr[i] << ", ";
cout << arr[size-1] << " }"; // print last item
}
void SwapElements(int arr[] , int a, int b)
// swap arr[a] and arr[b]
{
int temp;
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
int Sum(const int arr[], const int size)
{
int total = 0;
for (int i = 0; i < size; i++)
total = total + arr[i];
return total;
}
double Average(const int arr[], const int size)
{
return Sum(arr, size) * 1.0 / size;
}
int Largest(const int arr[], const int size)
{
int max = arr[0]; // the answer SO FAR
for (int i = 1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return max; // largest number
}
int Smallest(const int arr[], const int size)
{
int min = arr[0]; // the answer SO FAR
for (int i = 1; i < size; i++)
if (arr[i] < min)
min = arr[i];
return min; // largest number
}
No comments:
Post a Comment