What is an Array? Explain about One-Dimensional Arrays?


The fundamental data types can store only one value at any given time. To process large amounts of data, c supports a derived data type known as Array.

An array can be used to represent a list of numbers, or a list of names.

Def: A predefined set of values all of same data type & referred with single name is called as array.

Or

An array is a group of similar  type of data  items or related data  items which share  a common name  and have different  sub scripts , and they  are stored  in  continues  memory  locations.

An array is a sequential collection of related data items.

For example: Use an array salary to represent a set of salaries of a group of employees in an organization.

We can refer to the individual salaries by writing a no: called index (or) subscript in brackets after the array name.

Salary[10];

Represents the salary of 10th employee.

These index values starts with 0 and ends with n– 1.

Basically arrays can be classified in to 3 types:

1.Single (or) one dimensional  arrays

2.Double (or) two dimensional  arrays

3.Multidimensional arrays

One  dimensional arrays :

 A list of items can be given one variable name using only one sub script   and such a variable is called a single – subscripted variable (or) a one – dimensional array.

Single – subscripted variable x ; can be expressed  as:

x[1],x[2],x[3],........,x[n]; 

The subscript can begin with number 0. for  example if you want  to  represent  a set of  five numbers, say (35,40,80,90,99), by an  array variable number, then we  may  declare as:

int no[5] ;

and the computer reserves  five storage locations as  shown bellow:

1darray format








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

no[0]=35,
no[1]=40,……

The following are valid statements:

a=no[0] +10;

no[4]=no[10]+no[2];

no[5]=x[5]+y[10]

In one – dimensional array, an array consist of one row and several columns.

Declaration of one – dimensional array:

Like any other variable arrays must be declared before they are used. So that the compiler can allocate space for then in memory.

The general form of away declaration is:

Syntax: type variable_name[size];

The type specifies the type of elements that will be contained in the array, such as into, float or char.

The size indicates the max no; of elements  that can be stored inside the array.

Ex: int stu [10];

Declares the stu as an array to contain a maximum of 10 integer constants.

C language treats character strings  simply as arrays of character string represents the maximum number of characters that the string can hold for ex:

char  name [10];

Declares the name as a character array. Suppose we read

“WELL DONE”

Then each character of the string is treated as an element. Of the array name and is stored in memory as:

1darray


When the compiler sees a character string, it terminates it with an additional null  character.

When declaring character arrays, we must allow one extra element space for the null terminator.

Initialization of one – dimensional array:

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

An array can be initialized in 2 ways:

1. At compile time

2. At run time.

Compile  time initialization :

We can initialize the elements of arrays in the same way as the ordinary variables when they are declared.

The general  form of initialization is:

Syntax; type  array_name[size]  ={list of values};

The values  in the list  are separated by commas.

Ex:     int number[3]= {0,0,0};    

            float total[5] ={0.0,15.2,10};

Will initialize the first 3 elements to 0.0, 15.2 & 10.0 and the remaining 2 elements to zero.

The size may be omitted. In such canes the compiler allocates enough space for all  the initialized elements for ex:

int counter[ ] ={1,1,1,1};

Will declare the counter array to contain the elements with initial values 1.

Character arrays may be initialized in a similar manner.

char name [ ]={'j','o','h','n','\0'};

we can also  assign the string directly as  char name [ ]= “john”;

If the number of initializes may be Len then the declared size, then the reaming elements are initialized to zero if the array type is numeric and null the type is char.

Ex:    int    no[ ]={10,20} ;

Will initialize the first 2 elements to 10 and 20 respectively, and the remaining elements to 0.

Run time initialization:

An array can be explicitly initialized at run time. This approach  is usually applied  for initializing large Arrays.

Ex: for (i= 0;  i < 100 ;  i = i+1)
       {
            if  ( i < 50 )
              Sum[i] =0.0
            else
              Sum[i]=1.0;
        }

The first 50 elements of the array sum are initialized to zero while the remaining elements to 1.0 at run time.

We can also use a read in such as scan f to initialize an array.

Ex:      int x[3] ;
            scanf(“ %d%d%d”,  &x[0] , & x[1]  ,&x[2] );

Will initialize array elements with the values entered through the keyboard.         

Access an element in an array:

Index is used to access an individual element in away computer gives index values from Q on wards. x[i] refers to the I th element of as away x. Here p is an index (or) subscript.

Syntax: array_name [index] = value;

Ex: x[2] = 60;

The index is not only constant if also a variable or an expression.

Ex : i =3 ;
       x[i] = 20;
       j =2;
       x[i+j] = 30;
                                                                                   
Reading and Writing elements in to an array:

In c array elements are reading or writing with the help of looping statement.

printf (“enter elements into an array”);
for ( i =0; i < n; i ++)
{
scanf (“%d”,&x[i]);
}
For writing use printf Statement instead of scanf.


Related

C Language 4419076038441924394

Post a Comment

emo-but-icon

item