Friday, April 17, 2020

Array in C language


What is AN ARRAY??


array in C programming
Array in c Language


  1. .     Array is the set of identical data types or collections of comparable entity stored in the contiguous memory location.
  2. .      Array of character is also string.
  3. .      Each data item of an array is called a component. And every element is unique and located in a separate memory location.
  4.      Each of elements of an array share a variable but each element having     different index no known as subscript.


What are the Categories of Array in c language??


An array may be a single-dimensional or it can also be multi-dimensional and number of subscripts determines its dimension. And number of subscript is always starts with zero.

  1. .     One dimensional array is called a "vector"
  2. .     Two dimensional arrays are understood as "matrix"


Initialization of an ARRAY in c language:


Local array has garbage value in it so first of all we have to remove that first. If it is global or static array then it will be automatically initialize with zero.

Array subscript always start from zero which is understood as the lower bound and upper value is known as the upper bound and therefore the last subscript value is one but the length of the array.

 Subscript may be an expression i.e. integer value. It can be off any type of integer whether it is, integer constant, integer variable, integer expression or return value from functional call that yield integer value.


An explicitly it can be initializing that:



Data type array[size of array]=   {no 1, no 2, no 3, no 4.........}



For example:


array[5] = {90, 100,120,87,76}

Declaration/creation of an ARRAY :


The declaration of an array tells the compiler,  about the information of data type, name of the array, size of the array, and for every element it occupies memory space. Like for int data type, it occupies 2 bytes for every element and for float it occupies 4 byte for every element, etc.

Syntax for the declaration of Array is:



Data type array name [size];

int arr[10];

int mark[10];


int a[5]={6,40,70,20,50}



One Dimensional ARRAY (1D ARRAY):


To Declare an 1D array, you just have to specify the type of the data form of the array elements, the variable name, and therefore the array size.

The index of an array starts from zero and end with (array size – 1).

Access of ARRAY:

ARRAY can be accessed by its indication number. for example:


int arr[4]={2,76,89,43,42}


if we'd like to access "42" we've to produce its indicator which is "4" during this case.


Example no 1:

The following  program is  used to print the elements in a 1-D array using for loop to take the input and displaying output.



Source code:

 program uses for loop to take input and print elements of a 1-D array.

output:

output for a program  program uses for loop to take input and print elements of a 1-D array.


Example no 2:

The following program is used to print the sum of elements of an array.


#include<stdio.h>

 void main()

 {

 int i ;

 int arr [4];

int sum=o;

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

{

printf (“enter element \n”, i+1);

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

 }

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

 {

sum = sum + a[i];

 }

printf (“the sum of  is %d”, sum);


 }


Output:



Enter a value for arr[0] =5

Enter a value for arr[1] =32

 Enter a value for arr[2] =12

 Enter a value for arr[3] =3

The sum is 50


Two/Multi-Dimensional ARRAY (2D ARRAY):


Two-dimensional array are called Matrix in c language. it's a multidimensional array. The array declaration in both the array i.e

  1. In single dimensional array single subscript is used
  2. In two-dimensional array, two subscripts are used 

Declaration of 2D array:


2d array is declared in the following way:


Its syntax is:


 Data-type array name[row][column];

Or we are able to say 2-d array may be a collection of 1-D array placed one below the opposite.


Total no. of elements in a 2D array is calculated as row*column

Simple Two dimensional (2D) Array Example:


 int a[2][3];

Total no of elements=row*column is 2*3 =6

 It means the matrix consist of two rows and three columns.


Positions of 2-D array elements in an array are as below :


00     01     02
10      11     12


For example:-

  6       2        8
 48     66     15



Initialization of 2D array:

2D Array may be initialized in two ways which are given below:

First Way:

In the following code we are creating a 2D array having 2 rows and 4 columns.

int disp [2][4] = {
    {10, 11, 12, 13},
    {14, 15, 16, 17}
};

In the above code 10,11,13,12 is for the primary row and 14,15,16,17   is for the second row of the array bonus.

Second Way:


int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17}


Why we must always use ARRAY???

  1. Array variables can store over one value at a time where other variables can store one value at a time.
  2. if we would like to store the marks of all students it'll easy to store in array otherwise, we've to store marks in numerous different locations, which isn't easy to memorize.
  3.  It's capable of storing many elements at a time.
  4.  It allows random accessing of elements i.e. any element of the array can be randomly accessed using indexes.

No comments:

Post a Comment

If else in c

IF Else in C: If-else in c: If else is the most used condition in our life. Whenever we need to make any decision we use...