Monday, August 26, 2019

1-D Array in C


Arrays:
Ø  An array is defined as the collection of similar type of data items stored at contiguous memory locations.
Ø  An array is a group (or collection) of same data types.
Ø  An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.
Ø  An array is also known as a subscripted variable. 
Ø  Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.
Ø  Each item in the array is called an element.
Ø  Every element of an array is identified by a number and is known as index / subscript of an array element.
Ø  Array element index always start with zero (0).

C language support two types of array
1)     One Dimensional Array
2)     Multidimensional Array

One Dimensional
The array which have only one dimension i.e. elements in row or column fashion is non as one dimensional array.

Declaration of array:
The array variable can be declared as
dataType arrayName[arraySize];
Example
int marks[5];

in above example int is a data type,
marks is a name of array, number in square bracket indicates the total number of elements to be stored in an array. i.e. size of an array.


Initialization of array elements:

              Array elements can be initialized in two different ways.
1)     At the time of initialization

int marks[5] = {44,55,66,44,33};

2)     After declaration of array variable, using index of array

marks[0] = 44;
marks[1] = 55;
marks[2] = 66;
marks[3] = 44;
marks[4] = 33;
To access array elements one can use any looping construct as shown in example below.

Example 1: initialize array elements and display using for loop.
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        int marks[5] = {44,55,66,33,55};
        clrscr();
        for(i=0;i<5;i++)
        {
           printf("\n %d",marks[i]);
        }
        getch();
}

Example 2: Read array elements and display them.
#include<stdio.h>
#include<conio.h>
main()
{
        int i;
        int marks[5];
        clrscr();
        printf(“Enter 5 elements of an array”);
        for(i=0;i<5;i++)
        {
                 scanf(“%d”,&marks[i]);
        }
        for(i=0;i<5;i++)
        {
           printf("\n marks[%d] = %d",i, marks[i]);
        }
        getch();
}

 Example 3: Read array elements and display total of them.
#include<stdio.h>
#include<conio.h>
main()
{
        int i, tot=0;
        int marks[5];
        clrscr();
        printf(“Enter 5 elements of an array”);
        for(i=0;i<5;i++)
        {
                 scanf(“%d”,&marks[i]);
        }

        for(i=0;i<5;i++)
        {
           tot = tot + marks[i]);
        }

        printf("\n Sum of all elements of an array is %d”,tot);
        getch();
}

No comments:

Post a Comment