Through exercises on one-dimensional arrays, you can better grasp the definition, initialization, traversal, and common operations of arrays. Below are some exercises on one-dimensional arrays to help you consolidate your knowledge.
1. Exercise: Calculate the Sum of an Array
Write a program that inputs 5 integers, stores them in a one-dimensional array, and then calculates and outputs the sum of these integers.
Example Code:
#include <stdio.h>
int main() {
int arr[5];
int sum = 0;
// Input array elements
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
// Calculate sum
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum of the elements = %d\n", sum);
return 0;
}
Note: The program inputs 5 integers, stores them in the array <span>arr[]</span>, and then calculates the sum of the array elements through a loop.
2. Exercise: Find the Maximum Value in an Array
Write a program that inputs 10 integers, stores them in a one-dimensional array, and then finds and outputs the maximum value in the array.
Example Code:
#include <stdio.h>
int main() {
int arr[10];
int max;
// Input array elements
printf("Enter 10 integers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Assume the first element is the maximum
max = arr[0];
// Find the maximum value
for (int i = 1; i < 10; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("The maximum value is: %d\n", max);
return 0;
}
Note: The program first assumes the first element of the array is the maximum value, then traverses the array to update the maximum value.
3. Exercise: Find the Minimum Value in an Array
Write a program that inputs 10 integers, stores them in a one-dimensional array, and then finds and outputs the minimum value in the array.
Example Code:
#include <stdio.h>
int main() {
int arr[10];
int min;
// Input array elements
printf("Enter 10 integers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Assume the first element is the minimum
min = arr[0];
// Find the minimum value
for (int i = 1; i < 10; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
printf("The minimum value is: %d\n", min);
return 0;
}
Note: The program assumes the first element of the array is the minimum value and searches for and updates the minimum value during traversal.
4. Exercise: Reverse the Elements of an Array
Write a program that inputs an array containing 5 integers and outputs the reversed array.
Example Code:
#include <stdio.h>
int main() {
int arr[5], reversed[5];
// Input array elements
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
// Reverse the array
for (int i = 0; i < 5; i++) {
reversed[i] = arr[4 - i];
}
// Output the reversed array
printf("Reversed array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", reversed[i]);
}
printf("\n");
return 0;
}
Note: This program traverses the array <span>arr[]</span> and stores its elements in reverse order in the <span>reversed[]</span> array, ultimately outputting the reversed array.
5. Exercise: Calculate the Average of Array Elements
Write a program that inputs 5 integers, stores them in a one-dimensional array, and then calculates and outputs the average of these integers.
Example Code:
#include <stdio.h>
int main() {
int arr[5];
int sum = 0;
float average;
// Input array elements
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
// Calculate sum
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
// Calculate average
average = (float)sum / 5;
printf("The average value is: %.2f\n", average);
return 0;
}
Note: The program calculates the sum of the input array elements and obtains the average by dividing by the number of elements (5), finally outputting the average.
6. Exercise: Check if the Array is Symmetric
Write a program that inputs an array containing 5 integers and checks if the array is symmetric (i.e., the elements are the same from left to right and from right to left).
Example Code:
#include <stdio.h>
#include <stdbool.h>
int main() {
int arr[5];
bool isSymmetric = true;
// Input array elements
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
// Check for symmetry
for (int i = 0; i < 5 / 2; i++) {
if (arr[i] != arr[4 - i]) {
isSymmetric = false;
break;
}
}
if (isSymmetric) {
printf("The array is symmetric.\n");
} else {
printf("The array is not symmetric.\n");
}
return 0;
}
Note: The program checks if the array is symmetric by comparing the <span>i</span>th element with the <span>n-i-1</span>th element.
7. Exercise: Remove Duplicate Elements from an Array
Write a program that inputs an array containing multiple integers, removes the duplicate elements, and outputs the deduplicated array.
Example Code:
#include <stdio.h>
int main() {
int arr[10], unique[10];
int n, uniqueCount = 0;
// Input array elements
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Remove duplicates
for (int i = 0; i < n; i++) {
int j;
for (j = 0; j < uniqueCount; j++) {
if (arr[i] == unique[j]) {
break; // If a duplicate is found, break the inner loop
}
}
if (j == uniqueCount) {
unique[uniqueCount] = arr[i];
uniqueCount++;
}
}
// Output the deduplicated array
printf("Array after removing duplicates: ");
for (int i = 0; i < uniqueCount; i++) {
printf("%d ", unique[i]);
}
printf("\n");
return 0;
}
Note: The program checks and removes duplicate elements from the array using two loops, ultimately outputting the deduplicated array.
8. Exercise: Sort the Array Elements (Ascending Order)
Write a program that inputs 10 integers, stores them in a one-dimensional array, and then sorts the array elements in ascending order.
Example Code:
#include <stdio.h>
int main() {
int arr[10];
int temp;
// Input array elements
printf("Enter 10 integers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// Bubble sort (ascending order)
for (int i = 0; i < 10 - 1; i++) {
for (int j = 0; j < 10 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Output the sorted array
printf("Sorted array (ascending order): ");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Note: The program uses bubble sort to arrange the array in ascending order, gradually moving the largest element to the end.