Introduction
In this article, I am going to write top 15 programs on arrays in c. If you are a beginner and you want to know more about arrays in c, then you can read my article Arrays in C for Complete Beginners.
Write a Program in C to find the Average Marks obtained by a class of ‘n’ Students
In this program, we have to read marks of ‘n’ students and then we will have to calculate the average.
int main()
{
int sum = 0,i,n;
float avg; ;
int marks[100];
printf("Enter total number of students\n");
scanf("%d",&n);
printf ("Enter marks for %d students\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&marks[i]);
sum=sum+marks[i];
}
avg = sum/(float)n ;
printf("Average marks = %0.2f", avg);
return 0;
}
Output
Enter total number of students
5
Enter marks for 5 students
45
67
43
78
55
Average marks = 57.60
How do you find the size of an array in C?
Here, we are using sizeof() operator. This operator returns the size of any data-type. We know, the array is also a data-type. Here, size means total bytes of memory allocated to a particular data-type.
int main()
{
int int_arrays[100];
float real_arrays[10];
char char_arrays[100];
printf("Size of the Intger Arrays = %d bytes",sizeof(int_arrays));
printf("\nSize of the Real Arrays = %d bytes",sizeof(real_arrays));
printf("\nSize of the Character Arrays = %d bytes",sizeof(char_arrays));
return 0;
}
Output
Size of the Intger Arrays = 400 bytes
Size of the Real Arrays = 40 bytes
Size of the Character Arrays = 100 bytes
As you can see the above output, we have created array of 100 integer numbers. Each integer takes 4 bytes. Similarly, float takes 4 bytes and char takes 1 byte.
I hope these programs on arrays in c are helpful to you. Now, see the other examples.
How do you find the sum of an array?
In this program, we will read an array from the user and then return the sum of elements of an array. Suppose the user has entered 5 elements like a, b, c, d and e then we need to find a+b+c+d+e. See the following program.
int main()
{
int sum = 0,i,n;
int num[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf ("Enter total %d elements\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
sum=sum+num[i];
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter the size of an array
10
Enter total 10 elements
1 2 3 4 5 6 7 8 9 10
Sum = 55
C Program to Find Sum of Odd Numbers in an Array
In this program, we will read any numbers from the user and store these number into an array. Then we will find the sum of odd numbers only from the array.
int main()
{
int sum = 0,i,n;
int num[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf ("Enter total %d elements\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
if((num[i]%2)==1) // checking whether num[i] is odd or not.
{
sum=sum+num[i];
}
}
printf("Sum = %d", sum);
return 0;
}
In the above program, after reading an element, we will check whether the number is odd or not. How can we do this? We are using modulo operator (%) which gives us remainder after division. When we divide any number by 2, we can get 0 or 1 remainder only. If the remainder is 0 that means the number is even, otherwise the number is odd.
Output
Enter the size of an array
10
Enter total 10 elements
1 2 3 4 5 6 7 8 9 10
Sum = 25
Here, we have performed 1+3+5+7+9=25.
There are many programs on arrays in c where we can use the same logic. Just like, see the next c program.
C Program to Find Sum of Even Numbers in an Array
This is the same program as given above. The only difference is here we are finding the sum of even numbers in an array. The whole program remains the same, but only if condition will be changed.
int main()
{
int sum = 0,i,n;
int num[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf ("Enter total %d elements\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
if((num[i]%2)==0) // checking whether num[i] is even or not.
{
sum=sum+num[i];
}
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter the size of an array
10
Enter total 10 elements
1 2 3 4 5 6 7 8 9 10
Sum = 30
Here, we have performed 2+4+6+8+10=30.
C Program to Find Sum of Even and Odd Numbers Separately in an Array
This c program is the combination of above two programs. Here, we will use if-else statement.
int main()
{
int sume = 0, sumo = 0, i,n;
int num[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf ("Enter total %d elements\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
if((num[i]%2)==0) // checking whether num[i] is even or not.
{
sume=sume+num[i]; // sum of even numbers
}
else
{
sumo=sumo+num[i]; // sum of odd numbers
}
}
printf("Sum of Even Numbers = %d\n", sume);
printf("Sum of Odd Numbers = %d\n", sumo);
return 0;
}
Output
Enter the size of an array
10
Enter total 10 elements
1 2 3 4 5 6 7 8 9 10
Sum of Even numbers = 30
Sum of Odd numbers = 25
Sum of Cubes of Array Elements
In this program, we have to read the elements from the user and then find the cube of each element and add them.
int main()
{
int sum = 0,i,n;
int num[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf ("Enter total %d elements\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
sum=sum+num[i]*num[i]*num[i]; // addition of cubes of elements
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter the size of an array
5
Enter total 5 elements
1 2 5 4 3
Sum = 225
In this output, we have performed 13 +23 +53 +43 +33 = 1+8+125+64+27 = 225. I have already written an article on this topic. For a detailed explanation, click on this link. C Program to Find the Sum of Cubes of Elements in an Array.
Program to Find Square of Elements of an Array
In this program, we have to read an array and store the squares of those array elements in another array. See the following program.
int main()
{
int sum = 0,i,n;
int num[100], snum[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf ("Enter total %d elements\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
snum[i]=num[i]*num[i];
}
printf("Original Array Elements : ");
for(i=0;i<n;i++)
{
printf("%d",num[i]);
}
printf("\nSquare of Array Elements : ");
for(i=0;i<n;i++)
{
printf("%d",snum[i]);
}
return 0;
}
Output
Enter the size of an array
5
Enter total 5 elements
1 2 3 4 5
Original Array Elements : 1 2 3 4 5
Square of Array Elements : 1 4 9 16 25
Here, we have declared two arrays. One is num[] which contains original array elements and the other array variable is snum[] which contains square of array elements.
How do you find the smallest number in an unsorted array?
In this program, we will read an array of size n. Then we will have to enter n elements and find the smallest element in that array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],n;
int i,small;
printf("Enter size of an array\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if(small>a[i])
{
small=a[i];
}
}
printf("Original Array Elements: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nSmallest Element = %d",small);
return 0;
}
Output
Enter size of an array
5
Enter 5 elements
5 6 3 8 4
Original Array Elements: 5 6 3 8 4
Smallest Element = 3
How do you find the largest number in an unsorted array?
In this program, we will read an array of size n. Then we will have to enter n elements and find the largest element in that array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],n;
int i,large;
printf("Enter size of an array\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=a[0];
for(i=1;i<n;i++)
{
if(large<a[i])
{
large=a[i];
}
}
printf("Original Array Elements: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nLargest Element = %d",large);
return 0;
}
Output
Enter size of an array
5
Enter 5 elements
7 4 8 3 5
Original Array Elements: 7 4 8 3 5
Largest Element = 8
How do you find the largest and smallest number in an unsorted array?
This program is the combination of the above two programs. We have to find the smallest and largest element in the same array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int small,large,i,n, a[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf("Enter %d Numbers\n", n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
small=a[0];
large=a[0];
for(i=1;i<n;i++)
{
if(large<a[i])
large=a[i];
if(small>a[i])
small=a[i];
}
printf("Original Array Elements: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nSmallest Number = %d\n",small);
printf("Largest Number = %d\n",large);
return 0;
}
Output
Enter the size of an array
10
Enter 10 Numbers
5 9 2 44 3 7 66 4 6 8
Original Array Elements: 5 9 2 44 3 7 66 4 6 8
Smallest Number = 2
Largest Number = 66
Bubble Sort in C Program
In this program, we will read n elements in an array and then sort this array using bubble sort. At every pass, list of array elements get reduce. In the pass 1, you will get largest element in the end of the list, in the second pass, you will get second largest element and so on.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100];
int i,j,temp,n;
printf("Enter the size of an array\n");
scanf("%d",&n);
printf("Enter %d Numbers\n", n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Original List: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<(n-1)-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nSorted List: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
Output
Enter the size of an array
10
Enter 10 Numbers
10 9 8 7 6 5 4 3 2 1
Original List: 10 9 8 7 6 5 4 3 2 1
Sorted List: 1 2 3 4 5 6 7 8 9 10
Selection Sorting in C
In the selection sort method, In the first pass, we have to find smallest number first. In next pass, we will go for the second smallest number in the array and so on.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100];
int i,j,temp,smallest,n;
printf("Enter the size of an array\n");
scanf("%d",&n);
printf("Enter %d Numbers\n", n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Original List: ");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
for(i=0;i<(n-1);i++)
{
smallest=i;
for(j=i+1;j<n;j++)
{
if(a[smallest]>a[j])
{
temp=a[j];
a[j]=a[smallest];
a[smallest]=temp;
}
}
}
printf("\nSorted List: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
Output
Enter the size of an array
10
Enter 10 Numbers
10 9 8 7 6 5 4 3 2 1
Original List: 10 9 8 7 6 5 4 3 2 1
Sorted List: 1 2 3 4 5 6 7 8 9 10
Linear Search Program in C
Linear search is also called sequential search. That means you have to search an element from the beginning to the end of the list. Here, I have taken static array. You can do the same program by using dynamic arrays.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[]={1,4,45,23,34,76,39,80,56,48};
int i,element;
printf("Given List: ");
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an Element to Search : ");
scanf("%d",&element);
for(i=0;i<10;i++)
{
if(a[i]==element)
{
printf("%d is present at location %d",element,i+1);
return 0;
}
}
printf("%d is not found in a given list",element);
return 0;
}
Output
Given List: 1 4 45 23 34 76 39 80 56 48
Enter an Element to Search : 34
34 is present at location 5
Binary Search in C
In this program, we are again finding an element in an array using binary search. As the name suggests, binary means two. We have to divide the array list into two until we get our element. Binary search method works only when the array is sorted.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[]={2,4,5,8,19,31,34,56,78,81,89};
int element,beg,end,mid,i;
printf("Given List: ");
for(i=0;i<=10;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to search: ");
scanf("%d",&element);
beg=0;
end=10;
mid=(int)((beg+end)/2);
while((a[mid]!=element)&&(beg<=end))
{
if(element<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(int)((beg+end)/2);
}
if(a[mid]==element)
{
printf("Location of %d is %d",element,mid+1);
}
else
{
printf("%d is not present in given list",element);
}
return 0;
}
Output
Given List: 2 4 5 8 19 31 34 56 78 81 89
Enter an element to search: 31
Location of 31 is 6
These 15 programs on arrays in c programming are very important to understand the concept of arrays in c. I hope you have understood all these programs on arrays in c. If you have any doubt about any program given above, please feel free to contact me.
Thank you.