Homework 19: Meet the Array
Concepts
In this assignment, focus on learning how to declare (and initialize) arrays, how to access array values, and how to store values in arrays.
Collections of Things
To date, you've focused mostly on working with data one "piece" at a time. But in the real world, we're often concerned with collections of data: scores, hourly air temperatures, pressure readings over time, and colors of socks. In computer science, we call these collections "data structures" -- things that contain data. Perhaps the most simple data structure is the array: a container that holds multiple instances of only one type of thing. For example, you might have an array called scores
that contains a bunch of ints
, or an array called windSpeed
that contains a bunch of doubles
.
There are a couple of "rules" you need to remember when dealing with arrays in C++.
First, arrays can only store values of one specific type, so you can't store both cats and dogs in the same array. Sometimes you can "bend" this rule, depending on type compatibility and coercion (eg, int
s and double
s, or int
s and char
s).
Second, arrays are said to have a "fixed size" meaning that when you declare them, you must tell the machine how many elements you intend to store in it, and you can never store more than that number of items in the array.
Why does the machine need to know how many elements you intend to store in the array? In a nutshell, to allocate enough memory for N pieces of data of a given type. In the example above, the machine needs to allocate enough memory to store 26 char
values.
Third, you need to remember that arrays are "zero-indexed" meaning the first item in the array is "item 0" and the second is "item 1" and so on. This is important to remember, because it is easy to confuse the size of the array with the last index of the array. If you initialize an array like this:
What are the valid indices? 0 through 6. Notice how the size of the array is 7, but the last index is 6.
C++ Array Syntax
You should have seen some examples of array syntax using [
and ]
in class and in your reading. As such, we won't go over it in detail here. However, here's a brief summary.
int y = 2;
int z;
int scores[10];
scores[1] = 27;
scores[y] = 2;
z = scores[x] + scores[y];
cout << scores[x + 1];
You should definitely understand what the syntax means in the example above. How many numbers can score
hold? 10. What are their "names"? scores[0]
through scores[9]
. What is now the value of z? 29. What is printed to the screen? 2. What does the array look like?
27 | 2 | ||||||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|
Be sure to review your lecture notes and reading, or come see us if you don't understand the syntax above.
Arrays and Loops
Since you're aware of how to use loops and since you now know how to create arrays, you should recognize that there is a lot of hidden "power" in combining these two elements together. In fact, loops and arrays work so well together, it's rare you'll see one without the other -- they're best friends (but only friends, mind you). Let's take a look:
int shotsOfWheatgrass[DAYS_IN_WEEK] = {0, 1, 1, 1, 1, 7, 5};
for (int i = 0; i < DAYS_IN_WEEK; ++i) {
cout << "On day " << i << " I drank "
<< shotsOfWheatgrass[i] << " shots.\n";
}
Or better yet,
int shotsOfWheatgrass[DAYS_IN_WEEK] = {0, 1, 1, 1, 1, 7, 5};
for (int i = 0; i < DAYS_IN_WEEK; ++i) {
cout << "On day " << i << " I drank " << shotsOfWheatgrass[i];
if (shotsOfWheatgrass[i] == 1) {
cout << " shot.\n";
} else {
cout << " shots.\n";
}
}
Do you see how inside the loop, the value for i
is used to access each subsequent value in the array?
Instructions
Using the skeletal main.cpp
provided, create a program that prompts the users for five numbers, stores them in an array, determines which number is the biggest in the array, and then prints the biggest number to the screen. An example interaction follows:
Enter 5 numbers and I will tell you which is the largest.
Number 1: 8
Number 2: 6
Number 3: 7
Number 4: 5
Number 5: 3
So awesome!
The largest number was 8.
How should you prompt the user for each number? From within a for
loop.
You'll notice that a function prototype for max
has been defined for you. Below main
, implement the function definition such that the max
function's return value is the largest number in the array it is passed.
Requirements and Rubric
A friendly message from The Terminator, our grading program
*bzzzt* Hel-lo. I will check for the specific output:
- "Enter 5 numbers"
- "The largest number was N" where N is the largest number entered.
Your program must prompt the user to enter a number 5 times.
Your program must terminate after printing the output.
*bzzzt*
This work is worth 80 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 2 | |
Correct submission of src directory as a .zip file. | 3 | |
Uses constant ARRAY_SIZE for the array size. |
5 | 5 elements |
Correctly creates an int array that holds ARRAY_SIZE items |
5 | |
Correctly prints "Enter N numbers" where N is the same as ARRAY_SIZE. | 5 | |
Uses a for loop to collect user input. |
10 | |
Prompts with "Number 1" through "Number 5" | 10 | Use the for loop counter variable... with a little modification. |
Stores the five numbers in an array | 10 | Use the for loop's counter variable. |
Correctly prints "The largest number was X" where X is the largest number entered. | 10 | |
Correct implementation of max function |
20 |
Concepts Exercised: Collections of things, declaring facts, repetitive tasks, I/O, functions
© 2011 Yong Joseph Bakos.