There are many situations in which it's natural to ask for

This class is just going to look at functions that operate on arrays, and the few new concepts that arise with them.

In reality, there's nothing new at all! It's just that the consequences of the following fact:

It is the pointers to the arrays that get manipulated in our programs, not the arrays themselves.

Computing averages with two function calls

We'll look at this from the perspective of our simple program for computing averages. Here's that program. Essentially we'd like the bulk of the program to consist of two function calls:
  1. We need to call function readints() which creates an array A and read N integers from input
    
    int *A = readints(N,cin);
    
  2. Then we need to call function sum() to compute the average:
    
     double average = sum(A,N) / double(N);
    
In defining these two functions, we'll see how arrays get passed in and out of functions.

Function readints(): How to return arrays

Prototype

The prototype of the first function is as follows:

int* readints(int N, istream &IN);
This function should return an array of N ints, populated with values read in from IN.

Definition

The basics of the function should be fairly straightforward. The important thing is to understand what happens! So, here's the function definition:

int* readints(int N, istream &IN)
{
  // Create the array
  int* B = new int[N];
  
  // Read values into the array
  for(int i = 0; i < N; i++)
    IN >> B[i];

  // Return pointer to array
  return B;
}

Pictorial depiction

And here's a pictorial depiction of what goes on. Note:

In summary:

  1. A function can create arrays with new.
  2. Then then it can return the arrays by returning a pointer to the block of memory allocated by the call to new.

Function sum(): How arrays are passed (and modified)

Prototype

Arrays can be passed into functions by passing a pointer to the block of memory that comprises the array. So, the prototype of the function sum() is as follows:

int sum(int* A, int N);
Note about the pointer A: Note about N:

Definition

So, getting back to our example, we need to implement the function int sum(int* A, int N);. Hopefully the definition of this function looks straightforward.

int sum(int* A, int N)
{
  int total = 0;
  for(int i = 0; i < N; i++)
    total = total + A[i];
  return total;
}

Pictorial depiction

The question is this: What really goes on when we run it? Hopefully the pictures below, which illustrate what goes on in a sample run of the above function, will make clear to you what really goes on when arrays are passed to functions.

In summary,

Problems

  1. A contains predicate. Write function bool contains(string *A, int N, string s); that tells you whether or not the string s is contained in the array A.
  2. A getarray function. Write a function int* getarray(int N, int x); that allocates and returns a pointer to an array of N ints, each initialized to the value x.
  3. A shiftleft function. Write a function double shiftleft(double *A, int N, double x); that shifts all the elements in array A to the left by 1 position, puts the value x in the rightmost position in the array, and returns the value that had previously been in the leftmost position.
  4. An average function for arrays of doubles.