C Arrays
Write a C program to find the sum and average of array elements
Description: Take 5 numbers and store them in an array. Print the sum and the average.
Hint:
- Use a loop to read each element and add it to a
sumvariable.
C program to find the sum and average of an array
#include <stdio.h>
int main(void) {
int a[5], sum = 0;
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", sum / 5.0);
return 0;
}
Output:
Enter 5 numbers: 10
20
30
40
55
Sum = 155
Average = 31.00
How it works
- The loop reads the numbers into
a[0]toa[4], and adds them tosum. - We divide by
5.0to get the decimal average.
Read C Arrays.
Write a C program to find the largest element in an array
Description: Take 5 numbers in an array and print the largest one.
Hint:
- Assume the first element is the largest. Then compare the others, and update the value if a bigger one is found.
C program to find the largest element in an array
#include <stdio.h>
int main(void) {
int a[5], max;
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
max = a[0];
for (int i = 1; i < 5; i++) {
if (a[i] > max) {
max = a[i];
}
}
printf("Largest = %d\n", max);
return 0;
}
Output:
Enter 5 numbers: 23
91
45
67
12
Largest = 91
How it works
maxstarts as 23. When 91 is found, it is bigger, somaxbecomes 91. No later value is bigger.- For the smallest element, change
>to<.
Write a C program to print the elements of an array in reverse order
Description: Take 5 numbers in an array and print them in reverse order.
Hint:
- Start the loop from the last index (size – 1) and go down to 0.
C program to print an array in reverse order
#include <stdio.h>
int main(void) {
int a[5];
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
printf("Reverse order: ");
for (int i = 4; i >= 0; i--) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
Enter 5 numbers: 1
2
3
4
5
Reverse order: 5 4 3 2 1
How it works
- The second loop starts at index 4 and goes down to index 0.
- The array itself is not changed. We only print it in reverse.
Write a C program to sort an array using bubble sort
Description: Take 5 numbers in an array and sort them in ascending order.
Hint:
- Compare each pair of neighbours. If the left one is bigger, swap them. Repeat this until the array is sorted.
C program for bubble sort
#include <stdio.h>
int main(void) {
int a[5], temp;
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("Sorted array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
Enter 5 numbers: 64
25
12
22
11
Sorted array: 11 12 22 25 64
How it works
- After the first pass of the outer loop, the biggest number (64) has moved to the end. After the second pass, the second biggest is in its place, and so on.
- The inner loop runs
4 - itimes, because the lastielements are already sorted.
Write a C program to search an element in an array
Description: Take an array of 5 numbers and a number to search. Print its position, or a message if it is not found.
Hint:
- Compare the number with every element one by one. Stop when it is found.
C program for linear search
#include <stdio.h>
int main(void) {
int a[5] = {12, 45, 7, 67, 30};
int key, found = -1;
printf("Enter the number to search: ");
scanf("%d", &key);
for (int i = 0; i < 5; i++) {
if (a[i] == key) {
found = i;
break;
}
}
if (found != -1) {
printf("%d found at index %d\n", key, found);
} else {
printf("%d not found\n", key);
}
return 0;
}
Output:
Enter the number to search: 67
67 found at index 3
How it works
foundstarts as -1, which means “not found”.- When the value matches, we save the index in
foundand stop the loop withbreak. - Index 3 is the fourth element, because indexes start from 0.
Write a C program to add two matrices
Description: Take the elements of two 2 x 2 matrices as input. Print the sum matrix.
Hint:
- Add the elements which are at the same row and column:
sum[i][j] = a[i][j] + b[i][j].
C program to add two matrices
#include <stdio.h>
int main(void) {
int a[2][2], b[2][2], sum[2][2];
printf("Enter 4 elements of the first matrix: ");
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
scanf("%d", &a[i][j]);
printf("Enter 4 elements of the second matrix: ");
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
scanf("%d", &b[i][j]);
printf("Sum of the matrices:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter 4 elements of the first matrix: 1
2
3
4
Enter 4 elements of the second matrix: 5
6
7
8
Sum of the matrices:
6 8
10 12
How it works
- The nested loops go through each row and each column.
- The result matrix has two rows: 6 8 and 10 12.
Read C Two Dimensional Arrays.