Array’s in C Exercises and Solutions
Arrays are fundamental data structures in C, allowing you to store and manipulate sequences of data elements of the same type. This guide presents a range of exercises and solutions designed to build your understanding and practical skills in working with arrays in C, covering basic operations, manipulation techniques, and advanced applications.
1. Understanding Arrays: Basics and Terminology
Before diving into exercises, let’s refresh our understanding of key concepts:
- Array Declaration:
int numbers[10];
defines an array namednumbers
with 10 elements of typeint
. - Element Access: Use
numbers[index]
to access or modify the element at a specific index (starting from 0). - Looping through Arrays: Utilize loops like
for
orwhile
to iterate through all elements or subsets. - Array Size: Use
sizeof(array_name)
to obtain the total number of elements in the array.
2. Beginner Exercises:
2.1. Summing Array Elements:
Write a program to read an array of integers and print their sum.
Solution:
C
#include <stdio.h>
int main() {
int numbers[10], sum = 0;
// Read 10 elements into the array
for (int i = 0; i < 10; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
// Calculate the sum
for (int i = 0; i < 10; i++) {
sum += numbers[i];
}
// Print the sum
printf("Sum of all elements: %d\n", sum);
return 0;
}
2.2. Finding the Maximum Element:
Write a program to find the largest element within an array of integers.
Solution:
C
#include <stdio.h>
int main() {
int numbers[10], max = numbers[0];
// Read 10 elements into the array
for (int i = 0; i < 10; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
// Compare each element with current maximum
for (int i = 1; i < 10; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
// Print the maximum element
printf("Maximum element: %d\n", max);
return 0;
}
3. Intermediate Exercises:
3.1. Reversing an Array:
Write a program to reverse the order of elements within an array.
Solution:
C
#include <stdio.h>
int main() {
int numbers[10], temp;
// Read 10 elements into the array
for (int i = 0; i < 10; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
// Swap elements using two pointers
for (int i = 0, j = 9; i < j; i++, j--) {
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
// Print the reversed array
printf("Reversed array: ");
for (int i = 0; i < 10; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
3.2. Merging Two Sorted Arrays:
C
...
if (arr1[i] < arr2[j]) {
merged_array[k] = arr1[i];
i++;
} else {
merged_array[k] = arr2[j];
j++;
}
k++;
...
}
// Copy remaining elements from either array if applicable
while (i < sizeof(arr1) / sizeof(arr1[0])) {
merged_array[k] = arr1[i];
i++;
k++;
}
while (j < sizeof(arr2) / sizeof(arr2[0])) {
merged_array[k] = arr2[j];
j++;
k++;
}
// Print the merged and sorted array
printf("Merged array: ");
for (int i = 0; i < sizeof(merged_array) / sizeof(merged_array[0]); i++) {
printf("%d ", merged_array[i]);
}
printf("\n");
return 0;
}
This solution demonstrates combining two pre-sorted arrays while maintaining the sorted order in the final merged array. It utilizes two pointers iterating through both arrays and compares elements, copying the smaller one into the merged array at each step.
4. Advanced Exercises:
For further challenges, consider exploring exercises involving:
- Implementing sorting algorithms: Implementing popular sorting algorithms like Bubble Sort, Selection Sort, or Merge Sort from scratch.
- Multi-dimensional arrays: Working with arrays with multiple dimensions (e.g., representing matrices or grids).
- Dynamic memory allocation: Allocating memory for arrays dynamically instead of relying on statically defined sizes.
Remember to modify these examples and experiment with different functionalities to solidify your understanding and gain confidence in working with arrays in C!
I hope this guide provides a valuable resource for your journey in mastering arrays in C. Don’t hesitate to ask if you have any further questions or need clarifications on specific exercises!