An Introduction to Using C++ Arrays

How to Create and Manipulate Arrays in C++

© Mark Alexander Bain

Oct 17, 2008
Using Arrays in C++, Mark Alexander Bain
Arrays are often considered to be difficult to use in C++, but this article shows that this is not the case- it even shows how to calculate the number of array elements.

Many programmers who are new to C++ find arrays a difficult subject areas; and often that is because they are under the misconception that:

  • C++ arrays are difficult to create
  • C++ arrays are difficult to manipulate
  • it is not possible to calculate how long a C++ arrays is

This article will show, with some simple examples, that none of those are true.

Using Simple Arrays in a C++ Program

Arrays are easy to create and, in fact, there are two ways of creating a C++ array:

  • create an array of a set length and add items to it
  • create an array by assigning a set of items to it

This simple program shows both of those methods in action:

#include <iostream>
#include <string>
using namespace std;
#define LIST_LENGTH 5
int age [] = {16, 2, 77, 40, 12};
string name[LIST_LENGTH];
int i;
int total_age = 0, average_age = 0;
int main ()
{
name [0] = "Jane";
name [1] = "Fred";
name [2] = "Mary";
name [3] = "Henry";
name [4] = "John";
for ( i=0 ; i<LIST_LENGTH ; i++ )
{
cout << name[i] << ": " << age[i] << "\r\n";
total_age += age[i];
}
average_age = total_age/LIST_LENGTH;
cout << "Average Age: " << average_age << "\r\n";
return 0;
}

If this program is compiled and run then it will give the following result:

$ g++ -o array_01 array_01.cpp
$ ./array_01
Jane: 16
Fred: 2
Mary: 77
Henry: 40
John: 12
Average Age: 29

With simple arrays dealt with it's time to look at the much more difficult multidimensional arrays.

Using Multidimensional Arrays in a C++ Program

In actual fact multidimensional Arrays actually aren't any more difficult to work with than simple arrays, as another simple example shows:

#include <iostream>
#include <string>
using namespace std;
#define LIST_LENGTH 3
#define FIELDS 2
int person [LIST_LENGTH][FIELDS] = {{16, 20000},{20,63000},{60,90400}};
string name[] = {"Jane", "Fred", "Mary"};
int i;
int total_age = 0, total_salary = 0;
int average_age = 0, average_salary = 0;
int main ()
{
for ( i=0 ; i<LIST_LENGTH ; i++ )
{
total_age += person[i][0];
total_salary += person[i][1];
}
average_age = total_age / LIST_LENGTH;
average_salary = total_salary / LIST_LENGTH;
cout << "Average Age: " << average_age << "\r\n";
cout << "Average Salary: " << average_salary << "\r\n";
return 0;
}

And again, the result can be seen by compiling and running this new program:

$ g++ -o array_02 array_02.cpp
$ ./array_02
Average Age: 32
Average Salary: 57800

Calculating the Size of an Array in a C++ Program

In both of the examples so far the program has to be told exactly how long the array is, and (as everyone knows) if any attempt is made to read or write to the array outside of its assigned range will cause the program to crash. So here's the problem - how to test how many items there are in an array if there's no way to find out where the end of the array is. The answer is surprisingly simple - the sizeof method.

The sizeof method calculate the size (in bytes) of a variable, therefore in this case it's simply a matter of dividing the size of the array by the size of the data type used in the array:

#include <iostream>
#include <string>
using namespace std;
string name[] = {"Jane", "Fred", "Mary","Bill","Barney","William","Sue"};
int i, array_length;
int main ()
{
array_length = sizeof(name)/sizeof(string);
cout << "There are " << array_length << " people....\r\n";
for ( i=0 ; i<array_length ; i++ )
{
cout << name[i] << "\r\n";
}
return 0;
}

And, like the other examples, the program can be compiled and run to see the end result:

$ g++ -o array_03 array_03.cpp
$ ./array_03
There are 7 people....
Jane
Fred
Mary
Bill
Barney
William
Sue

Conclusion

The examples in this article are very simple, but do show just how easy it is to work with arrays in C++.


The copyright of the article An Introduction to Using C++ Arrays in C Programming is owned by Mark Alexander Bain. Permission to republish An Introduction to Using C++ Arrays in print or online must be granted by the author in writing.


Using Arrays in C++, Mark Alexander Bain
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Oct 23, 2008 7:57 AM
Guest :
Really Nice
Feb 20, 2009 10:33 PM
Guest :
your the best hehe
Apr 26, 2009 4:49 PM
Guest :
Well I am from Belize attending college and this website really did hepl me alot
1
3 Comments