Basic Array Programming Questions

Here are 10 C programming exercises that involve arrays:

  1. Sum of Array Elements: Write a program to calculate the sum of all elements in an array.

  2. Average of Array Elements: Create a program to find the average of elements in an array.

  3. Largest Element in an Array: Develop a program to find the largest element in an array.

  4. Smallest Element in an Array: Write a program to find the smallest element in an array.

  5. Reverse Array: Implement a program to reverse the order of elements in an array.

  6. Search Element in Array: Create a program that searches for a specific element in an array and prints its index.

  7. Even/Odd Count in an Array: Write a program to count the number of even and odd elements in an array.

  8. Copy Array: Develop a program to copy the elements of one array into another array.

  9. Merge Arrays: Create a program to merge two arrays into a third array.

  10. Remove Duplicate Elements: Write a program to remove duplicate elements from an array and print the resulting array.

These exercises cover a range of array-related concepts and will help you practice and strengthen your skills in C programming. I have solved all of the above with explanatory comments. You should first make a sincere attempt at solving these problems before looking at the solutions.

1. Sum of Array Elements:

#include <stdio.h>

int main() {
    // Declare an array
    int arr[] = {1, 2, 3, 4, 5};

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Initialize a variable to store the sum of elements
    int sum = 0;

    // Iterate through the array and calculate the sum
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    // Print the sum of array elements
    printf("Sum of array elements: %d\n", sum);

    return 0;
}

2. Average of Array Elements:

#include <stdio.h>

int main() {
    // Declare an array
    int arr[] = {1, 2, 3, 4, 5};

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Initialize a variable to store the sum of elements
    int sum = 0;

    // Iterate through the array and calculate the sum
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    // Calculate the average
    double average = (double)sum / n;

    // Print the average of array elements
    printf("Average of array elements: %.2f\n", average);

    return 0;
}

3. Largest Element in Array:

#include <stdio.h>

int main() {
    // Declare an array
    int arr[] = {10, 5, 8, 15, 7};

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Initialize a variable to store the maximum element
    int max = arr[0];

    // Iterate through the array to find the largest element
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    // Print the largest element in the array
    printf("Largest element in array: %d\n", max);

    return 0;
}

4. Smallest Element in Array:

#include <stdio.h>

int main() {
    // Declare an array
    int arr[] = {10, 5, 8, 15, 7};

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Initialize a variable to store the minimum element
    int min = arr[0];

    // Iterate through the array to find the smallest element
    for (int i = 1; i < n; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }

    // Print the smallest element in the array
    printf("Smallest element in array: %d\n", min);

    return 0;
}

5. Reverse Array:

#include <stdio.h>

int main() {
    // Declare an array
    int arr[] = {1, 2, 3, 4, 5};

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Print the original array
    printf("Original Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    // Print the reversed array
    printf("\nReversed Array: ");
    for (int i = n - 1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;
}

6. Search Element in Array:

#include <stdio.h>

int main() {
    // Declare an array
    int arr[] = {10, 5, 8, 15, 7};

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Specify the target element to search
    int target = 8;

    // Initialize a variable to check if the element is found
    int found = 0;

    // Iterate through the array to search for the target element
    for (int i = 0; i < n; i++) {
        if (arr[i] == target) {
            // Print the index if the element is found
            printf("Element %d found at index %d\n", target, i);
            found = 1;
            break;
        }
    }

    // Print a message if the element is not found
    if (!found) {
        printf("Element %d not found in the array\n", target);
    }

    return 0;
}

7. Even/Odd Count in Array:

#include <stdio.h>

int main() {
    // Declare an array
    int arr[] = {1, 2, 3, 4, 5};

    // Calculate the number of elements in the array
    int n = sizeof(arr) / sizeof(arr[0]);

    // Initialize variables to count even and odd elements
    int evenCount = 0, oddCount = 0;

    // Iterate through the array to count even and odd elements
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0) {
            evenCount++;
        } else {
            oddCount++;
        }
    }

    // Print the count of even and odd elements
    printf("Even elements: %d\n", evenCount);
    printf("Odd elements: %d\n", oddCount);

    return 0;
}

8. Copy Array:

#include <stdio.h>

int main() {
    // Declare an array
    int arr1[] = {1, 2, 3, 4, 5};

    // Calculate the number of elements in the array
    int n = sizeof(arr1) / sizeof(arr1[0]);

    // Declare another array for copying
    int arr2[n];

    // Iterate through the first array and copy elements to the second array
    for (int i = 0; i < n; i++) {
        arr2[i] = arr1[i];
    }

    // Print the original and copied arrays
    printf("Original Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr1[i]);
    }

    printf("\nCopied Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr2[i]);
    }

    printf("\n");

    return 0;
}

9. Merge Arrays:

#include <stdio.h>

int main() {
    // Declare two arrays
    int arr

1[] = {1, 2, 3};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[] = {4, 5, 6};
    int n2 = sizeof(arr2) / sizeof(arr2[0]);

    // Declare an array for merged elements
    int merged[n1 + n2];

    // Copy elements from the first array to the merged array
    for (int i = 0; i < n1; i++) {
        merged[i] = arr1[i];
    }

    // Copy elements from the second array to the merged array
    for (int i = 0; i < n2; i++) {
        merged[n1 + i] = arr2[i];
    }

    // Print the merged array
    printf("Merged Array: ");
    for (int i = 0; i < n1 + n2; i++) {
        printf("%d ", merged[i]);
    }

    printf("\n");

    return 0;
}

10. Remove Duplicate Elements:

#include <stdio.h>

int main() {
    // Declare an array with duplicate elements
    int arr[] = {1, 2, 2, 3, 4, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Create an array to store unique elements
    int unique[n];

    // Initialize a variable to track the size of the unique array
    int size = 0;

    // Iterate through the original array to remove duplicates
    for (int i = 0; i < n; i++) {
        int isDuplicate = 0;

        // Check if the element already exists in the unique array
        for (int j = 0; j < size; j++) {
            if (arr[i] == unique[j]) {
                isDuplicate = 1;
                break;
            }
        }

        // If not a duplicate, add the element to the unique array
        if (!isDuplicate) {
            unique[size++] = arr[i];
        }
    }

    // Print the original array with duplicates
    printf("Array with duplicates: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    // Print the unique array without duplicates
    printf("\nArray without duplicates: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", unique[i]);
    }

    printf("\n");

    return 0;
}