1.1.Multidimensional Arrays

C++ allows arrays with more than two dimensions. Here is the general form of a multidimensional array declaration:
type name[size1][size2]...[sizeN];
For example, the following declaration creates a 4 × 10 × 3 integer array:
int multidim[4][10][3];
Arrays of more than three dimensions are not often used, due to the amount of memory required to hold them. As stated before, storage for all array elements is allocated during the entire lifetime of an array. When multidimensional arrays are used, large amounts of memory can be consumed. For example, a four-dimensional character array with dimensions 10,6,9,4 would require 10 × 6 × 9 × 4 (or 2,160) bytes. If each array dimension is increased by a factor of 10 each (that is, 100 × 60 × 90 × 40), then the memory required for the array increases to 21,600,000 bytes! As you can see, large multidimensional arrays may cause a shortage of memory for other parts of your program. Thus, a program with arrays of more than two or three dimensions may find itself quickly out of memory!

1.2. Array Initialization

C++ allows the initialization of arrays. The general form of array initialization is similar to that of other variables, as shown here:
type-specifier array_name[size] = {value-list};

The value-list is a comma-separated list of values that are type-compatible with the base type of the array. The first value will be placed in the first position of the array, the second value in the second position, and so on. Notice that a semicolon follows the }. In the following example, a 10-element integer array is initialized with the numbers 1 through 10:
int i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
This means that i[0] will have the value 1, and i[9] will have the value 10. Character arrays that will hold strings allow a shorthand initialization that takes this form:
char array_name[size] = "string";
For example, the following code fragment initializes str to the phrase "hello":
char str[6] = "hello";
This is the same as writing
char str[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };
Because strings in C++ must end with a null, you must make sure that the array you declare is long enough to include it. This is why str is 6 characters long in these examples, even though "hello" is only 5. When a string literal is used, the compiler automatically supplies the null terminator. Multidimensional arrays are initialized in the same way as one-dimensional arrays. For example, the following program initializes an array called sqrs with the numbers 1 through 10 and their squares:
int sqrs[10][2] = {
1, 1,
2, 4,
3, 9,
4, 16,
5, 25,
6, 36,
7, 49,
8, 64,
9, 81,
10, 100

When initializing a multidimensional array, you may add braces around the initializers for each dimension. This is called sub aggregate grouping. For example, here is another way to write the preceding declaration:
int sqrs[10][2] = {
{1, 1},
{2, 4},
{3, 9},
{4, 16},
{5, 25},
{6, 36},
{7, 49},
{8, 64},
{9, 81},
{10, 100}

When using subaggregate grouping, if you don’t supply enough initializers for a given group, the remaining members will be set to zero, automatically. The following program uses the sqrs array to find the square of a number entered by the user. It first looks up the number in the array and then prints the corresponding square.
// ARRAY INITIALIZATION.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
using namespace std;

            int sqrs[10][2] = {
                        {1, 1}, {2, 4},{3, 9},
                        {4, 16},{5, 25},{6, 36},
                        {7, 49},{8, 64},{9, 81},
                        {10, 100}};

int _tmain(int argc, _TCHAR* argv[])
            int i, j;
            cout << "Enter a number between 1 and 10: ";
            cin >> i;
            for(j=0; j<10; j++)
                        if(sqrs[j][0]==i) break;
            cout << "The square of "<< i << " is "<< endl;
            cout << sqrs[j][1];

            return 0;

If you have entered correct program code without syntax error the program should look like this.

Global arrays are initialized when the program begins. Local arrays are initialized each time the function that contains them is called, as shown here:
#include <iostream>
#include <cstring>
using namespace std;
void f1();
int main()
return 0;
void f1()
char s[80]="this is a test\n";
cout << s;
strcpy(s, "CHANGED\n"); // change s
cout << s;

This program displays the following output:
this is a test
this is a test
In the program, the array s is initialized each time f1( ) is called. The fact that s is changed in the function does not affect its re-initialization upon subsequent calls. This means that f1( ) prints this is a test every time it is entered.
Share on Google Plus
    Blogger Comment


Post a Comment