First | Next | Previous | Last | Glossary | About

# Index - Introduction to C++

- Introduction
- Lesson 1 - Getting started, A first program, Program structure, Output from programs, The programming activity.
- Lesson 2 - Variables, Data types, Arithmetic operators, simple program input.
- Lesson 3 - Decisions and Iteration in C++, if, if ... else, switch, while, do ... while and for loops.
- Lesson 4 - Functions, function arguments, standard functions.
- Lesson 5 - Arrays, single-dimension arrays, character arrays, multi-dimension arrays.
- Lesson 6

## Introduction

An array is a data structure composed of many elements of the same data type. C++ bases array indices at 0 (zero) so the last element in a C++ array has an index (or subscript) which is one less than the array's maximum size.

    //ex11.cc
    #include <iostream>

    int main()
    {
     int    ar1[100];   // 100 elements, 0 to 99
     float  ar2[4] = { 1.01, 2.02, 3.03, 4.04 };
     int    i;

     cout << "size of ar2 "
          << sizeof(ar2)
          << endl;
     for (i = 0; i <= 99; ar1[i] = i, i++);
     for (i = 0; i < 4; cout << ar2[i] << endl, i++);
     return 0;
    }

This example shows us quite a bit about arrays:

- The array ar1 has 100 elements indexed from 0 to 99
- Array ar2 has 4 elements and each element is initialised to contain some value, eg ar2[0] contains 1.01.
- The index for ar2 could have been omitted, we could have written
  float ar2[] = { 1.01, 2.02, 3.03, 4.04 };.
  The size of ar2 would be determined at compile time by the number of initialisation values.
- The sizeof() function is a standard function which returns the size, in bytes, of its argument.
- The second for loop uses the comma operator:
  ar2[i] << endl, i++

We can declare arrays of any data type and the size of an array is only limited by the available memory. Remember that each element of an array is the same kind.

Return to top of page

## Character Arrays - C style strings

This topic is included to show you that character arrays can be used in C++. However when using character arrays as strings they can be quite tricky to deal with.

Later I introduce a string class which is available in C++. This provides a much nicer way to handle strings. For now just accept that we can deal with arrays of characters if we want to.

    //ex12.cc
    #include <iostream>

    int main()

    { char string1[ ] = {'F','r','e','d','\0'};
      char string2[ ] = {"Fred"};

      cout << "Size of string1 is " << sizeof(string1)
           << " bytes" << endl;
      cout << "string1 contains " << string1 << endl;
      cout << "Size of string2 is "
           << sizeof(string2) << endl;
      cout << "string2 contains " << string2 << endl;
      return 0;
    }

    The output is:

    Size of string1 is 5 bytes
    string1 contains Fred
    Size of string2 is 5
    string2 contains Fred

- You can see that neither string1 nor string2 have a size declared. The size is determined by the initialisation.
- A C style string is terminated by the null character which is written '\0'. This is essential when using C style strings.
- A string constant like "Fred" doesn't require the null character, the compiler will take care of its addition.
- The size of the C style array is always the length of the string plus 1 for the null terminating character.

I will reiterate the last point:

With this null-terminated string type the length of the variable need not be specified, the compiler will automatically determine the size according to the placement of null. The null character takes up a place in the array so whenever character arrays are being declared make sure you allow for the null and be sure that any string values you use are terminated with the null character. Programs can do frightening things to your computer system when strings are improperly terminated.

Later I will show you much better ways to deal with strings using the string class in C++.

Return to top of page

## Multi-dimensioned Arrays

We are not limited to single dimensioned arrays, we can have as many dimensions to an array as we need:

    //ex13.cc
    #include <iostream>
    #include <iomanip>

    int main()

    { int num[10][10];
      int row, column;

      for (row = 0; row < 10; row++)
        for (column = 0; column < 10; column++)
          num[row][column] = row * column;
      for (row = 0; row < 10; row++)
        for (column = 0; column < 10; column++)
          cout << setw(4) << num[row][column] << "  ";

      return 0;
    }

The declaration int num[10][10] declares a two-dimensioned array of size 10 by 10. Any reference to an element in the array then uses a pair of indices or subscripts, eg

num[5][7]
  which refers to the 6th row, 8th column of the array. Since in C arrays are zero-based the first element is [0][0]. Take care when referencing array elements, C doesn't check subscript boundaries.

       0   0   0   0   0   0   0   0   0   0
       0   1   2   3   4   5   6   7   8   9
       0   2   4   6   8  10  12  14  16  18
       0   3   6   9  12  15  18  21  24  27
       0   4   8  12  16  20  24  28  32  36
       0   5  10  15  20  25  30  35  40  45
       0   6  12  18  24  30  36  42  48  54
       0   7  14  21  28  35  42  49  56  63
       0   8  16  24  32  40  48  56  64  72
       0   9  18  27  36  45  54  63  72  81

The output from example 3 is shown here.

The neat spacing is provided by the stream manipulator setw(4). This manipulator sets the output field width to 4 characters.

Return to top of page

## Tutorial - Arrays

1. Take a copy of example 3 and do the following:
   1. Modify the program so that it calculates the total of each column and produces the output shown below:

          .......    0   0   0   0   0   0   0   0   0   0
          .......    0   1   2   3   4   5   6   7   8   9
          .......    0   2   4   6   8  10  12  14  16  18
          .......    0   3   6   9  12  15  18  21  24  27
          .......    0   4   8  12  16  20  24  28  32  36
          .......    0   5  10  15  20  25  30  35  40  45
          .......    0   6  12  18  24  30  36  42  48  54
          .......    0   7  14  21  28  35  42  49  56  63
          .......    0   8  16  24  32  40  48  56  64  72
          .......    0   9  18  27  36  45  54  63  72  81
          Totals:    0  45  90 135 180 225 270 315 360 405

      You can see a sample answer here but make sure you try to come up with your own solution.
   2. Now modify the program from the previous step by changing each activity to a function, ie you should have a function to
      1. initialise the num array,
      2. calculate totals,
      3. display the array,
      4. display the totals.
      You can see a sample answer here but make sure you try to come up with your own solution.

Return to top of page

First | Next | Previous | Last | Glossary | About

Copyright © 1999 - 2001 David Beech