c
exercises
exercises.cš§c
/*
* =============================================================================
* ONE-DIMENSIONAL ARRAYS - EXERCISES FILE
* =============================================================================
*
* Complete the following exercises to test your understanding of
* one-dimensional arrays in C.
*
* Compilation: gcc exercises.c -o exercises
* Execution: ./exercises
* =============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
/*
* =============================================================================
* EXERCISE 1: Array Declaration and Initialization
* =============================================================================
* Create arrays using different initialization methods.
*/
void exercise1_initialization(void) {
printf("\n=== Exercise 1: Array Initialization ===\n");
// TODO: Declare an integer array of size 5 without initialization
// int arr1[5];
// TODO: Declare and initialize an array with values 2, 4, 6, 8, 10
// int arr2[5] = { ... };
// TODO: Declare an array where size is determined by initializer
// int arr3[] = { ... }; // Should have 4 elements
// TODO: Declare an array of 10 elements, all initialized to 0
// int arr4[10] = { ... };
// Print your arrays here
printf("Complete the TODOs and print the arrays.\n");
}
/*
* =============================================================================
* EXERCISE 2: Calculate Sum and Product
* =============================================================================
* Calculate the sum and product of all elements in the array.
*/
void exercise2_sum_product(void) {
printf("\n=== Exercise 2: Sum and Product ===\n");
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
// TODO: Calculate the sum of all elements
int sum = 0;
// Your code here
// TODO: Calculate the product of all elements
int product = 1;
// Your code here
printf("Array: 1, 2, 3, 4, 5\n");
printf("Sum: %d (Expected: 15)\n", sum);
printf("Product: %d (Expected: 120)\n", product);
}
/*
* =============================================================================
* EXERCISE 3: Find Second Largest
* =============================================================================
* Find the second largest element in the array.
*/
void exercise3_second_largest(void) {
printf("\n=== Exercise 3: Second Largest Element ===\n");
int arr[] = {45, 78, 23, 89, 56, 12, 67};
int size = sizeof(arr) / sizeof(arr[0]);
// TODO: Find the second largest element
int largest = 0;
int secondLargest = 0;
// Your code here
printf("Array: 45, 78, 23, 89, 56, 12, 67\n");
printf("Second largest: %d (Expected: 78)\n", secondLargest);
}
/*
* =============================================================================
* EXERCISE 4: Count Even and Odd
* =============================================================================
* Count the number of even and odd elements in the array.
*/
void exercise4_even_odd(void) {
printf("\n=== Exercise 4: Count Even and Odd ===\n");
int arr[] = {12, 7, 15, 8, 3, 10, 21, 6};
int size = sizeof(arr) / sizeof(arr[0]);
// TODO: Count even and odd numbers
int evenCount = 0;
int oddCount = 0;
// Your code here
printf("Array: 12, 7, 15, 8, 3, 10, 21, 6\n");
printf("Even numbers: %d (Expected: 4)\n", evenCount);
printf("Odd numbers: %d (Expected: 4)\n", oddCount);
}
/*
* =============================================================================
* EXERCISE 5: Reverse Array Without Extra Array
* =============================================================================
* Reverse the array in-place without using another array.
*/
void exercise5_reverse(void) {
printf("\n=== Exercise 5: Reverse Array In-Place ===\n");
int arr[] = {10, 20, 30, 40, 50};
int size = 5;
printf("Before: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
// TODO: Reverse the array in-place
// Hint: Swap elements from both ends
// Your code here
printf("After: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Expected: 50 40 30 20 10\n");
}
/*
* =============================================================================
* EXERCISE 6: Linear Search with Count
* =============================================================================
* Search for an element and count how many times it appears.
*/
void exercise6_search_count(void) {
printf("\n=== Exercise 6: Search and Count ===\n");
int arr[] = {5, 3, 7, 3, 8, 3, 2, 3, 9};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 3;
// TODO: Find all occurrences of target
int count = 0;
int positions[10]; // Store positions where found
int posCount = 0;
// Your code here
printf("Array: 5, 3, 7, 3, 8, 3, 2, 3, 9\n");
printf("Target: %d\n", target);
printf("Count: %d (Expected: 4)\n", count);
printf("Positions: ");
for (int i = 0; i < posCount; i++) printf("%d ", positions[i]);
printf("(Expected: 1 3 5 7)\n");
}
/*
* =============================================================================
* EXERCISE 7: Copy Array in Reverse
* =============================================================================
* Copy array elements to another array in reverse order.
*/
void exercise7_copy_reverse(void) {
printf("\n=== Exercise 7: Copy in Reverse ===\n");
int source[] = {1, 2, 3, 4, 5};
int dest[5];
int size = 5;
// TODO: Copy source to dest in reverse order
// Your code here
printf("Source: ");
for (int i = 0; i < size; i++) printf("%d ", source[i]);
printf("\n");
printf("Dest (reversed): ");
for (int i = 0; i < size; i++) printf("%d ", dest[i]);
printf("\n");
printf("Expected: 5 4 3 2 1\n");
}
/*
* =============================================================================
* EXERCISE 8: Insert Element at Position
* =============================================================================
* Insert an element at a specific position in the array.
*/
void exercise8_insert(void) {
printf("\n=== Exercise 8: Insert Element ===\n");
int arr[10] = {10, 20, 30, 40, 50}; // Extra space for insertion
int size = 5;
int element = 25;
int position = 2; // Insert at index 2
printf("Before: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Insert %d at position %d\n", element, position);
// TODO: Shift elements and insert new element
// Your code here
// Don't forget to increment size!
printf("After: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Expected: 10 20 25 30 40 50 (size: 6)\n");
}
/*
* =============================================================================
* EXERCISE 9: Delete Element at Position
* =============================================================================
* Delete an element at a specific position from the array.
*/
void exercise9_delete(void) {
printf("\n=== Exercise 9: Delete Element ===\n");
int arr[] = {10, 20, 30, 40, 50};
int size = 5;
int position = 2; // Delete element at index 2
printf("Before: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Delete at position %d\n", position);
// TODO: Shift elements to fill the gap
// Your code here
// Don't forget to decrement size!
printf("After: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Expected: 10 20 40 50 (size: 4)\n");
}
/*
* =============================================================================
* EXERCISE 10: Rotate Array Left
* =============================================================================
* Rotate all elements of the array left by k positions.
*/
void exercise10_rotate_left(void) {
printf("\n=== Exercise 10: Rotate Left ===\n");
int arr[] = {1, 2, 3, 4, 5};
int size = 5;
int k = 2; // Rotate left by 2 positions
printf("Before: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Rotate left by %d\n", k);
// TODO: Rotate array left by k positions
// Hint: You can do this with a temporary array or multiple reverses
// Your code here
printf("After: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Expected: 3 4 5 1 2\n");
}
/*
* =============================================================================
* EXERCISE 11: Find Missing Number
* =============================================================================
* Array contains n-1 numbers from 1 to n. Find the missing number.
*/
void exercise11_missing_number(void) {
printf("\n=== Exercise 11: Find Missing Number ===\n");
// Array has numbers 1-10, one is missing
int arr[] = {1, 2, 4, 5, 6, 7, 8, 9, 10};
int size = 9;
int n = 10; // Expected to have 1 to 10
// TODO: Find the missing number
// Hint: Use sum formula n*(n+1)/2
int missing = 0;
// Your code here
printf("Array: 1, 2, 4, 5, 6, 7, 8, 9, 10\n");
printf("Missing number: %d (Expected: 3)\n", missing);
}
/*
* =============================================================================
* EXERCISE 12: Check if Sorted
* =============================================================================
* Check if the array is sorted in ascending order.
*/
void exercise12_is_sorted(void) {
printf("\n=== Exercise 12: Check if Sorted ===\n");
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {1, 3, 2, 4, 5};
int size = 5;
// TODO: Check if arr1 is sorted
int isSorted1 = 1; // Assume true
// Your code here
// TODO: Check if arr2 is sorted
int isSorted2 = 1; // Assume true
// Your code here
printf("Array 1: 1, 2, 3, 4, 5\n");
printf("Is sorted: %s (Expected: Yes)\n", isSorted1 ? "Yes" : "No");
printf("Array 2: 1, 3, 2, 4, 5\n");
printf("Is sorted: %s (Expected: No)\n", isSorted2 ? "Yes" : "No");
}
/*
* =============================================================================
* EXERCISE 13: Find Two Elements that Sum to Target
* =============================================================================
* Find two elements in the array that add up to the target sum.
*/
void exercise13_two_sum(void) {
printf("\n=== Exercise 13: Two Sum ===\n");
int arr[] = {2, 7, 11, 15, 3, 6};
int size = 6;
int target = 9;
// TODO: Find two elements that sum to target
int found = 0;
int index1 = -1, index2 = -1;
// Your code here (use nested loops)
printf("Array: 2, 7, 11, 15, 3, 6\n");
printf("Target: %d\n", target);
if (found) {
printf("Found: arr[%d]=%d + arr[%d]=%d = %d\n",
index1, arr[index1], index2, arr[index2], target);
} else {
printf("No pair found\n");
}
printf("Expected: arr[0]=2 + arr[1]=7 = 9 OR arr[4]=3 + arr[5]=6 = 9\n");
}
/*
* =============================================================================
* EXERCISE 14: Separate Positive and Negative
* =============================================================================
* Rearrange array so all negatives come before positives.
*/
void exercise14_separate(void) {
printf("\n=== Exercise 14: Separate Positive and Negative ===\n");
int arr[] = {-3, 2, -1, 5, -4, 6, -2, 8};
int size = 8;
printf("Before: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
// TODO: Rearrange so negatives come first
// Your code here
printf("After: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Expected: All negatives before all positives\n");
}
/*
* =============================================================================
* EXERCISE 15: Calculate Running Sum
* =============================================================================
* Calculate the running sum of the array.
*/
void exercise15_running_sum(void) {
printf("\n=== Exercise 15: Running Sum ===\n");
int arr[] = {1, 2, 3, 4, 5};
int size = 5;
int runningSum[5];
// TODO: Calculate running sum
// runningSum[i] = sum of arr[0] to arr[i]
// Your code here
printf("Original: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n");
printf("Running Sum: ");
for (int i = 0; i < size; i++) printf("%d ", runningSum[i]);
printf("\n");
printf("Expected: 1 3 6 10 15\n");
}
/*
* =============================================================================
* MAIN FUNCTION
* =============================================================================
*/
int main(void) {
printf("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā\n");
printf("ā ONE-DIMENSIONAL ARRAYS - EXERCISE FILE ā\n");
printf("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā\n");
printf("\nComplete each exercise by filling in the TODO sections.\n");
exercise1_initialization();
exercise2_sum_product();
exercise3_second_largest();
exercise4_even_odd();
exercise5_reverse();
exercise6_search_count();
exercise7_copy_reverse();
exercise8_insert();
exercise9_delete();
exercise10_rotate_left();
exercise11_missing_number();
exercise12_is_sorted();
exercise13_two_sum();
exercise14_separate();
exercise15_running_sum();
printf("\nāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā\n");
printf("ā ANSWER KEY ā\n");
printf("āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā\n");
printf("\nExercise 2:\n");
printf(" for(i=0;i<size;i++) sum += arr[i];\n");
printf(" for(i=0;i<size;i++) product *= arr[i];\n");
printf("\nExercise 3:\n");
printf(" for(i=0;i<size;i++) {\n");
printf(" if(arr[i]>largest) { secondLargest=largest; largest=arr[i]; }\n");
printf(" else if(arr[i]>secondLargest) secondLargest=arr[i];\n");
printf(" }\n");
printf("\nExercise 5:\n");
printf(" for(i=0;i<size/2;i++) {\n");
printf(" temp=arr[i]; arr[i]=arr[size-1-i]; arr[size-1-i]=temp;\n");
printf(" }\n");
printf("\nExercise 8:\n");
printf(" for(i=size;i>position;i--) arr[i]=arr[i-1];\n");
printf(" arr[position]=element; size++;\n");
printf("\nExercise 11:\n");
printf(" expectedSum = n*(n+1)/2;\n");
printf(" actualSum = sum of array;\n");
printf(" missing = expectedSum - actualSum;\n");
printf("\nExercise 15:\n");
printf(" runningSum[0]=arr[0];\n");
printf(" for(i=1;i<size;i++) runningSum[i]=runningSum[i-1]+arr[i];\n");
return 0;
}