Program to find largest and smallest element of an array

Source Code

// 41. Program to find largest and smallest element of an array
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
//#include <conio.h>
#include <malloc.h>

void main () {
	int *ar, min, max, i, n;
	//system("cls");
	//clrscr();

	printf("\t\t..:: calculate largest and smallest element of an array ::..\n\n");
	printf("Please enter the value of N: ");
	scanf("%d", &n);
	ar = (int *) malloc(n * sizeof(int));
	
	for (i=0; i<n; i++) {
		printf("\nPlease enter number %d: ", i+1);
		scanf("%d", &ar[i]);
	}
	
	min = max = ar[0];
	
	for (i=0; i<n; i++) {
		if (ar[i] > max) max = ar[i];
		if (ar[i] < min) min = ar[i];
	}
	
	printf("\n\tMax: %d\n\tMin: %d", max, min);
	//getch();
}

Output

	..:: calculate largest and smallest element of an array ::..

Please enter the value of N: 5

Please enter number 1: 12

Please enter number 2: 25

Please enter number 3: 45

Please enter number 4: 3

Please enter number 5: 57

	Max: 57
	Min: 3

Leave a Reply

Note: Your email address will not be published. Required fields are marked *