Array Operations
To increment the ith element the following statements can be used.
a[i]++;
a[i]+=1;
a[i]=a[i]+1;
To add value n to the ith element the following statements may be used.
a[i]+=n;
a[i]=a[i]+n;
To copy the contents of the ith element to kth element.
a[k]=a[i];
To copy content from an array to another.
int a[10],b[10];
for(i=0;i<10;i++)
b[i]=a[i];
Example Program 1:
C program to find sum of all elements in an array.
#include<stdio.h>
main()
{
int a[10],i,n,s=0;
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter n elements into array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
s=s+a[i];
}
printf("The sum of elements is %d",s); }
Output:

Example Program 2:
C program to replace even numbers in array by 0.
#include <stdio.h>
main()
{
int a[10],i,n;
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter n elements into array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2==0)
a[i]=0;
}
printf("The array elements are: ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
Output:
Example Program 3:
C program to print the array elements in reverse order.
#include<stdio.h>
main()
{
int a[10],i,n;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter n elements into array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the elements in reverse order\t");
for(i=n-1;i>=0;i--)
{
printf("%d\t",a[i]);
}
}
Output:
Example Program 4:
C program to print the numbers greater than average.
#include<stdio.h>
main()
{
int a[10],n,i,s=0;
float avg;
printf("enter the number of elements: ");
scanf("%d",&n);
printf("enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
s=s+a[i];
}
avg=(float)s/n;
printf("The numbers greater than average:");
for(i=0;i<n;i++)
{
if(a[i]>avg)
{
printf("%d ",a[i]);
}
}
}
Output:


