Array in C programming

 

 

Array

Introduction to Array.

An array is a collection of variables of the same type that represented by a common name, i.e. array is the collection of homogenous data type (contains all the similar elements). If it contains int data type, all the data of the array must be of int, if float, then all data must be of float. A specific element of an array is accessed by an index. Each array element is accessed by specifying an array name followed by subscript/index [enclosed in square bracket]. So, the array is also called as a subscripted/indexed variable.

Subscript/index is used to denote the size of the array. For example, int a [6], where int is the data type of the array, a is the name of array and 6 is the size of 6 elements of the array and each subscript/index must be a non-negative number.

Array Declaration

The C program, all the variables must be declared before its use. So, an array must be declared so that compiler will understand the type of the variable and array size in the first line after the main ().

Syntax

1.data_type array_name [size]; 

2.data_type array_name [size] ={list of elements};

3. data_type array_name [] = {list of elements};

Merits of array

  • It is used to represent multiple data items of the same type by using the only single name.
  • We can easily access each element of the array. 
  • Not a necessity to declare too many variables.
  • Array elements are stored in a continuous memory location.
  •  It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs.
  •  2D arrays are used to represent matrices.

Demerits of array

  • We cannot change the size of the array at the run time.
  • It can store only similar data type, i.e. either all data type is int or float or char.
  • Since the array is of fixed size, if we allocate more memory than the requirement, then the memory space will be wasted. And if we allocate less memory than a requirement, then it will a create problem.
  • C environment doesn't have a checking mechanism for array sizes. 
  • An array uses a reference mechanism to work with memory which can cause unstable behavior of the operating system (unless special methods were created to prevent it) and often even "blues screen" and soon.

/*A C program to calculate total and average salary of 6 persons without using array*/

#include<stdio.h> 

#include<conio.h> 

void main()

{

float salaryl, salary2, salary3, salary4, salary5, salary6, sum, avg;

clrscr();

printf("Enter salaryl=");

scanf("%f",&salaryl); 

printf("Enter salary2 2=");

scanf("%f", &salary2); 

printf("Enter salary3 3=" )

scanf (" %f ",&salary3);

printf("Enter salary4=");

scanf("%f",&salary4);

 printf("Enter salary5=" ;

scanf("%f", &salary5);

printf("Enter salary6 6=" ) ;

scanf("%f", &salary6); 

sum=salaryl+salary2+salary3+salary4+salary5+salary6;

avg=sum/6

printf("The sum of salary' s=\%f and its ;average=%.2 f",sum,avg) ;

 getch();

}

Types of Array

Declaring the name and type of an array and setting the number of elements in the array is known as dimensioning the array. The dimension of an array can be from 1-Dimension to nth Dimension(multi dimension). As the number of subscript increases the dimension of the array also increases

proportionately.

For example,

Single dimension

data_type array_name [constant];

i.e.int values [15];

Two dimension

data_type array_name [constant] [constant];

i.e int values [15] [25];

multi-dimension array or n dimension

data_type array_name [constant] [constant] [constant]..... [constant];

i.e. int values[15][25][10].....[50];




Author Spotlight

Santosh Chapagain
Gmail: chapagainsantoshcs@gmail.com
Phone no: +977-9863512955

Post a Comment

1 Comments