×
☰ Menu

One Dimensional array

A list of items can be given on a variable name using only one subscript and such a variable is called a one subscript variable or a one-dimensional array.

    • A one-dimensional array or Single-Dimensional array is used to represent and store data in a linear form.
    • An array having only one subscript variable is called a One-Dimensional array
    • It is also called a Single Dimensional Array or Linear Array.

 

Example :


If we want to represent a set of five numbers, ( 25,30,35,40,45) by an array variable number,

 

syntax: 

data-type arrayName [ arrays ];

 

int number [5];

and the computer reserve five memory location as shown below:

 

 

 

 

The values to the array elements can be assigned as follows:

number[0]=25;
number[1]=30;
number[2]=35;
number[3]=40;
number[4]=45;

This would cause the array number to store the values as shown below

 


Declaring Arrays

In C, a programmer declares an array by specifying the type of elements and the number of elements required by the array, as seen below. 

data-type arrayName [ arraySize ];

The array size must be an integer constant greater than zero and the type can be any valid C data type.

For example, to declare a 10-element array called percentage of type double, use this statement –

double percentage[10];


Here percentage is a variable array that is sufficient to hold up to 10 double numbers.

 

Initializing Arrays:

After an array is declared, its elements must be initialized. Otherwise, they will contain “garbage”.

Syntax:
type arrayName [ arraySize ]={list of values};

You can initialize an array in C either one by one or using a single statement as follows −

double percentage [5] = {2000.0, 3.0, 4.4, 8.0, 6.0};

 

The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

 


            `