1-Dimensional Arrays
By definition, an array is collection of data that is contiguous in memory.
Like any variable, an array must be declared before it is used. There are also numerous
types of arrays. The first type is the standard 1 dimensional array. A 1D
array contains 1 long row of data.
Here is how to declare a 1D array:
where type is the data type of the array (i.e int, char, bool),
name is an appropriate name for the array and capacity is the amount of space in the array.
A rule is that the capacity cannot be negative or zero and is always an integer.
Notice the syntax of the array declaration. There are square brackets indicating the capacity.
The brackets are used to show there is an array declared.
Data in an array is accessed with the [] operator. What goes inside the brackets is
the index. By definition, each individual cell in an array is
called an index. Indicies of an array ALWAYS BEGIN AT 0 (zero) and end at capacity-1.
Here are some examples of declaring a 1D array:
Example 1:
Random numbers and 1D arrays
Download source code here (Right click - Save Tagret As...)
Below is a short program demonstrating a one-dimensional array:
The program will randomly fill a one-dimensional integer array with numbers from 1 to 10 and
then print them out. One possible output is this:
Run the program a few times and see the different outputs each time you run it.
Example 2:
Storing scores
Download source code here (Right click - Save Tagret As...)
This program will allow a uer to enter 5 quiz scores to calculate an average. It will
then output the average on screen.
The first thing the program does is calculate allow the user to input, via a for loop,
the 5 quiz scores. Second, it will add the values up to the total variable. Finally, it
will display the average on screen at the end.
The program is not a good one however. It doesn't handle any error checking for a negative
number when a user enters a score. Try to write the error proof code snippet. To see the answer,
click here.
2-Dimensional Arrays
A two dimensional array in C++ can be thought of as a table containing rows and columns.
A 2 dimensional array is still contiguous in memory but now has a row and a column. It contains
the other properties of an array in respect to the indicies, capacity and data type.
Here is how to declare a 2D array:
Here are some examples of declaring a 2D array:
Example 3:
Numbers and 2D arrays
Download source code here (Right click - Save Tagret As...)
This program will allow a uer to enter 5 quiz scores to calculate an average. It will
then output the average on screen.
The program will initialize the array to the values of r*c+1 and then print out the
values contained in those cells. This just demonstrates how to use a two dimensional array.
The output is:
1 1 1
1 2 3
Arrays and Functions
Arrays can be used as parameters in a function. Here is what they may look like:
Notice that for a one-dimensional array, you do not have to declare the size of it in the function.
However, for a two dimensional array, you must declare the second dimension.
When used in the function, all array parameters are passed by reference.
Again, by the reference rules of functions, if you change anything in the array inside a function, you also change it elsewhere.
Example 4:
Minimum array entry (1D and 2D)
Download source code for 1D here (Right click - Save Tagret As...)
These programs will find the minimum entry in both a 1 and 2 dimensional array. The two programs
are separate from each other.
1 DIMENSIONAL MINIMUM:
The above program will initialize the array to the values shown by doing a shorthand notation (see below).
It will then call the function minArray() and pass the entire array to the function. Notice that
the function is an int type and will return an int as the minimum number.
With the function, it will take as parameters the array itself and the capactiy of the array. C++ does
not know this so you must specify the capacity as a parameter. It will then search through each array
entry and return the minimum value to the main() function.
Download source code for 2D here (Right click - Save Tagret As...)
2D MINIMUM ENTRY:
This program is essentiallythe same as the previous only it delas with a 2D array. The function will
take an additional parameter for the column capacity as well as the columns for the array parameter.
Trace through the code in the funtion to see what it is doing.
Array Notations
There are some shorthand notations when dealing with arrays.
First, instead of using a for loop or nested loops to initialize an array to
zeros, you can do a shorthand notation:
This will place, at index 0, the integer 0 (or whatever else you place inside it) and by default,
set the rest of the cells to zero. If you had done this:
This will place, at index 0, the integer 1 and by default, set the rest of the cells to zero,
NOT 1!
If you wanted to fill an entire array manually, you may do this:
Another way of using arrays is as follows:
This will declare a string array called arr and set the capacity to 4, since you initialized
it to 4 different items.