C Programming • Arrays & Functions
C Programming / Arrays (1-D) — Exercises

Arrays (1-D) — Exercises

Practical 3 Arrays & Functions

Write a C program to store 5 integers in a one-dimensional array and display all the elements.

Practical / Solution

1-D Array — Exercise 1

Problem

Write a C program to store 5 integers in a one-dimensional array and display all the elements.

Program

#include <stdio.h> int main() { int arr[5]; int i; printf("Enter 5 elements: "); // Store elements in the array for (i = 0; i < 5; i++) { scanf("%d", &arr[i]); } printf("Array elements: "); // Display array elements for (i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0; }

Explanation

A one-dimensional array stores multiple values of the same data type in contiguous memory locations. The index starts from 0.

Expected Output

Enter 5 elements: 10 20 30 40 50

Array elements: 10 20 30 40 50

1-D Array — Exercise 2

Problem

Write a C program to calculate the sum of all elements of a one-dimensional array.

Program

#include <stdio.h> int main() { int arr[100], n, i, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read array elements for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Calculate sum of elements for (i = 0; i < n; i++) { sum = sum + arr[i]; } printf("Sum = %d\n", sum); return 0; }

Explanation

Each array element is added to the accumulator variable sum during the second loop.

Expected Output

Enter number of elements: 5

Enter 5 elements: 10 20 5 15 10

Sum = 60

1-D Array — Exercise 3

Problem

Write a C program to find the largest element in a one-dimensional array.

Program

#include <stdio.h> int main() { int arr[100], n, i, largest; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read array elements for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Use first element as the initial largest largest = arr[0]; // Compare remaining elements for (i = 1; i < n; i++) { if (arr[i] > largest) { largest = arr[i]; } } printf("Largest = %d\n", largest); return 0; }

Explanation

The first array element is taken as the initial largest value. Each remaining element is then compared with it.

Expected Output

Enter number of elements: 5

Enter 5 elements: 12 45 7 32 18

Largest = 45

1-D Array — Exercise 4

Problem

Write a C program to find the smallest element in a one-dimensional array.

Program

#include <stdio.h> int main() { int arr[100], n, i, smallest; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read array elements for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Use first element as initial smallest smallest = arr[0]; // Compare remaining elements for (i = 1; i < n; i++) { if (arr[i] < smallest) { smallest = arr[i]; } } printf("Smallest = %d\n", smallest); return 0; }

Explanation

The first element is initially considered the smallest and is compared with every remaining element.

Expected Output

Enter number of elements: 5

Enter 5 elements: 12 45 7 32 18

Smallest = 7

1-D Array — Exercise 5

Problem

Write a C program to calculate the average of elements stored in a one-dimensional array.

Program

#include <stdio.h> int main() { int arr[100], n, i, sum = 0; float average; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read elements and calculate total for (i = 0; i < n; i++) { scanf("%d", &arr[i]); sum = sum + arr[i]; } average = (float)sum / n; printf("Average = %.2f\n", average); return 0; }

Explanation

The sum of all elements is divided by the total number of elements. Type casting ensures that the division produces a decimal result.

Expected Output

Enter number of elements: 5

Enter 5 elements: 10 20 30 40 50

Average = 30.00

1-D Array — Exercise 6

Problem

Write a C program to count the number of even and odd elements in a one-dimensional array.

Program

#include <stdio.h> int main() { int arr[100], n, i; int even = 0, odd = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read and classify each element for (i = 0; i < n; i++) { scanf("%d", &arr[i]); if (arr[i] % 2 == 0) even++; else odd++; } printf("Even elements = %d\n", even); printf("Odd elements = %d\n", odd); return 0; }

Explanation

Each array element is checked using the modulus operator. A remainder of zero when divided by 2 indicates an even number.

Expected Output

Enter number of elements: 6

Enter 6 elements: 10 15 22 7 18 9

Even elements = 3

Odd elements = 3

1-D Array — Exercise 7

Problem

Write a C program to search for an element in a one-dimensional array using a linear search.

Program

#include <stdio.h> int main() { int arr[100], n, i, search; int found = 0; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read array elements for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter element to search: "); scanf("%d", &search); // Search each element sequentially for (i = 0; i < n; i++) { if (arr[i] == search) { found = 1; break; } } if (found) printf("Element found at index %d.\n", i); else printf("Element not found.\n"); return 0; }

Explanation

Linear search checks array elements one by one until the required value is found or the array ends.

Expected Output

Enter number of elements: 5

Enter 5 elements: 10 25 30 45 50

Enter element to search: 30

Element found at index 2.

1-D Array — Exercise 8

Problem

Write a C program to reverse the elements of a one-dimensional array.

Program

#include <stdio.h> int main() { int arr[100], n, i; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read array elements for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Reversed array: "); // Display elements from last index to first for (i = n - 1; i >= 0; i--) { printf("%d ", arr[i]); } return 0; }

Explanation

The array does not need to be physically modified to display it in reverse order. The loop simply starts from the last index.

Expected Output

Enter number of elements: 5

Enter 5 elements: 10 20 30 40 50

Reversed array: 50 40 30 20 10

1-D Array — Exercise 9

Problem

Write a C program to sort the elements of a one-dimensional array in ascending order using Bubble Sort.

Program

#include <stdio.h> int main() { int arr[100], n, i, j, temp; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter %d elements: ", n); // Read array elements for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Bubble Sort for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { // Swap adjacent elements temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } printf("Sorted array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }

Explanation

Bubble Sort repeatedly compares adjacent elements and swaps them when they are in the wrong order. After each pass, the largest unsorted element moves toward the end of the array. :contentReference[oaicite:0]{index=0}

Expected Output

Enter number of elements: 5

Enter 5 elements: 40 10 30 5 25

Sorted array: 5 10 25 30 40

1-D Array — Exercise 10

Problem

Write a C program to calculate the average, highest, and lowest marks of N students using a one-dimensional array.

Program

#include <stdio.h> int main() { int marks[100], n, i; int sum = 0, highest, lowest; float average; printf("Enter number of students: "); scanf("%d", &n); printf("Enter marks: "); // Read marks into the array for (i = 0; i < n; i++) { scanf("%d", &marks[i]); } // Initialize highest and lowest highest = lowest = marks[0]; // Process all marks for (i = 0; i < n; i++) { sum = sum + marks[i]; if (marks[i] > highest) highest = marks[i]; if (marks[i] < lowest) lowest = marks[i]; } average = (float)sum / n; printf("Average = %.2f\n", average); printf("Highest = %d\n", highest); printf("Lowest = %d\n", lowest); return 0; }

Explanation

The array stores the marks of all students. A loop is then used to calculate the total, maximum and minimum values. This combines several common array-processing operations in one program. :contentReference[oaicite:1]{index=1}

Expected Output

Enter number of students: 5

Enter marks: 12 45 3 67 22

Average = 29.80

Highest = 67

Lowest = 3