Friday, March 17, 2023

C Programming. Playing with array.

 Playing with array.

// Program to perform following actions on an array : -
/*
    (a) Print the even valued elements.
    (b) Print the odd valued elements.
    (c) Calculate and print the sum and average of the elements of array.
    (d) Print the maximum and minimum element of array.
    (e) Remove the duplicate from the array.
    (a) Print the array in reverse order.
*/

#include <stdio.h>

int main()
{
    int n, i, j;
    int sum = 0;
    float avg;
    int max, min;
    printf("Enter the size of the array : ");
    scanf("%d", &n);
    int arr[n], arr1[n], revarr[n];
    printf("Enter the elements of the array : \n");
    for (i = 0; i < n; i++)
    {
        printf("arr[%d] = ", i);
        scanf("%d", &arr[i]);
    }

    // For even numbers...
    printf("\n(a) The even numbers are : ");
    for (i = 0; i < n; i++)
    {
        if (arr[i] != 0){
            if (arr[i] % 2 == 0)
            {
                printf("%d ", arr[i]);
            }
        }
    }

    // For odd numbers...
    printf("\n(b) The odd numbers are : ");
    for (i = 0; i < n; i++)
    {
        if (arr[i] % 2 != 0)
        {
            printf("%d ", arr[i]);
        }
    }

    // For sum and average...
    for (i = 0; i < n; i++)
    {
        sum += arr[i];
    }
    avg = (float)sum / n;
    printf("\n(c) The sum of all elements is : %d\n    Average is : %0.2f ", sum, avg);

    // For maximum and minimum...
    max = min = arr[0];
    for (i = 0; i < n; i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
        }
        if (arr[i] < min)
        {
            min = arr[i];
        }
    }
    printf("\n(d) The maximum number is : %d\n    The minimum number is : %d",max, min);

    // For removing duplicate elements...
    for (i=0; i<n; i++){
        arr1[i]=arr[i];
    }

    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr1[i] == arr1[j])
            {
                arr1[j] = -1;
            }
        }
    }
   
    printf("\n(e) The array after removing duplicate element is : ");
    for (i = 0; i < n; i++)
    {
        if (arr1[i] != -1)
        {
            printf("%d ", arr1[i]);
        }
    }

    // For reverse order...
    for (i=0; i<n; i++){
        revarr[i]=arr[n-i-1];
    }
    printf("\n(f) The reverse of the array is : ");
    for (i=0; i<n; i++){
        printf("%d ", revarr[i]);
    }
    return 0;
}

Output :

Enter the elements of the array : arr[0] = 2 arr[1] = 4 arr[2] = 7 arr[3] = 2 arr[4] = 9 (a) The even numbers are : 2 4 2 (b) The odd numbers are : 7 9 (c) The sum of all elements is : 24 Average is : 4.80 (d) The maximum number is : 9 The minimum number is : 2 (e) The array after removing duplicate element is : 2 4 7 9 (f) The reverse of the array is : 9 2 7 4 2

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...