CS 102 (Fall '03)
[Schedule]
[Examples] [Programs]
[Notes & Reference]
[Syllabus] [Lab
& TA] [Tests]
[Grades]
Consider how you might write a program to read in some test scores and average them.
- Our first attempt might be to use a variable to store each test score
The problem with this approach is that we must know ahead of time how many test scores there will be, and changing this number requires editing and recompiling the program.- The second approach is to read the scores using a loop, accumulating the sum of the scores and counting the number of the scores as we go.
This approach is promising, but if the requirements changed and we had to echo all the scores before displaying the average, we wouldn't be able to do this. Once the scores have been entered, we accumulate their value, but we cannot retrieve the individual scores.- In order to echo all the scores before displaying their average, we could use a recursive approach.
This does work for any number of scores, however the scores are displayed in reverse order.
The most natural way to solve the scores problem is to use arrays.
- This solution declares a fixed-size array inside of main, which stores all the scores as they are entered.
This allows use flexibility in how to use the scores, as they are all stored to be retrieved any way we wish. One limitation is that we must know the maximum number of scores ahead of time.
A second version reads straight into the array, instead of into an intermediary variable.- We can pass the array to a function as a parameter, where the changes in the array are automatically reflected back to the calling part of the program.
Rather than having to store multiple values of the same type in different variables, we can store them in an array. This is appropriate when we have a group of values of the same type, such as a list of numbers, or a group of characters representing a playing board for a game.
To declare an array, we use:
type arrayName[ size];
where type can be any type (e.g. char, int, float), arrayName is an identifier you make up, and size indicates how many items will be stored in the array.
For example
int theScores[ 10];
declares an array called theScores that can hold 10 integers. In the array shown above the first element is the 0th element, and the last is the 9th element. This element number is known as the index (a.k.a. the subscript) , which starts at 0 and goes to n-1 for an array of n elements.
Counting Characters
We can write a program using cin.get( c) to count the number of each character on an inputline. Instead of reading in one character at a time, we could read the entire line into an array in a program using cin.getline( array, maxSize).
This program reads characters from an input file and counts how many there are of each character.Searching and Sorting
This program uses binary searching to find a particular integer key within an array of ordered numbers, and bubble sort to put some integers in an array into ascending order.
Character Stack
A character stack can be used to check for balanced parenthesis in an input expression.
Why do we need 2-D arrays? Some information is easier to manage that way (tables, text, matrices, etc.)
Examples:
- 2-D array C++ program to store results of temperature conversion table (includes floating point formatting)
(and the equivalent in C)- Character recognition program
Passing 2-D arrays to functions
In the above example, note that the first dimension of a multi-dimensional array can be omitted. This is because:
- C does not check to make sure you stay within bounds of an array
- Multi-dimensional arrays are stored row-major
We could "spoof" a 2-D array using a one-dimensional array, which is actually what happens inside the machine, since computer memory is addressed linearly.
[CS Dept] [UIC] [Prof. Reed]