C Program to Calculate Average of Elements of an Array.

Problem: Given an integer Array of size n, the task is to write C programming code to calculate the average of all the elements present in the given array.

Example:

Input: arr[] = {3, 5, 9, 2, 8}
Output: 5.40

Explanation:
Sum of elements = 3+5+9+2+8 = 27
Total number of elements = 5
Average = 27/5 = 5.40

Algorithm to Calculate Array Average.

Here is a step-by-step algorithm to calculate the average of an array in C language using pseudocode:
  • Declare an integer variable n to store the size of the array.
  • Declare an integer array arr of size n.
  • Declare an integer variable sum and initialize it to 0.
  • Declare a float variable avg and initialize it to 0.
  • Read the value of n from the user.
  • Read the values of the array arr[] from the user.
  • Traverse the array arr from index 0 to index n-1.
  • Add each element of the array arr to the variable sum.
  • Calculate the average of the array by dividing the sum of the elements by the number of elements in the array.
  • Store the result in the variable avg.
  • Display the value of avg.

Pseudocode to Calculate the Average of all elements of an array.

1. Declare n as integer
2. Declare arr[n] as integer
3. Declare sum as float and initialize it to 0
4. Declare avg as float and initialize it to 0
5. Read n from user
6. Read arr from user
7. for i = 0 to n-1 do
8.     sum = sum + arr[i]
9. end for
10. avg = sum / n
11. Display avg

C Program to Calculate the Average of all elements of an array.

C Code:
//C program to find average of array elements
#include <stdio.h>

int main() {
    int size;
    printf("Enter the size of the array: ");
    scanf("%d", &size);

    int arr[size];
    printf("Enter %d elements:\n", size);
    
    // Input array elements and calculate sum
    int sum = 0;
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
        sum += arr[i];
    }

    // Calculate average
    float average = (float) sum / size;

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

    return 0;
}
Output:
Enter the size of the array: 6
Enter 6 elements:
12 9 3 2 10 5
Average of array elements: 6.83

Time Complexity: O(n) where is the size of the given array
Space Complexity: O(1) as constant extra space is required.

Related Articles:

C Program to Find and Display duplicate Elements in an array.

Problem Statement: Given an array of integers. Your task is to write a C program to find and display the duplicate elements present in the array.

Example:

Input: arr[] = {3, 2, 4, 2, 7, 8, 4, 1, 8}
Output: Duplicate numbers: 2 4 8

Input: arr[] = {1, 4, 1, 3, 2}
Output: Duplicate numbers: 1

Algorithm to Find Duplicate Elements.

Below are the steps that you need to follow:

Step 1: Traverse through the array one element at a time.
Step 2: For each element, check if it appears again later in the array.
Step 3: If the element is found later in the array and is not already marked as a duplicate, mark it as a duplicate and print it.
Step 4: Continue this process for each element.

Below is the C program implementation to find and display duplicate numbers from the given array.

C code:
//C program to find duplicate number from the given array
#include <stdio.h>

int main() {
    int arr[] = {3, 2, 4, 2, 7, 8, 4, 1, 8};
    int size = sizeof(arr) / sizeof(arr[0]);

    printf("Duplicate elements: ");
    //traverse to find duplicate
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                printf("%d ", arr[i]);
                break;
            }
        }
    }

    printf("\n");
    return 0;
}
Output:
Duplicate elements: 2 4 8 

Time Complexity: The time complexity of this algorithm is O(n^2) in the worst case, where n is the number of elements in the array. 

Space Complexity: The space complexity of this algorithm is O(1) as it uses a constant amount of extra space to store the loop variables and other variables.

Related Articles:

C Program to Count Odd and Even Elements in an Array.

Problem Statement: Given an array of integers. Your task is to write a C program to count the number of odd and even elements in the array.

  • Even Number: A number that is completely divisible by 2.
  • Odd Number: A number that is not completely divisible by 2.

Example: 

Input: arr[] = {2, 4, 5, 7, 10}
Output: Even Count = 3
        Odd Count = 2

Input: arr[] = {12, 13, 8, 2, 10}
Output: Even Count = 4
        Odd Count = 1

Algorithm to Count Even and Odd Elements.

Below are the steps that need to follow:

Step 1: Initialize two variables, evenCount and oddCount, to keep track of the count of even and odd elements respectively. Set both counts to zero.
Step 2: Traverse through the array one element at a time.
Step 3: For each element, check if it is even or odd.
Step 4: If the element is even, increment the evenCount by 1.
Step 5: If the element is odd, increment the oddCount by 1.
Step 6: After traversing through the entire array, output the values of evenCount and oddCount.

Program to Count numbers of odd and even elements in an Array.

// C program to count even and odd numbers in the array
#include <stdio.h>

int main() {
    int arr[] = {2, 4, 5, 7, 10};
    //size of given array
    int size = sizeof(arr) / sizeof(arr[0]);
    int evenCount = 0, oddCount = 0;
    
    //check even and odd
    for (int i = 0; i < size; i++) {
        if (arr[i] % 2 == 0) {
            evenCount++;
        } else {
            oddCount++;
        }
    }

    printf("Number of even elements: %d\n", evenCount);
    printf("Number of odd elements: %d\n", oddCount);

    return 0;
}
Output:
Number of even elements: 3
Number of odd elements: 2

Time Complexity: The time complexity of this algorithm is O(n) because it needs to traverse through all the elements of the array once.

Space Complexity: The space complexity of this algorithm is O(1) as it uses a constant amount of extra space to store the loop variable and other variables.

Similar articles:

C Program to Search for a given Element in an Array.

Problem Statement: You are given an array of integers and a target element. Your task is to find if the target element exists in the array or not. Write a C program to search the element.

Example:

Suppose we have an array arr[] = {2, 4, 6, 8, 10}, and the target element is 6. The program should output "Element found" since 6 is present in the array.


Linear Searching Algorithm.

Below is the step-by-step algorithm to search for an element from the given array if exists:

Step 1: Start with the first element of the array and compare it with the target element.

Step 2: If the current element is equal to the target element, return "Element found".

Step 3: If the current element is not equal to the target element, move on to the next element in the array.

Step 4: Repeat steps 2 and 3 until you find the target element or reach the end of the array.

Step 5: If the end of the array is reached and the target element is not found, return "Element not found".


Program to search an element in the given array.

//C program to search an element from the given array
#include <stdio.h>

int searchElement(int arr[], int size, int target) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) {
            return 1; // Element found
        }
    }
    return 0; // Element not found
}

int main() {
    int arr[] = {2, 4, 6, 8, 10};
    int size = sizeof(arr) / sizeof(arr[0]);
    int target = 6;

    if (searchElement(arr, size, target)) {
        printf("Element found\n");
    } else {
        printf("Element not found\n");
    }

    return 0;
}
Output:
Element found

Time Complexity: The time complexity of this algorithm is O(n) because, in the worst-case scenario, we might have to check all the elements of the array to find the target element.

Space Complexity: The space complexity of this algorithm is O(1) as it uses a constant amount of extra space to store the loop variable and other variables.

C Program to Reverse the Elements of an Array.

Problem Statement: Given an array of integers, write a C program to reverse the elements of the array in place. 

Note: In-place means without using any additional data structure to store the reverse elements. The original array is updated to contain its elements in reverse order, without creating a new array to hold the reversed elements. 


Example:

Input: arr[] = {10, 20, 30, 40, 50}
Output: Reversed array: {50, 40, 30, 20, 10}

Algorithm to Reverse Array Elements.

Below are the steps that we need to follow:

Step 1: Take the size of the array and create an array arr of that size.
Step 2: Input the elements of the array arr.
Step 3: Initialize two variables, start with 0 and end to the size of the array minus 1.
Step 4: Swap the elements at start and end indexes and increment start and decrement end until the start becomes greater than or equal to the end.
Step 5: Print the reversed array.

Program to Reverse the Elements of an Array.

// C program to reverse array elements in-place 
#include <stdio.h>

// Function to swap two elements
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int n;

    printf("Enter the size of the array: ");
    scanf("%d", &n);

    int arr[n]; 

    // Input the elements of the array
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    int start = 0;
    int end = n - 1;

    while (start < end) {
        swap(&arr[start], &arr[end]);
        start++;
        end--;
    }

    printf("Reversed array: {");
    for (int i = 0; i < n; i++) {
        printf("%d", arr[i]);
        if (i < n - 1) {
            printf(", ");
        }
    }
    printf("}\n");

    return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
11 10 9 20 8
Reversed array: {8, 20, 9, 10, 11}

Time Complexity: The time complexity of this code is O(n) because we need to traverse through the entire array once to reverse the elements.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to solve this problem.

Reverse the Elements of the Array Using Recursion.

We can also reverse the order of the elements stored in the given array by using the Recursive method. 

Algorithm:
1. Base Case: If the array is empty or has only one element, it is already reversed.
2. Recursive Case:
  • Swap the first and last elements of the array.
  • Recursively reverse the remaining subarray.

C Program to Reverse Array Using Recursion.

// C code to reverse order of an array using recursive method
#include <stdio.h>

// Function to swap two elements
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// Recursive function to reverse an array
void reverseArray(int arr[], int start, int end) {
    // Base case: if the array is empty or has only one element
    if (start >= end) {
        return;
    }

    // Swap the first and last elements
    swap(&arr[start], &arr[end]);

    // Recursively reverse the remaining subarray
    reverseArray(arr, start + 1, end - 1);
}

// Function to print an array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    printArray(arr, n);

    // Reverse the array
    reverseArray(arr, 0, n - 1);

    printf("Reversed array: ");
    printArray(arr, n);

    return 0;
}
Output:
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
  • Time Complexity: O(n)
  • Space Complexity: O(1)

C Program to Find the Smallest element in an Array.

Problem Statement: Given an array of integers, write a C program to find the smallest element in the array. 

Example:

Input: arr[] = {12, 45, 67, 32, 90, 3}
Output: Smallest element: 3

Input: arr[] = {11, 15, 17, 22, 9, 10}
Output: Smallest element: 9

Algorithm to Find Smallest Element in an Array.

Below are the steps that we need to follow:

Step 1: Initialize a variable min to store the smallest element and set it to the first element of the array.
Step 2: Input the size of the array and create an array arr of that size.
Step 3: Input the elements of the array arr.
Step 4: Traverse through the array from the second element to the last element.
  • If the current element is smaller than min, update the value of min to the current element.
Step 5: Print the value of min.

Program to Find the Smallest Element from Given Array.

//C program to find smallest element of the array
#include <stdio.h>

int main() {
    int n, min;

    printf("Enter the size of the array: ");
    scanf("%d", &n);

    int arr[n]; 

    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Initialize 'min' to the first element of the array.
    min = arr[0];

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

    printf("Smallest element: %d\n", min);

    return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
12 3 9 10 22
Smallest element: 3

Time Complexity: The time complexity of this code is O(n) since we need to traverse through the entire array once to find the smallest element.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to find the minimum element.

Related articles:

C Program to Find the Largest element in an array.

Problem Statement: Given an array of integers, write a C program to find the largest element in the array.

Example:

Input: arr[] = {12, 45, 67, 32, 90, 3}
Output: Largest element: 90

Input: arr[] = {10, 9, 12, -6, 11}
Output: Largest element: 11

Algorithm to Find Largest Element from Array.

Below are the steps that we need to follow:

Step 1: Initialize a variable max to store the largest element and set it to the first element of the array.
Step 2: Input the size of the array and create an array arr of that size.
Step 3: Take the elements of the array arr from the user.
Step 4: Traverse through the array from the second element to the last element.
  • If the current element is greater than max, update the value of max to the current element.
Step 5: Print the value of max.

Program to Find the Largest Element of the Array.

//C program to find largest element of the array
#include <stdio.h>

int main() {
    int n, max;

    printf("Enter the size of the array: ");
    scanf("%d", &n);
    // Creating an array of size 'n'.
    int arr[n]; 

    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Initialize 'max' to the first element of the array.
    max = arr[0];

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

    printf("Largest element: %d\n", max);

    return 0;
}
Output:
Enter the size of the array: 6
Enter the elements of the array:
11 23 10 31 9 12
Largest element: 31

Time Complexity: The time complexity of this code is O(n) since we need to traverse through the entire array once to find the largest element.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to find the smallest elements of the input array.

Similar articles:

C Program to Find Sum of Array Elements.

Problem Statement: Given an array of integers, write a C program to find the sum of all elements in the array.


Example:

Input: arr[] = {1, 2, 3, 4, 5}
Output: Sum of elements: 15

Algorithm to find the Sum of Array Elements.

Below are the steps that we need to follow:

Step 1: Initialize a variable sum to 0 to store the sum of elements.
Step 2: Input the size of the array and create an array arr of that size.
Step 3: Input the elements of the array arr.
Step 4: Traverse through the array from the first element to the last element.
  • Add each element to the sum variable.
Step 5: Print the value of the sum.

Program to Find Sum of Array Elements.

//C program to find sum of array elements
#include <stdio.h>

int main() {
    int n, sum = 0;

    printf("Enter the size of the array: ");
    scanf("%d", &n);
    
    // Creating an array of size 'n'.
    int arr[n]; 

    // Input the elements of the array 'arr'.
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // calculate the sum of elements.
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    printf("Sum of elements: %d\n", sum);

    return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
12 10 5 2 9
Sum of elements: 38

Time Complexity: The time complexity of this code is O(n) since we need to traverse through the entire array once to calculate the sum.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to solve this problem except for storing the elements of the input array.

Similar articles:

DON'T MISS

Tech News
© all rights reserved
made with by AlgoLesson