|
Arrays
Before we go on to arrays, imagine that your program requires a
huge list of a specific data type to be held in memory, so you can
call up any item in the list from your program when needed. How
would you do that? Of course assigning individual identifier to
each of the items in the HUGE list could be an option, however,
how long would it take you to assign one million different numbers
to one million different variables? This is where an array, also
called a vector, comes in handy. Let’s look at this example:
Problem: Tonight’s winning lottery numbers are 3, 17, 22, 33,
36, 41
Now, we want <something> to hold these numbers in our
program.
Option 1:
long winNums = 31722333641;
Option 2:
int num1 = 3, num2 = 17, num3 = 22, num4 = 33, num5 = 36, num6
= 41;
Option 3:
int winNums[6];
winNums[0] = 3;
winNums[1] = 17;
winNums[2] = 22;
winNums[3] = 33;
winNums[4] = 36;
winNums[5] = 41;
Ok, let’s look at this. First of all, ask yourself if you
want your six numbers to look like this 31722333641. Well, do you
like them? Okay, now I want the third lottery number to be
assigned to a new variable int newNum, how would you do
that? You can just use int newNum=22, easy! Well before you
argue, may I ask what’s the point of storing the six numbers to long
winNums then? Therefore, option 1 is unacceptable.
Now let’s look at option 2. Well, this option seems nice, so
why should we not choose this option? The answer is efficiency.
How? Let’s see, what if you were asked to print out all six
numbers to the screen? Easy!
cout << num1 << " " << num2
<< " " << num3 << " "
<< num4 << " "<< num5 << "
" << num6;
Now, let’s see how you can do this with the third option:
for (int j=0; j<5; j++)
cout << winNums[j] << " ";
Not much difference? In this example, yes. However, if the
number of numbers :P were increased to 1000, which one do you find
more preferable? I will leave that to you.
Yes, you are right the third one is an array. If you get
confused with the syntax and handling, don’t worry, read on.
One-dimensional array:
declaration syntax: any_type any_variable[#_of_items];
example: int winNums[6];
It’s almost the same as declaring a single variable, however
with the power of arrays, you can make a lot of works done faster.
For example, to get 10 number inputs from the user.
you would do something like this:
int numbers[10];
for (int j = 0; j < 10; j++)
cin >> numbers[j];
To access the first number, use numbers[0], why is the
index 0 instead of 1? Well it’s just the way it is, all the
arrays start with the 0th element, and ends with
SizeOfArray – 1. So to access the last number, you should use number[9]
instead of number[10].
to output the numbers:
for (int k = 0; k < 10; k++)
cout << numbers[k];
This is basically how arrays work. Now let’s look at our next
topic, Strings
Connection with Strings:
In C++, strings can be treated as a type or an array. When do
we use strings? Well let’s look at the definition of string
first. A string is an array of characters. So, when do we use it?
We use it to store names of people, names of places, or just
anything that involves a combination of letters, however, as
numbers can be stored as characters, a string can be an array of
numbers, too. Let’s look at this example:
#include <iostream.h>
int main()
{
char a_string[20]; //declared a string of max 20 characters
char another_string[20]; //another string of max 20 characters
cout << "Please enter your first name: ";
cin >> a_string; //input a single word without spaces
cout << "Please enter your full name: ";
cin.getline(another_string, 20); //input multiple words with
spaces
cout << "Your first name is: "<< a_string
<< "\n and your full name is "
<< another_string;
return 0;
}
Okay, now let’s split this down. If you have paid attention
during your previous classes, you should be able to interpret this
code. One thing I have to mention though, is the difference
between cin and cin.getline(). cin terminates your input whenever
it receives the blank_space character ‘ ‘, while cin.getline()
gets the whole line and terminates only at the end of line
character. ‘\n’. A special character ‘\0’ would be
appended to every input to indicate the end of a string. But for
now, you don’t really have to mind that. Just remember a string
is a combination of characters, and it is really an array holding
characters.
Two-dimensional arrays:
I am sure you have played the game tic-tac-toe before, and that’s
exactly what a two-dimensional array is like, arrays stretching in
two dimensions. A two-dimensional array is sometimes called a
matrix. Yes, it’s just like the matrices in your math class.
Well, what is the use of a matrix in programming anyways? It is
useful, because it acts like a table, you know how useful tables
are in science and math right? Yes, it is as useful in C++. What’s
more, it is loop-able. Just like one-dimensional arrays, you
access elements in a matrix with indices. But this time, it’s 2
indices.
Let’s look at this 3x3 matrix:
Columns:
Rows 0 1 2
0 [1][2][3]
1 [4][5][6]
2 [7][8][9]
How would you put this matrix into C++? Look at the following
example:
#include <iostream.h>
int main()
{
int matrix[3][3]; //declare a matrix of 3 rows and 3 columns
for (int row = 0; row < 3; row++) //loop the row indices
for (int column = 0; column < 3; column++) //loop the column
indices
cin >> matrix[row][column]; //input data into the matrix
…
return 0;
}
As you can see, a matrix is nothing more than a double array,
and it can store not only numbers, but also any type of values.
For example:
char matrix[4][7];
would be a 4x7 matrix holding characters.
So much about arrays. Let’s go on to another topic.
NEXT |