Write a program using two dimensional arrays that determines the highest and lowest of the n numbers inputted.
#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;
}
Comments
Leave a comment