C Programming Fundamentals: Arrays, Functions, and Search Algorithms
Array Initialization Methods in C
Arrays in C can be initialized in two main ways:
1. Compile-Time Initialization
Values are assigned at the time of declaration.
int arr[5] = {1, 2, 3, 4, 5};
If fewer values are provided than the array size, the unused elements are automatically initialized to 0:
int arr[5] = {1, 2}; // arr = {1, 2, 0, 0, 0}
2. Run-Time Initialization
Values are entered during execution using input functions like scanf().
int arr[5], i;
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
Pros and Cons of Using C Arrays
Advantages of Arrays
- Efficient storage of similar data types.
- Index-based access allows fast retrieval of elements.
- Easy to traverse and manipulate using loops.
Disadvantages of Arrays
- Fixed size: The size cannot be changed during execution.
- No bounds checking: Accessing out-of-bound elements causes undefined behavior.
- Static memory allocation (typical for standard arrays).
Understanding Array Indexing (Subscripts)
An index or subscript is a number used to access specific elements in an array. Array indexing always starts from 0.
int arr[3] = {10, 20, 30};
printf("%d", arr[1]); // Outputs 20
Valid Data Types for Indexes
Indexes must be of an integer type, such as int, short, or char.
C Program Example: Reversing a 3-Digit Number
This program demonstrates basic arithmetic operations to reverse a three-digit integer.
#include <stdio.h>
int main() {
int num, rev;
printf("Enter a 3-digit number: ");
scanf("%d", &num);
// Logic: Extract digits and reconstruct the number in reverse order
rev = (num % 10) * 100 + ((num / 10) % 10) * 10 + num / 100;
printf("Reversed: %d", rev);
return 0;
}
C Array Definition and Declaration Syntax
What is an Array?
An Array is a collection of elements of the same data type stored in contiguous memory locations.
Declaration Syntax
Declaring an array reserves memory space based on the specified size:
int arr[5];
This declares an integer array named arr of size 5.
Initialization Example
int marks[3] = {80, 90, 100};
Function Declaration vs. Definition in C
Function Declaration (Prototype)
The declaration, also known as the function prototype, informs the compiler about the function’s name, return type, and parameters.
int add(int, int); // Declaration/Prototype
Importance of Prototypes
- Helps the compiler catch type mismatches.
- Supports modular programming by allowing functions to be defined after
main().
Function Definition
The definition provides the actual body (implementation) of the function, detailing the operations it performs.
int add(int a, int b) {
return a + b;
}
Single vs. Multidimensional Arrays in C
Single-Dimensional Arrays
These arrays require only one index to access elements (conceptually like a list or vector).
int a[5];
Multidimensional Arrays
These arrays require more than one index (e.g., a matrix or table).
int b[2][3];
Access Example: b[1][2] refers to the element located in the 2nd row and 3rd column.
Implementing Binary Search in C
Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing the search interval in half.
#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1; // Element not found
}
int main() {
int arr[] = {1, 3, 5, 7, 9}; // Array must be sorted
int index = binarySearch(arr, 5, 7);
if(index != -1)
printf("Element found at index %d", index);
else
printf("Element not found");
return 0;
}
Output
Element found at index 3
C Function Components: Definition, Declaration, and Calling
What is a Function?
A Function is a self-contained block of code designed to perform a specific, reusable task.
Declaration
int square(int); // Prototype
Definition
int square(int x) {
return x * x;
}
Calling the Function
Executing the function by passing required arguments:
int result = square(5);
