Software Design Using C++Basics of ArraysIntroductionIn this Web page we will look at the simplest kinds of arrays. Thus we will only look at 1-dimensional arrays and won't do any complicated processing with them. For more advanced information on arrays, look at the intermediate section on arrays in these Web pages. A 1-dimensional array is used to hold a sequence of items all of the same type. An index number is used to specify each storage location in the array. In C++ indexing begins with 0. For an array named A, the first storage location is A[0], the second location is A[1], etc. An array of 8 integers would look like this:
Let's assume that the name of this array is A. We could place the 20 into array location 0 by using the following assignment statement:
We could read a number into an array location, also. For example, to read in a number into array location 1 we might use something like this:
Since we often want to place numbers into all of the array locations, we are likely to write a loop to handle things. In many cases we know the number of items to be processed, so that a FOR loop is typical. Here is an example of a FOR loop that reads numbers into all 8 locations of our array A. This is followed by another FOR loop that writes out all of the data in array A.
Array of Integers ExampleLook at array1.cpp for a more interesting example using an array of integers. This program asks the user how many numbers are to be entered, then reads that many numbers into an array. Finally, the program finds and prints the average of the numbers.
At the top of this program you find the following code to set up one
constant and one new type.
Although not absolutely necessary, it is a great
convenience to have a one word name for such a type. We will consistently
use a
If you read the comment section for this function, you will see that it
returns data in both parameters. Normally a parameter must be a
reference parameter,
the kind that uses an ampersand, in order to return an answer. Why, then,
is there no ampersand on the
The main function creates an array of type
The bulk of the work in the main function consists in calling the two
helping functions, as shown below. The
Note how these functions are commented with the "Given, Task, Return"
method. Any values passed into a function are listed, by name, and
described in the "Given" section. A parameter that contains only garbage
data when the function is called (such as
The bulk of the code for the
The A Better Average ProgramNow, just because the above program works doesn't mean that it is a reasonable way to find the average. In fact it is unreasonable. There is no need to save all of those numbers up in an array when we could have just added them up as the user entered them. Then the average could be found as this sum divided by the number of items. What could be simpler? (A good general rule is to never use a complex method when a simpler one is available.) Our program above wastes space by needlessly storing numbers in an array. In some applications the number of items might be huge so that an array would waste a lot of space. Don't use arrays unless there is a good reason to save all of that data! A more sensible program to handle the average is array2.cpp. It uses the approach described above of just summing the numbers as they are entered. The numbers themselves are not saved. There is no need for an array, and the code is so short that it can easily all be placed into the main function without having things look too complicated. Where an Array is NeededWhen would we need to save up data in an array? Basically whenever we need to look at the data more than once. The program array4.cpp is such a case. In this program we enter some integers and average them, that's one pass over the data. But after finding the average we want to print out the numbers that are below this average. That requires a second pass over this data. There is no way to print the below average numbers until you know what the average is! An array is just the thing needed to save the data for that second pass. This program is much like our older one. Essentially we have just added a new helping function to print out the below average numbers and called it at the appropriate place in the main function. Dividing things up into functions, each of which handles one main task, helps when adding new functionality or when adding the same kind of thing to a new program. The bulk of the code for our new function is shown below:
The main thing in here is the comparison of a number from the array with
the average. Each time around the loop the index Array of Characters (a String)An array of characters is a simple way to implement a string, something that could hold someone's name or other text data. Safer and more advanced ways to handle strings can be used once you have some experience with using objects. (See the STL string class as well as the discussion of buffer overflows and safely copying string with the STL string class.) When implementing a string as an array of characters, the array just holds one character after another, starting at index 0. After the last character of data, a NULL character is placed in the next array location. It does not matter if there are unused array locations after that. They will simply contain garbage data. A NULL character is a special character (represented by the ASCII code 0) used to mark the end of this type of string. A literal character array is written in double quotes, as in "hello". These have been used before as messages in our output statements. A picture of the array holding "hello" would be as follows, assuming that the array is of length 10:
We need to know how to get data into a character array, and how to print it. Those are the two basic operations that we will learn here. For more advanced information on characters arrays, look at the characters arrays topic within the intermediate page on arrays.
For our purposes here, we will use the array3.cpp
example. Toward the top of this program you see that we have set up one
constant and one type.
Since this program is short, the code for the main function has been
copied in below, in slightly simplified form.
Note how you create a character array variable: you
simply say it has type
So, one way to get a string into a character array is to place it there when you declare the variable. However, from what was said above, it would be illegal to try to assign it into the variable after the declaration. Thus the following is not allowed:
Another common way to get a string into a character array is to read it in.
The program above uses the
This works to some extent. However, it stops at the first blank space.
Since there is blank space between a person's first and last name, this is
not a good method for reading a name. The Printing out a string stored in a character array is very easy. You just output it the same way you would output the value of an integer or float variable. The last few lines of our example program illustrate this. Related Items
|