• Now Online : 19
  • admin@codemyne.net

Objectives:

  • Introduction to arrays
  • A brief discussion on pointers
  • Functions

Introduction:

In everyday life similar objects are commonly grouped into units. It would always benefit us to buy tablets by the whole strip or bottle instead of buying one at a time. Similarly, in computer languages also we need to group objects of the same type. In C++, the mechanism that enables this is the array.

Introduction to Arrays:

An array is a collection of elements of the same type that can be referenced either individually or together as a single module by a common name. Each element of an array can referred to by the array name and a subscript or index.

Array Fundamentals:

A simple example program will serve to introduce arrays and illustrate their declaration and usage. The following example accepts five numbers, stores them in an array and redisplays them.

/* this program demonstrate the declaration and usage of arrays*/
/*this program taken five no's as input, stores them in array and 
 display them*/
#include<iostream.h>
void  main()
{  
    int arr1[5];
    for(int x=0;x<5;x++)
    {
        cout<<"\n Enter a number: ";
        cin>>arr1[x];
    }
    for(x=0;x<5;x++)
    {
        cout<<"\n You entered " << arr1 [x];
    }
}

The out Put of the program
Enter a number: 30
Enter a number: 8
Enter a number: 43
Enter a number: 5
Enter a number: 62
You Entered: 30
You Entered: 8
You Entered: 43
You Entered: 5
You Entered: 62

The first for loop gets the numbers from the user and places them in the array arr1. The second for loop reads them from the array and displays them.

Defining arrays:

Like all other variables in C++, arrays must be defined before they can be used. And like other definitions, an array definition specifies a variable type and a name. But it includes another feature; a size specifies how many data items the array will contain. It immediately follows the name and is surrounded by square brackets. The array arr1 was defined as fallows

       int arr1[5];

Where int is the data type of the array, arr1 is the name of the array and 5 is the size of the array.

The items in array are called the elements of array. As mentioned earlier, all elements in an array are of the same type, only the values vary. The pictorial representation of array arr1:

30         
arr1[0]
8         
arr1[1]
43         
arr1[2]
5         
arr1[3]
62         
arr1[4]

Notice that the first array element has the subscript zero. Thus, science there are five elements, the last one is the number 4. To access the second element, the subscript 1 has to be used the statement

Cout<< arr1[1];
//Prints the value 8 on the screen.

Initializing arrays:

Values can be given to each array element even when the array is first defined. Here’s a program that illustrate how array elements can be assigned values at the beginning.

Example:

/* this program shows days from start of the year to date specified */
#include<iostream.h>
void main()
{  
    int month, no_of_days;
    int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    cout<<" \n enter the month (1 to 12):";
    cin>>month ;
    if (month >12)
    {
        cout<<"\n invalid month";
        return 
    }
    cout<< "\n enter the day (1 to 31):";
    cin >>no_of_days ;
    if (no_of_days>31)
    {
    cout<<"\n invalid date";
    return
    }
    for (int j= 0 ;j <month -1;j++)
    {
        no_of_days+= days[j];
        cout<<"\n Total days from start of the year is :";
        cout<<no_of_days ;
    }
}

Example:

/* Array example*/
 
#include<iostream.h>
int billy[] = {16,2,77,40,12071};
int n, result = 0;
int main()
{   
    for(n = 0 ;n<5; n++)
    {
        result += billy [n];
    }
    cout<< result ;
    return 0;
}

The output of the program will be:
Enter the month (1 to 12): 4
Enter the day (1 to 31): 7
Total days from start of the year is 97

Once it gets the month and no_of_days values, the program cycles through a loop. Adding values from the days array to the no_of_days variable, and arrives at the final number of days.

Two-Dimensional Arrays:

So far, we have looked at single dimensional arrays. Where a single variable specifies array elements but arrays can have more dimensions. A two dimensional array is a grid containing rows and columns where each element can be accessed by specifying the row and column coordinates. The general form of the declaration of a two dimensional array is

      datatype arrayname [no.of rows ] [no.of columns ]

A typical example of a character two-dimensional array would be one hiding the weekdays, the array definition would be

char weekdays[7][10]= 
{
    “Sunday”,
    “Monday”,
    “Tuesday”,
    “Wednesday”
    “Thursday”
    “Friday”
    “Saturday” 
};

Science there are seven strings and the longest string of them Wednesday, holds nine characters, the column subscript in the array declaration is given as 10. Remember, a string needs an additional byte to hold the string terminator, the ‘\0’.

Manipulation of two dimensional arrays:

In the above example, weekdays [0] would refer to the string “Sunday”, whereas weekdays [0] [0] would refers to the character “S” of the string “Sunday”.

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment