ONE DIMENSIONAL ARRAYS


1.1.One dimensional arrays

The simplest form of arrays is the one dimensional array whose members are retrieved with a single variable index. Members of one dimensional array are arranged in a linear sequence and the index of an individual member corresponds to the distance of initial member.
For example if we want to declare a one dimensional array with ten decimal numbers of type float, we will write:
float x[10];

With this declaration compiler will provide continuous space in memory for ten float data type. With this declaration the members of array are not initialized, so they have random values, depending on previously contained content in pieces of memory that is allocated. The graphical representation of random array declaration is given below.

ARRAY DECLARATION

Members of array can be initialized during the declaration of array. For example:
float x[10] = {10, 20, 30, 40, 53.5, 60, 85.6, 102.4, 43.5, 35.4}
With this command we have declared one dimensional array x and assign initial values to the variables of an array. Values of array variables are specified within curly braces and they are separated by commas. If we declare array in this way:
float x[] = {10, 20, 30, 40, 53.5, 60, 85.6, 102.4, 43.5, 35.4},
the compiler will conclude that the array has ten variables and will reserve corresponding memory space.
ONE DIMENSIONAL ARRAY WITH INITIALIZED MEMBERS

If you indicate the array length, you should be careful not to exceed given length. Otherwise the compiler will report an error. Let’s look at the example.
In Microsoft Visual C++ start a new Win32 project and enter following code.
// ONE DIMENSIONAL ARRAYS.cpp : Defines the entry point for the console application.
//

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

int _tmain(int argc, _TCHAR* argv[])
{
            int sample[100];
            int t;
            for (t=0; t<100; ++t) sample[t]=t;
            for (t=0; t<100; ++t) cout << sample[t] << "," << endl;
            return 0;
}

This program will print numbers of and array form 0 to 99. The numbers are initialized without our declaration.  After debugging the program if you don’t have any syntax errors the program should look like this. 
WIN32 PROGRAM

Share on Google Plus