Great! Below are the exercises related to Chapter 13 “Functions and One-Dimensional Arrays” in C language. Each code segment includes detailed comments and function descriptions to help you understand better.
Exercise 1: Calculate the Sum of a One-Dimensional Array
Requirement: Write a function to calculate the sum of all elements in a one-dimensional array.
#include <stdio.h>
// Function: sumArray
// This function takes an integer array and its size, returning the sum of all elements in the array.
int sumArray(int arr[], int size) {
int sum = 0; // Initialize sum to 0
for(int i = 0; i < size; i++) { // Iterate through each element of the array
sum += arr[i]; // Add each element to the total sum
}
return sum; // Return the total sum of array elements
}
int main() {
int arr[] = {1, 2, 3, 4, 5}; // Define and initialize the array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
int total = sumArray(arr, size); // Call sumArray function to calculate the sum of the array
printf("The sum of the array elements is: %d\n", total); // Output the result
return 0;
}
Comment Explanation:
- •
<span>sumArray</span>function: This function is used to calculate the sum of all elements in the passed array. It takes the array<span>arr[]</span>and the length of the array<span>size</span>as parameters. - • In the
<span>main()</span>function, the<span>sumArray</span>function is called to calculate the total sum of the array<span>arr[]</span>and output the result.
Exercise 2: Find the Maximum Value in an Array
Requirement: Write a function to return the maximum value in a one-dimensional array.
#include <stdio.h>
// Function: maxArray
// This function takes an integer array and its size, returning the maximum value in the array.
int maxArray(int arr[], int size) {
int max = arr[0]; // Assume the first element is the maximum
for(int i = 1; i < size; i++) { // Start iterating from the second element
if(arr[i] > max) { // If the current element is greater than the maximum
max = arr[i]; // Update the maximum value
}
}
return max; // Return the maximum value
}
int main() {
int arr[] = {10, 25, 7, 90, 42}; // Define and initialize the array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
int maxValue = maxArray(arr, size); // Call maxArray function to get the maximum value
printf("The maximum value in the array is: %d\n", maxValue); // Output the maximum value
return 0;
}
Comment Explanation:
- •
<span>maxArray</span>function: This function is used to find and return the maximum value in the array. It initializes<span>max</span>to the first element of the array, iterates through the other elements, and updates<span>max</span>to the current maximum value. - • In the
<span>main()</span>function, the<span>maxArray</span>function is called and the maximum value is output.
Exercise 3: Reverse an Array
Requirement: Write a function to reverse a one-dimensional array.
#include <stdio.h>
// Function: reverseArray
// This function takes an integer array and its size, reversing the elements in the array.
void reverseArray(int arr[], int size) {
int temp; // Temporary variable for swapping elements
for(int i = 0; i < size/2; i++) { // Only need to swap elements from the front half with the back half
temp = arr[i]; // Store the current element in a temporary variable
arr[i] = arr[size - 1 - i]; // Swap the back half element to the front half
arr[size - 1 - i] = temp; // Swap the front half element to the back half
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5}; // Define and initialize the array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
reverseArray(arr, size); // Call reverseArray function to reverse the array
printf("Reversed array: ");
for(int i = 0; i < size; i++) { // Iterate and output the reversed array
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Comment Explanation:
- •
<span>reverseArray</span>function: This function reverses the array by swapping elements from the front and back. It uses a temporary variable<span>temp</span>to perform the swap. - • In the
<span>main()</span>function, the<span>reverseArray</span>function is called to reverse the array and output the result.
Exercise 4: Calculate the Average Value of an Array
#include <stdio.h>
// Function: averageArray
// This function takes an integer array and its size, calculating and returning the average value of the array elements.
float averageArray(int arr[], int size) {
int sum = 0; // Initialize sum to 0
for(int i = 0; i < size; i++) { // Iterate through the array to calculate the total sum
sum += arr[i];
}
return (float)sum / size; // Return the average value (note type casting)
}
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Define and initialize the array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
float avg = averageArray(arr, size); // Call averageArray function to calculate the average value
printf("The average value of the array is: %.2f\n", avg); // Output the average value
return 0;
}
Comment Explanation:
- •
<span>averageArray</span>function: This function is used to calculate the average value of the array. It first calculates the sum of the array elements, then divides the total by the number of elements. - • In the
<span>main()</span>function, the<span>averageArray</span>function is called to calculate and output the average value.
Exercise 5: Count Array Element Frequencies
Requirement: Count the occurrences of each element in the array.
#include <stdio.h>
// Function: countFrequency
// This function takes an integer array and its size, counting and outputting the occurrences of each element in the array.
void countFrequency(int arr[], int size) {
int counted[size]; // Array to mark elements that have been counted
for(int i = 0; i < size; i++) counted[i] = 0; // Initialize the marked array to 0
for(int i = 0; i < size; i++) {
if(counted[i] == 0) { // If the current element has not been counted
int freq = 1; // Initial frequency is 1
for(int j = i+1; j < size; j++) {
if(arr[i] == arr[j]) { // If there are duplicate elements
freq++; // Increase frequency by 1
counted[j] = 1; // Mark this element as counted
}
}
printf("Element %d appears %d times\n", arr[i], freq); // Output the current element and its frequency
}
}
}
int main() {
int arr[] = {1, 2, 2, 3, 3, 3, 4}; // Define and initialize the array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
countFrequency(arr, size); // Call countFrequency function to count frequencies
return 0;
}
Comment Explanation:
- •
<span>countFrequency</span>function: This function traverses the array using a nested loop to count the occurrences of each element. It uses a<span>counted[]</span>array to mark elements that have been counted, avoiding duplicate counting. - • In the
<span>main()</span>function, the<span>countFrequency</span>function is called to count and output the occurrences of each element.
Summary
- • Functions: Functions are modular code blocks that can be reused, enhancing code readability and maintainability.
- • Arrays: Arrays are used to store data of the same type, often used with functions as parameters.
- • Combining Arrays and Functions: By passing arrays as parameters to functions, functions can manipulate the data within the arrays.