Arrays

Say that you wanted to keep track of 100 people in variables, you could make a loop and keep them all in one variable, but every time you set the variable to something different, the last person would get erased. This is where arrays come in. An array is one variable name with a way to reference its many different items.

The first thing you need to know is how an array works. An array just like a variable, only after it there are square brackets [] with a number inside (While you can leave the number blank and set the values during initialization, it is better practice to put a constant in the square brackets). This number is known as an index. The index starts at 0 and goes to any number you want minus 1. For example if you put a 9 in for the index it would go from 0 to 8. Arrays are commonly found in for loops, but can be found in any loop using a counter. The reason for loops are popular is because it has the counter right when you program it. The counter would go in the index so that every time it looped, the counter would be incremented and thus the variable would not be wiped out. When we first declare the array, the index would be the number of elements we want. When declaring an array, the index has to be a constant and not a variable. While it isn't necessary, it is good practice to set the variables to something. To do this we first initialize the array then equal it to curly braces {} and the numbers we want to initialize them to. They go in order starting at 0 up to the upper bounds minus 1, and if you stop shot of that then the rest will be set to 0.

For this example we're going to input 10 full names and their grades and then display them backwards:
string name[10] = {"Bob", "Bob Evans"};
int x;
for (x=0; x<10; x++)
{
getline(cin,name[x]);
}
for (x=9; x>-1; x--)
{
cout<<name[x];
}
u


If your loop loops more than the number of items in the array variable, then you have a problem. While it will compile, when it is run your program will probably crash and could be touching things it isn't supposed to be touching, like your device drivers. If you try this and the anti virus goes off, then stop your program.

Vectors

What if we didn't know how many instances of the array we need? A vector is basically a one dimensional array that can be defined with a variable where an array can't be. In fact a vector can even add on the fly, so if you were writing a program where a teacher would enter her students in the beginning of the semester and needed to add a new one, you could actually do this on the fly, without reinitializing it. The first thing you want to do when using vectors is including the vector header file. Then in the code we add the keyword vector then we put the date type inside of less than and greater than brackets, then the variable name and in parenthases the upperbounds of the array -1. Vectors, like arrays, begin at 0 and not 1. Accessing a vector's elements is the same way as doing it with an array. To add to the upperbounds we do put the variable name and then a period then push_back with two parenthases inside will be the value you want the new upperbounds to be (this can be a variable or a constant).

With the following code, a getline would not work due to some problems it has when working with a vector with an index of 0, so we put the first and last name in two seperate variables and then put the last name into the same variable after the first name. Before we concatenate the two, we need to put a space in there so thats what the third variable, space comes in. I've commented on some of the more confusing stuff and made the code pertaining to vectors red.

#include <iostream>
#include <string>
#include <vector> //Include the file header
using namespace std;
void main()
{
int x, y, t;
t=0;
char yorn, space;
string temp;
space=' '; //Used for adding a space between the names
cout<<"Enter the number of names you wish to enter. ";
cin>>x; //Set the initial upperbounds for the vector
vector<string>name(x); //Declares the vector
for (y=0; y<x; y++) //Loop for the same amount of the upperbounds of the vector
{
cout<<"Enter a name"<<y;
cin>>name[y]>>temp; //Get the name from the user
name[y]+= space; //Adds a space to the first name
name[y]+= temp; //Adds the last name to the name vector
}
do
{
cout<<"You have "<<y<<" names entered, would you like to enter more?";
cin>>yorn;
if (yorn=='y' || yorn=='Y') //Checks if you'd like to enter more names
{
cout<<"How many more would you like to enter?";
cin>>x;
for(t=y; t<(x+y); t++) //Loops for the number of names you want to enter
name.push_back(""); //Adds to the upperbounds of the array
cout<<"Enter a name";
cin>>name[y]>>temp; //Get the name from the user
name[y]+= space; //Adds a space to the first name
name[y]+= temp; //Adds the last name to the name vector
}
else
{
break; //If you enter N it will break out of the loop
}
}while (yorn=='y'); //It will loop while the variable yorn is y
system("PAUSE");
}


Multidimensional Arrays


The most familiar table students are familiar with is their multiplication table. Twelve rows across and 12 rows down. If you wanted to write a program that displays the multiplication table, or any table, you would want to use a multidimensional array. With multidimensional arrays the same rules apply for one dimensional arrays, the indexes have to be a constant. The difference with multidimensional arrays is that they have more than one index. The following program, while not looking very pretty, is actually the multiplication table.

const int NumRows = 13; //Set the number of rows to 13 because we want to include 12
const int NumColumns = 13; //Set the number of columns to 13 because we want to include 12
int x, y;
int multiplication[NumRows][NumColumns]; //Declare the multidimensional array
char space=' '; //space to seperate the numbers
for (x=0; x<13; x++)
{
cout<<x<<space; //Displays the numbers 0 through 12 across the top
}
cout<<endl;
for (x=0; x<NumRows; x++)
{
for (y=0; y<NumColumns; y++) //Nested loops are common with multidimensional Arrays
{
multiplication[x][y]= x*y;
cout<<multiplication[x][y]<<space;
}
if (x<12)
{
cout<<endl<<x+1<<space; //Puts 0 through 12 on the left hand column
} }


Nested loops are the best way to use multidimensional arrays because the outer loop won't loop again until the inner loop has finished looping, then the outer loop increments and the inner loop goes again. If you ran the following program you'll notice that it isn't aligned correctly, the reason for this is because I am just showing you how to use it and not how to make it look good. Also I didn't put anything in red in the last example because if you understand arrays then you should understand multidimensional arrays and they aren't as complicated as vectors.

Next Section: Prewritten Functions
Twitter








Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2014 Sean Noble, All rights reserved
Read our Disclosure Policy