Mastering arrays is fundamental steps for any new programmer. An array is a variable in C programming that has the ability to store multiple values in one variable instead of having different variables for each element. It is one of the simplest data structures in which each data item can be accessed at random using its index. In this series, we’ll explore essential C array programs are crafted in a simple and easy-to-understand manner, ensuring a smooth learning experience.
Table of Contents
1. Program to calculate the sum of elements in an array.
#include <stdio.h>
int main() {
int n;
// Get the size of the array from the user
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Declare an array of size 'n'
int arr[n];
// Input array elements from the user
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; ++i) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Calculate the sum of array elements
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += arr[i];
}
// Display the array elements
printf("\nEntered array elements: ");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
// Display the sum of array elements
printf("\nSum of array elements: %d\n", sum);
return 0;
}
Output:
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5
Entered array elements: 1 2 3 4 5
Sum of array elements: 15
2. Program to find the maximum and minimum values in an array.
#include <stdio.h>
int main() {
int n;
// Get the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Check if the array size is non-negative
if (n <= 0) {
printf("Invalid array size. Exiting the program.\n");
return 1; // Exit with an error code
}
int arr[n];
// Input array elements from the user
printf("Enter the array elements:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Initialize max and min with the first element of the array
int max = arr[0];
int min = arr[0];
// Find the maximum and minimum values in the array
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
// Display the results
printf("\n Maximum value in the array: %d\n", max);
printf("Minimum value in the array: %d\n", min);
return 0; // Exit successfully
}
Output:
Enter the number of elements in the array: 5
Enter the array elements:
Element 1: 10
Element 2: 5
Element 3: 9
Element 4: 15
Element 5: 8
Maximum value in the array: 15
Minimum value in the array: 5
3. C Program to Find Second Largest Element of an Array.
#include <stdio.h>
int main() {
int size, i;
// Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &size);
// Declare an array of the given size
int arr[size];
// Input array elements from the user
printf("Enter %d elements:\n", size);
for (i = 0; i < size; i++)
{
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Find the second-largest element
int firstLargest, secondLargest,id;
firstLargest = arr[0];
id = 0;
for (i = 1; i < size; i++) {
if (firstLargest < arr[i])
{
firstLargest = arr[i];
id = i;
}
if(id!=0 )
{
secondLargest = arr[0];
}
else
{
secondLargest = arr[1];
}
}
for(i= 0; i<size ; i++){
if(firstLargest > arr[i] && secondLargest < arr[i])
secondLargest = arr[i];
}
// Output the result
printf("The second-largest element in the array is: %d\n", secondLargest);
return 0;
}
Output:
Enter the size of the array: 4
Enter 4 elements:
Enter element 1: 15
Enter element 2: 8
Enter element 3: 25
Enter element 4: 32
The second-largest element in the array is: 25
4. Write Program to reverse the elements of an array
#include <stdio.h>
int main() {
// Declare variables
int size, i;
// Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &size);
// Declare an array of the given size
int arr[size];
// Get array elements from the user
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Print the original array
printf("\nOriginal Array: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
// Reverse the array
int temp;
for (i = 0; i < size / 2; i++) {
temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
// Print the reversed array
printf("\nReversed Array: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
Element 1: 1
Element 2: 3
Element 3: 5
Element 4: 7
Element 5: 9
Original Array: 1 3 5 7 9
Reversed Array: 9 7 5 3 1
5. Write a Program to counts the number of even and odd elements in an array.
#include <stdio.h>
int main() {
int n;
// Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &n);
// Declare an array of size n
int arr[n];
// Get the elements of the array from the user
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Initialize counters for even and odd elements
int evenCount = 0, oddCount = 0;
// Iterate through the array to count even and odd elements
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
// Display the count of even and odd elements
printf("Number of even elements: %d\n", evenCount);
printf("Number of odd elements: %d\n", oddCount);
return 0;
}
Output:
Enter the size of the array: 5
Enter 5 elements:
4 3 12 5 8
Number of even elements: 3
Number of odd elements: 2
6. Write C program that calculates the frequency of each element in an array.
#include <stdio.h>
int main() {
int size, i, j;
// Prompting user for the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Declaring an array of the given size
int arr[size];
// Prompting user to enter array elements
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Counting the frequency of each element
int freq[size];
for (i = 0; i < size; i++) {
freq[i] = -1; // Initialize frequency array with -1
}
for (i = 0; i < size; i++) {
int count = 1;
for (j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
count++;
freq[j] = 0; // Mark the duplicate element
}
}
if (freq[i] != 0) {
freq[i] = count;
}
}
// Displaying the frequency of each element
printf("\nFrequency of each element in the array:\n");
for (i = 0; i < size; i++) {
if (freq[i] != 0) {
printf("%d occurs %d time(s)\n", arr[i], freq[i]);
}
}
return 0;
}
Output:
Enter the size of the array: 7
Enter the elements of the array:
Element 1: 5
Element 2: 3
Element 3: 2
Element 4: 3
Element 5: 2
Element 6: 5
Element 7: 5
Frequency of each element in the array:
5 occurs 3 time(s)
3 occurs 2 time(s)
2 occurs 2 time(s)
7. Write Program to Search an Element in Array
#include <stdio.h>
int main() {
int size, searchElement;
// Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &size);
// Declare an array of the specified size
int arr[size];
// Get array elements from the user
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Get the element to be searched
printf("Enter the element to search: ");
scanf("%d", &searchElement);
// Perform linear search to find the element
int found = 0;
for (int i = 0; i < size; i++) {
if (arr[i] == searchElement) {
found = 1;
break;
}
}
// Display the result
if (found) {
printf("Element %d found in the array.\n", searchElement);
} else {
printf("Element %d not found in the array.\n", searchElement);
}
return 0;
}
Output:
Enter the size of the array: 5
Enter 5 elements:
Enter element 1: 14
Enter element 2: 6
Enter element 3: 17
Enter element 4: 3
Enter element 5: 8
Enter the element to search: 3
Element 3 found in the array.
8. Write C program that copies elements from one array to another.
#include <stdio.h>
int main() {
int size;
// Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &size);
// Declare two arrays of the given size
int originalArray[size];
int copiedArray[size];
// Input array elements from the user
printf("Enter elements of the array:\n");
for (int i = 0; i < size; ++i) {
printf("Enter element %d: ", i + 1);
scanf("%d", &originalArray[i]);
}
// Copy elements from the original array to the copied array
for (int i = 0; i < size; ++i) {
copiedArray[i] = originalArray[i];
}
// Display the original array
printf("\nOriginal Array: ");
for (int i = 0; i < size; ++i) {
printf("%d ", originalArray[i]);
}
// Display the copied array
printf("\nCopied Array: ");
for (int i = 0; i < size; ++i) {
printf("%d ", copiedArray[i]);
}
return 0;
}
Output:
Enter the size of the array: 4
Enter elements of the array:
Enter element 1: 2
Enter element 2: 4
Enter element 3: 6
Enter element 4: 8
Original Array: 2 4 6 8
Copied Array: 2 4 6 8
9. Write C program to sort elements of an array in ascending order.
#include <stdio.h>
// Function to perform ascending order bubble sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap if the element found is greater than the next element
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n;
// Input the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input array elements from the user
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Call the bubbleSort function to sort the array
bubbleSort(arr, n);
// Display the sorted array
printf("\nSorted array in ascending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the number of elements in the array: 5
Enter 5 elements:
12
8
10
7
9
Sorted array in ascending order: 7 8 9 10 12
10. Write C Program to Sort the Elements of an Array in Descending Order
#include <stdio.h>
void descendingSort(int arr[], int size) {
// Selection sort algorithm
for (int i = 0; i < size - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
// Swap the found maximum element with the current element
if (maxIndex != i) {
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
}
}
int main() {
int size;
// Get the size of the array from the user
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
// Get array elements from the user
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Sort the array in descending order
descendingSort(arr, size);
// Display the sorted array
printf("Array elements in descending order: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the size of the array: 4
Enter the elements of the array:
Element 1: 25
Element 2: 12
Element 3: 8
Element 4: 16
Array elements in descending order: 25 16 12 8