Creating a C Program to Identify Largest and Smallest Numbers in Unsorted Arrays

08 Apr 2023 Balmiki Mandal 0 C Programming

C Program to Find Largest and Smallest Numbers in Unsorted Array

To design a C program function to print 10 numbers through the keyword into an array and find the biggest and smallest number in an unsorted array without using any sorting technique means to write a function in C programming language that performs the following tasks:

  1. Prompt the user to input 10 numbers through the keyword.
  2. Store the 10 numbers in an array.
  3. Find the largest and smallest number in the array without using any sorting technique.
  4. Print the largest and smallest number to the console.

The function should be designed to take an array of integers as input, perform the above tasks, and return the largest and smallest numbers found in the array as output. The function should not use any built-in sorting functions or techniques to find the largest and smallest numbers in the array. Instead, it should use iterative comparison to determine the maximum and minimum values in the array.

C Program to Find the Maximum and Minimum Number in an Unsorted Array of 10 Elements

#include<stdio.h>

void findMinMax(int arr[], int n)
{
    int i, min, max;
    min = max = arr[0];
    
    for (i = 1; i < n; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    printf("The smallest number in the array is: %d\n", min);
    printf("The largest number in the array is: %d\n", max);
}

int main()
{
    int arr[10], i;
    printf("Enter 10 numbers:\n");
    
    for (i = 0; i < 10; i++) {
        scanf("%d", &arr[i]);
    }
    findMinMax(arr, 10);
    return 0;
}

Explanation

The function findMinMax() takes two arguments, an array arr[] and its size n. In this function, we first initialize min and max with the first element of the array. Then we loop through the array and compare each element with min and max to find the smallest and largest numbers. Finally, we print the values of min and max.

In the main() function, we declare an integer array arr[] of size 10 and prompt the user to input 10 numbers through the keyword. We then call the findMinMax() function and pass the array and its size as arguments.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.