Array-in-C .

In this session i have discussed array in C .


Array is a collection of similar data types items in contiguous memeory location . The item is called the element . Array always start from 0 index .

CSharp array image

Types of Array :

There are two types of array in C as follows .

1-One-Dimensional array .

2-Multi-Dimensional array .


1-One-Dimensional Array :

In one dimensional array the list of items can stored using one name and one subscript .

Creating Array :

1-Array declaration by specifying size

    Type Array_Name[size];
    float salary[5] //float array
    int marks[5]; // int array
   

2-Array declaration by initializing elements

    Type Array_Name[]={List of items};
    int marks[]={10,20,30,40,50};
   

3-Array declaration by specifying size and initializing elements

    Type Array_Name[size]={list of items};
    int marks[5]={10,20,30,40,50};
   

Putting values into memory Locations .

    marks[0]=20;
    marks[1]=30;
    marks[2]=40;
    marks[3]=50;
   

Example : Write a program that print the value stored in one dimensional array .

    #includ<stdio.h> 
    int main()
    { 
          int i;
          int arr[] = { 10, 20, 30, 40, 50 };
          for(i=0;i<5;i++)
          {
                printf("%d",arr[i]);
                printf(" ");
          }
          return 0;
    }
   
   OUTPUT : 10 20 30 40 50
   


Example : Write a program that print the sum of values from one dimensional array using for loop .

    #includ<stdio.h> 
    int main()
    {
            int sum;
            int i;
            int arr[] ={10,20,30,40,50};
            sum=0;            
            for(i=0;i<5;i++)
            {
                sum+=arr[i];
            }
            printf("Sum  : %d",sum);
            return 0;
    }
   
   OUTPUT : Sum : 150
   

Example : Write a program that take the input from user in one dimensional array and print using for loop .

    #includ<stdio.h> 
    int main()
    {
            int arr[5];
            int i;
            for(i=0;i<5;i++)
            {
                printf("Enter the value : ");
                scanf("%d",&arr[i]);
            }
            printf("Entered value in array :");            
            for(i=0;i<5;i++)
            {
               printf("%d",arr[i]); 
               printf(" "); 
            }
            return 0;
    }
   
   OUTPUT : Enter the value : 10
            Enter the value : 20
            Enter the value : 30
            Enter the value : 40
            Enter the value : 50
            Entered value in array :
            10 20 30 40 50

   

Example : Write a program that search the value from one dimensional array .

    #includ<stdio.h> 
    int main()
    {
            int search;
            int i;
            int k;
            k=0;
            int arr[] = { 10, 20, 30, 40, 50 };
            printf("Enter the value for search : ");
            scanf("%d",&search);
            
            for(i=0;i<5;i++)
            {
                if(arr[i]==search)
                {
                   k=1;
                   break;
                }
            }
            if(k==1)
            {
                printf("Search value found");
            }
            else
            {

                printf("Search value not found");
            }
            return 0;
    }
   
   OUTPUT : Enter the value : 30
            Search value found

   

Example : Write a program that find the max value from one dimensional array .

    #includ<stdio.h> 
    int main()
    {
            int max;
            int i;
            int arr[] = { 10, 30, 60, 40, 50 };
            max = 0;
            for(i=0;i<5;i++)
            {
                if(max<arr[i])
                {
                    max = arr[i];
                }
            }
            printf("Max : %d",max);
            return 0;
        
    }
   
   OUTPUT : Max : 60
   

Example : Write a program that sort the one dimensional array in descending order .

    #includ<stdio.h> 
    int main()
    {
            int temp;
            int i,j;
            int arr[]  = { 10, 30, 60, 40, 50 };
            temp = 0;
            for(i=0;i<5;i++)
            {
                for (j = i+1; j <5;j++ )
                    if (arr[i] < arr[j])
                    {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
           
            }
            for(i=0;i<5;i++)
            {

                printf("%d",arr[i]);
                printf(" ");
            }
            return 0;
    }
   
   OUTPUT :  60 50 40 30 10
   

Welcome to AndroidATPoint.

back image                     next image