Arrays :

 Arrays

  • ·            Array is set of similar data types, which can be referred by common name.
  • ·        Array is called subscripted variable because array uses the subscript i.e. []
  • ·        The array is called as indexed variable because the array elements can be accessed by using index number.

Array declaration

Syntax:

            Datatype   variable_name  [size];

                        Or

            Datatype array_name [size];

Here,

Data type – type of element that is to store in array

[]- known as subscript  

Size- is maximum possible element that can be store in array.

Example:

            int a[5];          - here, int is datatype ,a is array name/ variable name and  size is 5.

            float num[4];

            char a[5];

Array initialization:

       Datatype array_name [size] = {value separated by comma};

            The array can be initialized as follows,

            nt[5] = {11,22,33,44,55};

            Float num ={2.5,3.5,4.3,5.4};

            Char a ={‘V’,’I’,’V’,’E’,’K’};

                        Or

            Char a = {“VIVEK”};

  • ·        In all above examples, the first element will be stored at zero location, the second element at one location and so on…
  • ·        The array variable can be initialized only at the time of declaration.

Memory representation of array:

            Array is stored in memory in row order and in continuous memory location.

Example,

            int a[4] = {11,22,33,44};

a[0]

a[1]

a[2]

a[3]

11

22

33

44

1000

1002

1004

1006


Program: Write a program to initialize an array and print.

/* Program to initialize an array and print */

#include <stdio.h>

#include <conio.h>

void main()

{

   int i,a[5]={11,22,33,44,55};

   clrscr();

   printf("\n Print array element\n ");

 

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

   {

            printf("\n  %d ",a[i]);

 

   }

   getch();

}

 

Output:




Program: Write a program initialize an array and print.

/* Program to initialize an array and print */

#include <stdio.h>

#include <conio.h>

void main()

{

   int i,a[5];

   clrscr();

   printf("\n Print 5 array element\n ");

 

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

   {

            scanf("\n  %d ",&a[i]);

 

   }

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

   {

            printf("\n  %d ",a[i]);

 

   }

   getch();

}

 

Output:



Program : Program to find largest number from array and print.

 

/* Program to find largest number from array and print */

#include <stdio.h>

#include <conio.h>

void main()

{

   int i,a[5]={11,22,77,66,55},largest;

   clrscr();

 

   largest = a[0];

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

   {

     if(a[i]>largest)

     {

            largest = a[i];

     }

   }

   printf("\n Largest Number from Array = %d ",largest);

   getch();

}

Output:

 




Program: Write a Program to sort an array element in ascending order.

/*  program to sort an array element in ascending order */

#include <stdio.h>

#include <conio.h>

void main()

{

   int a[5],i,j,temp;

   clrscr();

   printf("\n\nEnter 5 array element\n ");


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

   {

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


   }

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

   {

for(j=1;j<=5;j++)

{

if(a[i]<a[j])

{

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}


   }

   printf("\nArray elemente in Accending order\n");

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

   {

printf("\n%d ",a[i]);

   }

   getch();

}

 

Output:







Post a Comment

0 Comments