Ques:-2) program-to-find-the-largest-and-smallest-element-of-an-array.

				
					#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10],i,n,sm,la;
 clrscr();
 printf("how many elements you want to enter");
 scanf("%d",&n);
 printf("\nEnter %d elements into array\n",n);
 for(i=1;i<=n;i++)
  scanf("%d",&a[i]);
 printf("\nElements into array are:\n");
 for(i=1;i<=n;i++)
 {
  printf("%d",a[i]);
  sm=la=a[i];
 }
 for(i=1;i<=n;i++)
 {
  if(a[i]<sm)
   sm=a[i];
  if(a[i]>la)
   la=a[i];
 }
 printf("\nLargest element is%d\n",la);
 printf("\nSmallest element is%d\n",sm);
 getch();
}

				
			

Out Put

Example 2 Here's a program in C to find the largest and smallest elements of an array:

				
					#include <stdio.h>

void findMinMax(int arr[], int size, int *min, int *max) {
    // Initialize min and max with the first element of the array
    *min = *max = arr[0];

    // Iterate through the array to find the minimum and maximum elements
    for (int i = 1; i < size; i++) {
        if (arr[i] < *min) {
            *min = arr[i];
        }
        if (arr[i] > *max) {
            *max = arr[i];
        }
    }
}

int main() {
    int arr[] = {4, 7, 2, 9, 1, 5, 8};
    int size = sizeof(arr) / sizeof(arr[0]);
    int min, max;

    findMinMax(arr, size, &min, &max);

    printf("The smallest element in the array is: %d\n", min);
    printf("The largest element in the array is: %d\n", max);

    return 0;
}

				
			

In this program, the findMinMax() function takes the integer array, its size, and pointers to variables representing the minimum and maximum values. It iterates through the array elements, updating the minimum and maximum values accordingly. The main() function demonstrates the usage of findMinMax() by passing the array and printing the smallest and largest elements found.

Scroll to Top