In C programming, arrays are fundamental data structures, and understanding how to pass arrays to functions, as well as how to sort and search arrays, is crucial for building efficient and effective programs. Let’s break down these concepts:
In C, arrays can be passed to functions in two ways: by reference and by pointer. When passing arrays to functions, the array's name is treated as a pointer to the first element of the array. This means that when you pass an array to a function, you're actually passing the address of the first element, and the function can modify the array's elements.
void functionName(type arrayName[], int size) {
// Function code
}
Alternatively, you can also pass the array using pointers:
void functionName(type *arrayName, int size) {
// Function code
}
In both cases, the array is passed by reference (using the array's memory address), so modifications inside the function will affect the original array.
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int numbers[] = {5, 10, 15, 20, 25};
int size = sizeof(numbers) / sizeof(numbers[0]);
printArray(numbers, size); // Passing array to function
return 0;
}
printArray takes an array arr[] and its size as parameters.numbers is passed to printArray in the main function.5 10 15 20 25
Sorting an array means arranging its elements in a specific order, either in ascending or descending order. There are several algorithms to sort an array, with Bubble Sort and Selection Sort being two of the simplest.
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
Bubble Sort works by "bubbling" the largest element to the end of the array after each pass.
#include <stdio.h>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int numbers[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Unsorted array: \n");
printArray(numbers, size);
bubbleSort(numbers, size); // Sorting the array
printf("Sorted array: \n");
printArray(numbers, size);
return 0;
}
bubbleSort performs the sorting by comparing adjacent elements and swapping them if they are in the wrong order.printArray function prints the array before and after sorting.Unsorted array:
64 34 25 12 22 11 90
Sorted array:
11 12 22 25 34 64 90
Selection Sort works by selecting the smallest (or largest) element from the unsorted part of the array and swapping it with the first unsorted element.
#include <stdio.h>
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int numbers[] = {64, 25, 12, 22, 11};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Unsorted array: \n");
printArray(numbers, size);
selectionSort(numbers, size); // Sorting the array
printf("Sorted array: \n");
printArray(numbers, size);
return 0;
}
selectionSort function selects the smallest element in the unsorted portion of the array and swaps it with the element at the beginning of the unsorted portion.printArray function prints the array before and after sorting.Unsorted array:
64 25 12 22 11
Sorted array:
11 12 22 25 64
Searching an array involves finding a specific element in the array. There are different searching algorithms, with Linear Search and Binary Search being the most common.
Linear Search is the simplest search algorithm. It checks each element in the array, one by one, to see if it matches the target value. It is a sequential search method.
#include <stdio.h>
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return the index if the element is found
}
}
return -1; // Return -1 if the element is not found
}
int main() {
int numbers[] = {5, 10, 15, 20, 25};
int size = sizeof(numbers) / sizeof(numbers[0]);
int target = 20;
int result = linearSearch(numbers, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
linearSearch searches through the array by comparing each element with the target value.-1 is returned.Element 20 found at index 3.
Binary Search works only on sorted arrays. It repeatedly divides the search interval in half. If the target value is less than the middle element, the search continues in the left half; otherwise, it continues in the right half. Binary Search has a time complexity of O(log n), which is faster than Linear Search's O(n).
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// Check if the target is at the middle
if (arr[mid] == target) {
return mid;
}
// If target is greater, ignore the left half
if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller, ignore the right half
else {
right = mid - 1;
}
}
return -1; // Return -1 if the element is not found
}
int main() {
int numbers[] = {5, 10, 15, 20, 25};
int size = sizeof(numbers) / sizeof(numbers[0]);
int target = 15;
int result = binarySearch(numbers, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
binarySearch works on sorted arrays and efficiently searches for the target using the divide-and-conquer method.-1 if not.Element 15 found at index 2.
Passing Arrays to Functions:
Sorting Arrays:
Searching Arrays:
These concepts are foundational for working with arrays in C and allow you to manipulate and search data efficiently.
Open this section to load past papers