Answer to Question #180658 in C for jhon

Question #180658

Write a program using two dimensional arrays that determines the highest and lowest of the n numbers inputted.


1
Expert's answer
2021-04-12T16:04:02-0400
#include <stdio.h>
 
// C function to find maximum in arr[] of size n

int largest(int arr[], int n)

{

    int i;


    

    // Initialize maximum element


    int max = arr[0];

 

    // Traverse array elements from second and


    // compare every element with current max  


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


        if (arr[i] > max)


            max = arr[i];

 

    return max;

}
 
int smallest(int arr[], int n)
{
  int i;
  int min = arr[0];
  for (i = 1; i < n; i++)


        if (arr[i] < min)


            min = arr[i];

 

    return min;

}
int main()

{

    int n;
    printf("Enter the number of elements in the array ");
    scanf("%d",&n);

    int arr[n];
    for(int i=0;i<n;i++)
    {
      scanf("%d",&arr[i]);
    }
    printf("Largest in given array is %d", largest(arr, n));
    printf("Smallest in given array is %d", smallest(arr,n));


    return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS