One Dimensional Array Examples

Example : A c program to create an array to read & display the values in one dimensional array.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10],i;   
    printf("\n Enter ten elements : \n"); 
    for(i=0; i<10; i++) 
    { 
        printf("Enter %d element = ",i+1); 
        scanf("%d",&x[i]); 
    }   
    for(i=0; i<10; i++)
    {
       printf("%d",x[i]); 
    }    
    getch();	
}

Example : Write a program in c to create an array to read & display the first and last element of an array element.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,x[10];
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    } 
       printf("%d\t", x[0]);
       printf("%d\t", x[9]);	
      
    getch();	
}

Example : Write an array program in c to read the data and display in reverse order.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,x[10];
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    } 
    for(i=9;i>0;i--)
    {
       printf("%d\t", x[i]);
    }      
    getch();	
}

Example : Write a program in c to create an array and display the sum and product of array elements.

#include <stdio.h>
#include <conio.h>

void main()
 {
    int x[10]; 
    int sum=0,product=1,i;  
   
    printf("\nEnter ten elements : \n"); 
    for(i=0; i<10; i++) 
    { 
        printf("Enter %d element",i+1); 
        scanf("%d",&x[i]); 
    }    
    for(i=0; i<10; i++)
    {
        sum=sum+x[i];
        product=product*x[i];
    }       
    printf("\nSum of total array element is   : %d"  ,sum); 
    printf("\nProduct result of total array element is : %d\n",product); 
	    
    getch();	
}

Example : Write a program in c to create an array to store ten data and display only negative data if any, present in the array.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,x[10];   
    printf("Enter ten values for array : ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }
    printf("\nAll negative elements present in array are : ");
    for(i=0;i<10;i++)
    {
        if(x[i]<0)
        {
            printf("%d\t", x[i]);
        }
    }	    
    getch();	
}

Example : Write a program in c to create an array to store ten values and display even & odd values separately.

#include<stdio.h>
#include<conio.h>
void main()
{
	int x[10],i;
	printf("Enter ten no:-");
		for(i=0;i<=9;i++)
		{
		   scanf("%d",&x[i]);
		}
		printf("\nEven Numbers are = ");
		for(i=0;i<=9;i++)
		{
		    if(x[i]%2==0)
			{
			   printf("%d\t",x[i]);	
			}			
		}
		
		printf("\n\nOdd Numbers are = ");
		for(i=0;i<=9;i++)
		{
			if(x[i]%2!=0)
			{				
			   printf("%d\t",x[i]);	
			}			
		}
	getch();	
}

Example : Write a program in c to create an array to store the values and display only even value from only even index location/address of the array.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,x[10];
    printf("Enter ten values for array: ");
    for(i=0;i<10;i++)
    {
        scanf("%d", &x[i]);       
    } 
    for(i=0;i<10;i++)
    {	
        if(i%2==0 && x[i]%2==0)
       	   printf("%d\t", x[i]);
    }      
    getch();	
}

Example : Write a program in c to create an array and display only even value from only odd index location of the array.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,x[10];
    printf("Enter ten values for array: ");
    for(i=0;i<10;i++)
    {
        scanf("%d", &x[i]);       
    } 
    for(i=0;i<10;i++)
    {	
        if(i%2!=0 && x[i]%2==0)
       	   printf("%d\t", x[i]);
    }      
    getch();	
}

Example : Write a program in c to create an array to store and display the largest and smallest value.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10];
    int i, largest, smallest;
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }   
    largest = x[0];
    smallest = x[0];    
    for(i=1; i<10; i++)
    {
        if(x[i]>largest)
        {
            largest=x[i];
        }
        
        if(x[i]<smallest)
        {
            smallest=x[i];
        }
    }   
    printf("Largest element is = %d\n", largest);
    printf("Smallest element is = %d", smallest);
	    
    getch();	
}

Example : Write a program in c to create an array to store and display the largest and second largest value.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10];
    int i, largest, slargest;
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }
   
    largest = x[0];
    slargest = x[0];    
    for(i=1; i<10; i++)
    {
        if(x[i]>largest)
        {
            slargest=largest;
			largest=x[i];            
        }       
    }   
    printf("Largest element is = %d\n", largest);
    printf("slargest element is = %d", slargest);    
      
    getch();	
}

Example : Write a program in c to create an array to store and then count the total number of odd and even present in it.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10];
    int i, even=0, odd=0;
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    } 
    for(i=0;i<10;i++)
    {
        if(x[i]%2==0)
        {
            even++;
        }
        else
        {
            odd++;
        }
    }
   
    printf("Total even elements in the array are = %d\n", even);
    printf("Total odd elements in the array are = %d", odd);      
    getch();	
}

Example : Write a program in c to create an array to store and then copy all the values in similar order in another array and display them.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,x[10],y[10];
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    } 
    for(i=0;i<10;i++)
    {
        y[i]=x[i];
    }   
    for(i=0;i<10;i++)
    {
        printf("%d\t", y[i]);
    }      
    getch();	
}

Example : Write a program in c to create an array to store and then copy all the values in reverse order in another array and display them.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,x[10],y[10];
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    } 
    for(i=0;i<10;i++)
    {
        y[i]=x[i];
    }   
    for(i=0;i<10;i++)
    {
        printf("%d\t", y[i]);
    }       
    getch();	
}

Example : Write a program in c to create an array to store ten values and arrange them in ascending order.

#include <stdio.h>
#include <conio.h>

void main()

 {
    int x[10];   
    int i, j, temp;
   
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }     

    for(i=0; i<10; i++)
    {
       for(j=i+1; j<10; j++)
        {
            if(x[i]>x[j])
            {
                temp=x[i];
                x[i]=x[j];
                x[j]=temp;
            }
        }
    }    
    printf("\n Sorted order of array elements are: ");
    for(i=0; i<10; i++)
    {
        printf("%d\t", x[i]);
    }      
	getch();	
}

Example : Write a program in c to create an array to store and then search a particular value present in an array or not.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10];   
    int i, j, temp=0;   
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }     

    printf("\n Enter a value to search in an array: ");
    scanf("%d", &j);    
    for(i=0; i<10; i++)
    {
       if(x[i]==j)
        {
            temp++;            
        }
    }

    if(temp==1)
    {
        printf("\n value %d is found at position %d", j, i + 1);
    }
     if(temp==0)
    {
        printf("\nValue %d is not found in the array", j);
    }    
    if(temp>=2)
    {
        printf("\n More than one value is found in the array");
    }
      
	getch();	
}

Example : Write an array program in c to delete an element from an array and display them.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10];   
    int i, j, temp=0;
   
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }     
    
    printf("Enter the element position to delete : ");
    scanf("%d", &j);
   
    if(j<0 || j>9)
    {
        printf("Incorrect array position to delete");
    }
    else
    {
        for(i=j-1; i<10; i++)
        {
            x[i] = x[i + 1];
        }        
    }
   
    printf("\n Array elements after deletion are : ");
    for(i=0; i<10; i++)
    {
        printf("%d\t", x[i]);
    }
      
    getch();	
}

Example : Write an array program in c to separate odd and even values from an  array into two different array  and display them.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10],y[10],z[10];   
    int i, j=0, k=0;
   
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }  

    for(i=0; i<10; i++)
    {
        y[i]=z[i]=0;  //To stop garbage value in blank array element
        
	if(x[i]%2==0)
        {
            y[j]=x[i];
            j++;
        }
    	if(x[i]%2!=0)
        {
           z[k]=x[i];
           k++;
        }        
    }

    printf("\n Elements of even array: \n");
    for(i=0; i<10; i++)
    {
        printf("%d\t", y[i]);
    }    
    printf("\nElements of odd array: \n");
 		   
    for(i=0; i<10; i++)
    {
        printf("%d\t", z[i]);
    }      
    getch();	
}

Example : Write an array program in c to merge two array into one display them.

#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[5],y[5],z[10];   
    int i, j=0;
   
    printf("Enter five values in first array: ");
    for(i=0;i<5;i++)
    {
        scanf("%d", &x[i]);
    }      
   
    printf("Enter five values in second array: ");
    for(i=0;i<5;i++)
    {
        scanf("%d", &y[i]);
    } 
	 
    for(i=0;i<5;i++)
    {
            z[j]=x[i];
            j++;
    }    
    for(i=0;i<5;i++)
    {
            z[j]=y[i];
            j++;
    }    

    printf("\nElements of merged array: \n");
    for(i=0; i<10; i++)
    {
        printf("%d\t", z[i]);
    }      
    getch();	
}

Example : Write an array program in c to swap two adjacent cell values and display them.

#include<stdio.h>
#include<conio.h>
void main()
{
    int num[10],m,i,temp;
    clrscr();     
    printf("Enter the size of array elements in even format for proper swapping : ");
    scanf("%d",&m);     
    
    if(m%2!=0)
    {
        printf("You entered odd no.of array element size, change it into even no. form.");
        return 1;
    }
    
    printf("Now enter total %d array elements:\n",m);
    for(i=0;i<m;i++)
    {
        printf("Enter %d array element :",i+1);
        scanf("%d",&num[i]);
    }
    
    for(i=0;i<m;i=i+2)
    {
        temp = num[i];
        num[i] = num[i+1];
        num[i+1]= temp;
    }
     
    printf("\nFinal Array elements after swapping adjacent elements are :\n");
    for(i=0;i<m;i++)
    {
        printf("%d ",num[i]);
    }
    getch();
}

Output : 
Enter the size of array elements in even format for proper swapping : 4
Now enter total 4 array elements:
Enter 1 array element :21
Enter 2 array element :12
Enter 3 array element :32
Enter 4 array element :23

Final Array elements after swapping adjacent elements are :
12 21 23 32

Example : Write an array program in c to forward swap each adjacent cell value and display them.

#include<stdio.h>
#include<conio.h>

void main()
{
    int num[10],m,i,temp1,temp2;    
     
    printf("Enter the size of array elements for forward swapping : ");
    scanf("%d",&m);
    
    printf("Now enter total %d array elements:\n",m);
    for(i=0;i<m;i++)
    {
        printf("\nEnter %d array element :",i+1);
        scanf("%d",&num[i]);
    }    
   		
	temp1=num[0];        
        temp2=num[1];
		  
    	for(i=0;i<m;i=i+1)
    	{		
	    	if(i==0)
		{
		  num[0]=num[m-1];	
		}
		else
		{
	           num[i]=temp1;
		   temp1=temp2;
		   temp2=num[i+1];
	        }
     	}
    printf("\nThe Array elements after forward swapping of adjacent elements are :\n");
    for(i=0;i<m;i++)
    {
        printf("%d\n",num[i]);
    }
    getch();
}

Output :
Enter the size of array elements for forward swapping : 4
Now enter total 4 array elements:
Enter 1 array element :14
Enter 2 array element :23
Enter 3 array element :65
Enter 4 array element :85

The Array elements after forward swapping of adjacent elements are :
85
14
23
65

 912 total views,  1 views today

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.