Q:

C program to find Smallest and Largest elements from One Dimensional Array Elements

0

C program to find Smallest and Largest elements from One Dimensional Array Elements

This program will read 10 elements and find the smallest and largest elements from inputted One Dimensional Array Elements.

To find Smallest element from the Array - We will assume that first element is smallest and assign it in a variable and then compare each element from array with the variable, if any of the element is smaller than variable assign that element into variable, finally we will get smallest elements.

To find Largest element from the Array - We will assume that first element is largest and assign it in a variable and then compare each element from array with the variable, if any of the element is larger than variable assign that element into variable, finally we will get largest elements.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Find Smallest and Largest elements from One Dimensional Array using C program

/*Program to find largest & smallest element among all elements.*/

#include <stdio.h>

/** funtion :   readArray() 
    input   :   arr ( array of integer ), size 
    to read ONE-D integer array from standard input device (keyboard). 
**/
void readArray(int arr[], int size)
{
    int i = 0;

    printf("\nEnter elements : \n");

    for (i = 0; i < size; i++) {
        printf("Enter arr[%d] : ", i);
        scanf("%d", &arr[i]);
    }
}

/** funtion :   getLargest() 
    input   :   arr ( array of integer ), size 
    to get largest element of array. 
**/
int getLargest(int arr[], int size)
{
    int i = 0, largest = 0;

    largest = arr[0];

    for (i = 1; i < size; i++) {
        if (arr[i] > largest)
            largest = arr[i];
    }

    return largest;
}

/** funtion :   getSmallest() 
    input   :   arr ( array of integer ), size 
    to get smallest element of array. 
**/
int getSmallest(int arr[], int size)
{
    int i = 0, smallest = 0;

    smallest = arr[0];

    for (i = 1; i < size; i++) {
        if (arr[i] < smallest)
            smallest = arr[i];
    }

    return smallest;
}

int main()
{
    int arr[10];

    readArray(arr, 10);

    printf("\nLargest  element of array is     : %d", getLargest(arr, 10));
    printf("\nSmallest element of array is     : %d\n", getSmallest(arr, 10));

    return 0;
}

Output:

    Enter elements : 
    Enter arr[0] : 11 
    Enter arr[1] : 22 
    Enter arr[2] : 33 
    Enter arr[3] : 44 
    Enter arr[4] : 55 
    Enter arr[5] : 66 
    Enter arr[6] : 77 
    Enter arr[7] : 66 
    Enter arr[8] : 56 
    Enter arr[9] : 56 

    Largest  element of array is     : 77 
    Smallest element of array is     : 11 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to replace all EVEN elements by 0 and Od... >>
<< C program to calculate sum, product of all One Dim...