Table of Contents hide
1. One-Dimensional Array Examples

One-Dimensional Array Examples

Example : A One-dimensional Array Examples or program in C to create an array to store ten static values given by the programmer & display them.
#include <stdio.h>
#include <conio.h>
void main()
 {
	int x[5] = {10,20,30,40,50};
          /* x[0] = 10;
           x[1] = 20;
           x[2] = 30; 
           x[3] = 40;
           x[4] = 50; */
   	int i;
   	printf ("The five static stored elements of the array are : ");
   	for (i=0; i<=4; i++)
   	{
   	   printf (" %d ", x[i]);	
	}
      	
    getch();	
}

-------------------------------------- OR --------------------------------------
#include <stdio.h>
#include <conio.h>
void main()
 {
	int x[10] = {10,20,30,40,50};          
   	int i;
   	printf ("The total static stored elements of the array are : ");
   	for (i=0; i<=9; i++)
   	{
   	   printf (" %d ", x[i]);	
	}      	
    getch();	
}

Output:
The total static stored elements of the array are :  10  20  30  40  50  0  0  0
  0  0
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]); 
    }
    printf("\n Ten elements are : \n"); 
    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 a C program to exchange/swap only the first and last value of an array having size ten to store ten values.
#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,m,x[10];
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    } 
       m=x[0];
       x[0]=x[9];
       x[9]=m;
   
       printf("The swap values are : ");
       printf("%d\t", x[0]);
       printf("%d", x[9]);	
      
    getch();	
}
Example : Write an array program in C to read the data and display it 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 of a table and display them.
#include <stdio.h>
#include <conio.h>
void main()
 {
    int i,m,x[10];
    printf("Enter a value to store table in an array: ");
    scanf("%d", &m);
    
    for(i=0; i<10; i++)
    {
        x[i]=m*(i+1);
    } 
       
   	printf("\nThe table values are : \n");
   	for(i=0; i<10; i++)
    {
        printf(" %d ",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 values 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 an even value from only the 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 values.
#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 values.
#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 a 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[9-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/descending order and display them.
#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 Ascending Sorted order of array elements are: ");
    for(i=0; i<10; i++)
    {
        printf("%d\t", x[i]);
    }      
	getch();	
}

----------------------  OR  -----------------------

#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 Descending 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 for a particular value present in an array or not.
#include <stdio.h>
#include <conio.h>
void main()
 {
	int x[10],i,k,found=0;      
   	printf ("Enter ten values in an array : ");
   	for (i=0; i<=9; i++)
   	{
   	   scanf ("%d", &x[i]);	
	}
        printf ("Enter the value we want to search from the array : ");
	scanf ("%d", &k);
	
	for (i=0; i<=9; i++)
   	{
   	   if(x[i]==k)
   	   {
   	   	found=found+1;
	   }
	}
	
	if(found>0)	
	{
		printf("Value Found");		
	}
	else
	{
		printf("Value Not Found");	
	}
    getch();	
}
-----------------------  OR  ------------------------
#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 it.
#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 values into one and display the merged array.
#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 split a large one-dimensional array into two one-dimensional arrays and display the split array.
#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[10],y[5],z[5];   
    int i, j=0;
   
    printf("Enter ten values in first array: ");
    for(i=0;i<10;i++)
    {
        scanf("%d", &x[i]);
    }      
   
    //The first split array creation
    for(i=0;i<5;i++)
    {
        y[i]=x[i];
    } 
    //The second split array creation 
    for(i=5;i<10;i++) 
    {
        z[i-5]=x[i];            
    } 
    
    printf("The first split array elements are : ");   
    for(i=0;i<5;i++)
    {
        printf("%d\t", y[i]);
    }    

    printf("\nThe second split array elements are : ");
    for(i=0; i<5; i++)
    {
        printf("%d\t", z[i]);
    }      
    getch();	
}
Example : Write an array program in 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

--------------------  OR  --------------------

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

void main()

 {
    int x[10],i,temp;    
   
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }
		    
    printf("\n The stored array elements are: ");
    for(i=0; i<10; i++)
    {
        printf("%d\t", x[i]);
    }
    
    temp=x[0];
    for(i=0; i<10; i++)
    {
      x[i]=x[i+1];              
    }
    x[9]=temp; 
		   
    printf("\n The adjacent replaced array elements are: ");
    for(i=0; i<10; i++)
    {
        printf("%d\t", x[i]);
    }      
getch();	
}

Output:
Enter ten values for array: 10
20
30
40
50
60
70
80
90
100

The stored array elements are: 10      20      30      40      50      60      70      80      90      100
The adjacent replaced array elements are: 20   30      40      50      60      70      80      90      100     10  
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
Example : A C program to Reverse the Array values and display them.
#include <stdio.h>
#include <conio.h>

void main()

 {
    int x[10],y[10],i,temp;    
   
    printf("Enter ten values for array: ");
    for(i=0; i<10; i++)
    {
        scanf("%d", &x[i]);
    }
				    
    printf("\nThe Original array elements are: \n");
    for(i=0; i<10; i++)
    {
        printf("%d\t", x[i]);
    }    
    for(i=0; i<10; i++)
    {
        y[i]=x[9-i];
    }
    
    for(i=0; i<10; i++)
    {
        x[i]=y[i];
    }    
		   
    printf("\nThe Reverse array elements are: \n");
    for(i=0; i<10; i++)
    {
        printf("%d\t", x[i]);
    }      
getch();	
}

Output:
Enter ten values for array: 10
20
30
40
50
60
70
80
90
100

The Original array elements are:
10      20      30      40      50      60      70      80      90      100
The Reverse array elements are:
100     90      80      70      60      50      40      30      20      10

------------------  OR  ------------------

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

void main()
 {
    int n,i,temp;
    printf("Enter the value for array size: ");    
    {
        scanf("%d", &n);
    }
    
    int x[n];
		
    printf("Now, Enter values for array: ");
    for(i=0; i<n; i++)
    {
        scanf("%d", &x[i]);
    }
				    
    printf("\nThe Original array elements are: \n");
    for(i=0; i<n; i++)
    {
        printf("%d\t", x[i]);
    }
		    
    for(i=0; i<n/2; i++)
    {
        temp=x[i];
	x[i]=x[n-1-i];
	x[n-1-i]=temp;
    }        
		   
    printf("\nThe Reverse array elements are: \n");
    for(i=0; i<n; i++)
    {
        printf("%d\t", x[i]);
    }      
getch();	
}

Output:
Enter the value for array size: 11
Now, Enter values for array: 10
20
30
40
50
60
70
80
90
100
110

The Original array elements are:
10      20      30      40      50      60      70      80      90      100     110
The Reverse array elements are:
110     100     90      80      70      60      50      40      30      20      10
Example : Write a C program to find the size of an array.
#include <stdio.h>
#include <conio.h>
void main()
 {
    int x[7]= { 10, 20, 30, 40, 50,60,70 }; 
    float y[3]= { 10.25, 20.54, 30.258};   
    int sizei,sizef;
	
    sizei = sizeof(x)/sizeof(int); 
    sizef = sizeof(y)/sizeof(float);     
    printf("The Size of Array are %d and %d", sizei,sizef);
	    
    getch();	
}

Loading

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.