Week 6 Syntax Guide
Passing Arrays to Functions
When passing an array to a function, the array “decays” into a pointer to its first element. Changes inside the function affect the original array.
Syntax:
// Function Prototype
void functionName(dataType arrayName[], int size);
// Function Call
functionName(actualArray, size);
Example:
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int nums[5] = {1, 2, 3, 4, 5};
printArray(nums, 5); // Pass array name and size
return 0;
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}
int main() {
int nums[5] = {1, 2, 3, 4, 5};
printArray(nums, 5); // Pass array name and size
return 0;
}
Searching Algorithms
Linear Search
Iterates through the array to find a specific key. Works on unsorted arrays.
Syntax:
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}
Binary Search
Divides the search interval in half. Requires a sorted array.
Syntax:
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] < key)
low = mid + 1; // Ignore left half
else if (arr[mid] > key)
high = mid - 1; // Ignore right half
else
return mid; // Match found
}
return -1; // Not found
}
Common Array Algorithms
Reversing an Array
Swaps elements from both ends moving towards the center.
Syntax:
void reverseArray(int arr[], int size) {
int start = 0, end = size - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
Rotating an Array
Moves elements to the right or left, wrapping around.
Rotate Right:
void rotateRight(int arr[], int size) {
int last = arr[size - 1];
for (int i = size - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = last;
}
Rotate Left:
void rotateLeft(int arr[], int size) {
int first = arr[0];
for (int i = 0; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
arr[size - 1] = first;
}