Folder101   Home Notes Quiz Assigns Info  
Forum Forum
101 Home 101
 
  C++ Level 2

Arrays, Characters and Strings

Introduction

Creating Arrays

Character Arrays - Strings

Extension Work


  Introduction

So far you have discovered how to declare variables of various types. Suppose though you wanted to declare a set of variables all at once, such as 100 ints. For that, you need arrays. Also, you may know how to create a single character, such as 'a', but how do you create a string of characters such as "abcd"?


  Creating Arrays

What is an array? An array is a collection of storage locations for holding objects of the same type. Each location in an array is called an element of the array. Moreover, the collection can be referenced by a common name.

      Array Declaration

Suppose for instance, you want a collection of ten integers. Then you would declare an array of integers as follows:-

int myArray[10];

Each of the ten elements of the array can hold an integer data type. Of course, as with normal integers, the integers in the array are not initialized. You can initialize during the declaration as so:-

int myArray[] = {0,1,2,3,4,5,6,7,8,9};

This creates an integer array, containing the set of numbers from 0 to 9. Here is a more varied set.

int anotherArray[] = {3, 100, -98, 66,9};

Now let's have an array of floating point numbers.

int floatArray[] = {3.0, 100.123, -98.6, 66.66};

Easy yes? Its the same principle for creating an array of any type of object.

      Accessing Elements of an Array

Although creating arrays is fairly straightforward, you might wonder how to access a particular object stored within. If you remember, each location within an array is called an element and these elements are numbered by index number. What that means is that the first element is called the 0 element, the second element is the 1 element, the third is the 2 element, and so on.

Note --  element 0 is the first element in any array

Lets clarify all this. Here's a diagram of an unitialized array called myArray. E.g.

int myArray[5];

0
1
2
3
4
?
?
?
?
?

The numbers in bold, 0 to 4 indicate the element numbers. The cells below each number are the stores for the data. I have typed in question marks because the value of the data in each location is unknown at present because the array is uninitialized. In fact, there could be anything in these locations because all that has happened so far, is a contiguous block of memory has been set aside for the array. This block of memory could have any old data left in it that is no longer needed.

Of course, we could have declared and initialized at the same time. E.g.

int myArray[5] = {3,6,10,31,9];

0
1
2
3
4
3
6
10
31
9

Now, element 0 contains the integer value 3, element 1 holds a 6 and so on.

I could have initialized in a different way - choosing particular elements to put values in:-

int myArray[5];

myArray[2] = 10;

myarray[4] = 9;

0
1
2
3
4
?
?
10
?
9

Accessing a particular element is also straightforward:-

int x;   

x = myArray[2];   //this sets the value of x to 10

Don't make the mistake of trying to access an element that doesn't exist. A common error is to forget that the first element is labeled 0. So what will the the last element of myArray be labeled as?

int x;   

x = myArray[5];   //this would give an error

In the case above, there are 5 elements in the array, but the last element is labeled 4 and not 5.

Don't Forget: --  element 0 is the first element in any array


  Character Arrays - Strings

You have already learnt how to create characters using the char keyword. A string however is a collection of characters. You create a string by creating a character array. You have seen strings before through using the cout statement:-

cout << "I am a string";

      Creating a String

In C++ a string is a series of characters ending with a null character \0. Here is an example:-

char hi[]={'H','e','l','l','o',` `,`W','o','r','l','d',`\0'};

The last character \0 makes this series of characters into a string. You can imagine that the null character tells C++ where the end of a string is. The null character doesn't show up when you output the string, say to the screen. Try it:-

cout << hi;

There is a quicker way to initialize an array and create the string:-

char hi[] = "Hello World";

You should note that that when you create a string this way, the compiler actually adds the null character for you. In fact, how many character do you think are in this array? Perhaps you would say 11. No! There are 12 because of the null character. You can check by using the sizeof operator which returns the number of bytes in an array. Remember, each character takes up 1 byte of memory. So lets see if we get 11 or 12 bytes for this array:-

cout<< sizeof(hi);

      Uninitialized Buffer

Consider how you would get and store input from a user. Quite often you have no idea what the input will consist of or the what its length will be. The way to handle that is to create a buffer. A buffer is a set of storage locations you can store data in, which sounds just like an array, don't you think? Lets have an example:-

 
#include <iostream.h>
void main()
{
   char name[80];
   cout <<
"Enter your name" << endl;
   cin >> name
;
}

This example creates an uninitialized character array, with 80 storage locations, (elements). When the user enters a name, the input is stored in the array. Look at the program running below:-

I entered "angela", so how many of the 80 elements of the name buffer array have been initialized now? I hope you thought 7 and not 6, remember the null character. Now change the code as follows:-

 

#include <iostream.h>
void main()
{
  char name[80];

  //lets see what's stored in the first element
  cout << "In the first element we have: " << name[0] << endl;

  //lets see what's stored in the 10th element
  cout << "In the 10th element we have: " << name[9] << endl;

  cout << "Enter your name" << endl;
  cin >> name
;

  //now lets see what's stored in the first element
  cout << "In the first element we now have: " << name[0] << endl;

  //lets see what's stored in the 10th element now
  cout << "In the 10th element we have: " << name[9] << endl;
}

Running the program will give output similar to:-

You can see that there is strange unknown data in the uninitialized array. When the array is filled with input, the first seven elements then contain the string "angela" with the null character at the end. However, any element after the seventh will still contain garbage.

Note: There are two problems to be aware of when using buffers. In the case of the program above, if the user enters more than 79 characters, cin writes past the end of the buffer. Also, if the user enters a space, cin thinks that it is the end of the string, and it stops writing to the buffer.


  Extension Work

Carry out the following exercise:-

  1. Start step4 of the practice assignment 3

That it!
Now try the quiz questions