(Advanced) Maximum and Minimum numbers from an array using C language

 Problem: Find Maximum and Minimum numbers from an array using the C programming language.

Sample of an array to find out minimum and maximum numbers.

an array

Description

1. You have to take an input 'n', which declares the number of numbers in an array list.

2. You can't use any built function to find out the Minimum and Maximum number from an array.

3. You have to print all steps for the program until the program finds out the Minimum and Maximum numbers. That means you have to print all comparisons between the numbers of the array.


Solution:

Step 1: Take variables for the input and 'for' loop system.

int n, i;

Step 2: Input for the array size.

scanf("%d", &n);
int arr[n+1];

Step 3: Input for the array list using the 'for' loop.

for (i=1; i<=n; i++)
{
    scanf("%d", &arr[i]);
}

Step 4: Declare 'max' for maximum and 'min' for the minimum variable. Then, assign the first number from the array list in those variables.

int max, min;
max=arr[1];
min=arr[1];

array first elements

min max assign

Step 5: Write conditions for the maximum and minimum numbers.

        if (max<arr[i])
{
max=arr[i];
}
else if (min>arr[i])
{
min=arr[i];
}

Step 6: Print the comparison of each number.

        printf("Comparing %d with %d\n", min, arr[i]);
        if (max<arr[i])
{
max=arr[i];
}
else if (min>arr[i])
{
min=arr[i];
}

Step 7: Print out when the program finds the Minimum and Maximum numbers in the loop.

      for (i=1; i<=n; i++)

    {
printf("Comparing %d with %d\n", min, arr[i]);
if (max<arr[i])
{
printf("(Maximum Number found) comparing %d with %d\n", min, arr[i]);
max=arr[i];
}
else if (min>arr[i])
{
printf("(Minimum Number found) comparing %d with %d\n", min, arr[i]);
min=arr[i];
}
}

Step 8: Finally, print the results.

printf("Maximum Number is: %d\n", max);
printf("Minimum Number is: %d\n", min);

Code for  Maximum and Minimum numbers from an array using the C programming language.

#include<stdio.h>
int main()
{
int n, i;
scanf("%d", &n);
int arr[n+1];
for (i=1; i<=n; i++)
{
scanf("%d", &arr[i]);
}
printf("Printing the array: ");
for (i=1; i<=n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
int max, min;
max=arr[1];
min=arr[1];
for (i=1; i<=n; i++)
{
printf("Comparing %d with %d\n", min, arr[i]);
if (max<arr[i])
{
printf("(Maximum Number found) comparing %d with %d\n", min, arr[i]);
max=arr[i];
}
else if (min>arr[i])
{
printf("(Minimum Number found) comparing %d with %d\n", min, arr[i]);
min=arr[i];
}
}
printf("Maximum Number is: %d\n", max);
printf("Minimum Number is: %d\n", min);
return 0;
}

Sample Input:
12
40 30 11 59 70 95 45 69 99 25 88 60

Sample Output:
Printing the array: 40 30 11 59 70 95 45 69 99 25 88 60
Comparing 40 with 40
Comparing 40 with 30
(Minimum Number found) comparing 40 with 30
Comparing 30 with 11
(Minimum Number found) comparing 30 with 11
Comparing 11 with 59
(Maximum Number found) comparing 11 with 59
Comparing 11 with 70
(Maximum Number found) comparing 11 with 70
Comparing 11 with 95
(Maximum Number found) comparing 11 with 95
Comparing 11 with 45
Comparing 11 with 69
Comparing 11 with 99
(Maximum Number found) comparing 11 with 99
Comparing 11 with 25
Comparing 11 with 88
Comparing 11 with 60
Maximum Number is: 99
Minimum Number is: 11

Pictorial details to find the maximum number

Pictorial details to find the maximum number


Pictorial details to find the minimum number

Pictorial details to find the minimum number


Min-Max related problem on Codeforces

Problem Name: "Plus One on the Subset#763 (Div.3) A

Codeforces Round #764 (Div. 3)

This problem and codes for finding the minimum and maximum from an array are the same. we have to just print the result as " result= max-min" for this code forces problem. And, also we have to add case input to test multiple cases for this problem. For this, take input for the test case and run a while loop until the test case number becomes zero.

scanf("%d", &t);
while(t--)
{}

Solution for Codeforces Round #764 (Div. 3) A "Plus One on the Subset"


#include<stdio.h>
int main()
{
int n, i, t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
int arr[n+1];
for (i=1; i<=n; i++)
{
scanf("%d", &arr[i]);
}
int max, min;
max=arr[1];
min=arr[1];
for (i=1; i<=n; i++)
{
if (max<arr[i])
{
max=arr[i];
}
else if (min>arr[i])
{
min=arr[i];
}
}
printf("%d\n", max-min);
}
return 0;
}

Post a Comment

Previous Post Next Post