Array is a collection of homogeneous data items described by a single name.

or array is a collection of similar type of data items. Each individual data item is termed as subscripted variable and is represented by array followed by subscript enclosed in square brackets.

 

Array can be one-dimensional or multi-dimensional depending upon whether number of subscripts attached with subscripted variable is one or more.

 

Declaration of array:-

As the variables, we can also declare the array. The array should be declared with data-type, array name and number of subscript in the bracket. By declaring an array, the number of memory locations (size of array) is allocated in memory.

 

The syntax for declaration of array is

 

Storage type (data type) array name (expression);

 

Here storage class may be auto, static or extern, which tells the scope of the array variable and expression, may be positive constant or constant expression, which gives the size of the array. If the storage class (type) is not given then compiler assumes it as an auto class (type).

 

The array can be declared as:-

 

int age[10];

floatsal[10];

char grade[10];

 

Here first one is the integer type array. Every element of array can hold an integer value. Second one is the float type array, can hold float value and third one is the character type array, can hold one character.

 

In memory, it occupies continuous memory location and can be represented as:-

 

Subscript or index

 

 

0 4 A[0]
1 7 A[1]
2 10 A[2]
3 13 A[3]

 

Declaration of array:-

 

Just like other variable in C, array need to be declared before its use in the program.

 

The general form for array declaration is:-

Data-type aname[size]

 

Where data type is the data type of array elements, aname is name of array and size is the maximum number of data elements which can be handles by array.

 

So in order to declare an array, user must provide following information

 

Data type of array

Name of array

Maximum value of subscript

 

The rules for the naming an array same as that of an ordinary variable.

Declare that roll is one-dimensional array of maximum 10 elements namely roll[0], roll[1], – – – – – – – – – – roll[9] and each one is of type integer.

 

Eg:-int roll[10];

 

 

Initialization of arrays

 

Assuming value to the data elements of array is termed as initialization of array. Initialization of array can be performed at the time of declaration also.

 

The general form for array initialization at the declaration time is:-

static data-type aname[size]={v1, v2, v3, – – – – – vn}

where data type is data type of array elements, aname is the name of array.

and v1, v2, v3, – – – – – vnare value assigned to first, second, – – – – -nth data elements respectively.

Eg: static introll[5]={4,5,6,7,8,9}

staticfloat marks[5]={2.3, 3.4, 4.5, 5.6, 6.7}

 

Entering data in array

 

In putting of value to an individual data element is same as it to a variable. But array is a collection of number of data elements, so to input value to all data elements in a quick and easy way, we make use loop statement.

 

Eg:- to input values to an array A of type integer and size 10, we write

 

for (i=0;i<10;i++)

scanf (“%d”,A[i]);

Accessing Array:-

As described in inputting of array, an individual member of an array can be displayed in the same manner as any other variable. But array is a collection of data elements, so to display all elementsin a quick manner and easy manner, we make use of loop statements.

 

Eg:

#include<stdio.h>

Main( )

{

int i, a[10];

printf (“enter the array values n”);

for (i=0;i<10;i++)

scanf (“%d”,&a[i]);

for (i=0;i<10;i++)

printf(“array values is=%d”,a[i]);

}

 

 

 

Multi-Dimensional Array:-

 

There can be more than one subscript to an element of an array. Such array is termed as multi-dimensional array. A two dimensional array is identified by two subscripts, a three dimensional array by three subscripts and so on.

 

The general form of n-dimensional array declaration is

 

Data-type aname[size 1] [size 2] – – – – [size n];

Where data-type is data-type of array.

Aname is name of the array and [size i] is the size of the ith dimension.

 

 

Two dimension array:-

A two dimension array is also termed as a matrix or table. A matrix has two subscripts, one denotes row and the other denotes column.

 

Eg:-

 

1        2          3

A=               4        5          6

7        8          9

 

It consists of nine elements arranged in 3 rows and 3 columns. Each element of array A is identified by the row and column to which it belongs.

 

Initialization of 2-Dimensional array:-

 

A two dimensional array can be initialized at the time of declaration in a similar manner as described for 1-D array.

 

Eg:-

static int marks[2][3]={1,2,3,4,5,6}

 

marks[0][0]=1

marks[0][1]=2

marks[0][2]=3

marks[1][0]=4

marks[1][1]=5

marks[1][2]=6

 

Entering Values:-

As described in 1-D array, loop is required to input data values to individual data elements of array. For 2-D array, nested loop is required to enter data values.

 

In row wise matrix, outer loop control the row number and inner loop controls the column number.

 

Eg:- to read 2-D array A of type 3×2, we write:-

 

For (i=0;i<10;i++)

{

For (j=0;j<10;j++)

Scanf(“%d”,&A[i]);

}

/*wap to print maximum no */

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],t,i,max;

clrscr();

printf(“enter the no’s”);

for(i=0;i<=9;i++)

scanf(“%d”,&a[i]);

max=a[0];

for(i=0;i<=9;i++)

if(max<a[i])

{

t=max;

max=a[i];

a[i]=t;

}

printf(“the max is %d”,max);

getch();

}

 

/*wap to find largest no in array*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],n,i,large;

clrscr();

printf(“enter the number”);

scanf(“%d”,&n);

printf(“enter the values”);

for(i=1;i<n;i++)

scanf(“%d”,&a[i]);

large=a[1];

for(i=2;i<n;i++)

{

if (a[i]>large)

large=a[i];

}

printf(“largest no is %d”,large);

getch();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                       ARRAYS

/EXERCISE/

  1. What do you mean by subscripted variables?
  2. What do you mean by arrays? Why we use arrays?
  3. State general form for declaring an array.
  4. It is possible to initialize on array at time of declaration? If yes, state example for   such Initialization.
  5. Distinguish between row wise and column wise 2-D arrays.at
  6. Write a program segment to input values to a 1-D array.
  7. Write a program segment to display values of 1-D array.
  8. Write a program in c to find product of elements of an array.
  9. Write a program in c to find smallest number in a list of n number. Also find its position in the given list.
  10. Write a program to find sum of two matrices.
  11. Write a program to find sum of diagonal elements of a matrix.
  12. Write a program to find trace of a matrix.
  13. Write a program to display reverse list (i.e. numbers in reverse order) of a given list of n numbers.
  14. Write a program to find to variance of list of n numbers.

Leave a Reply

Leave a Reply

Your email address will not be published.