ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » C programs » C One-D Array programs

C program to find Smallest and Largest elements from One Dimensional Array Elements

This program will read 10 elements and find the smallest and largest elements from inputted One Dimensional Array Elements.

To find Smallest element from the Array - We will assume that first element is smallest and assign it in a variable and then compare each element from array with the variable, if any of the element is smaller than variable assign that element into variable, finally we will get smallest elements.

To find Largest element from the Array - We will assume that first element is largest and assign it in a variable and then compare each element from array with the variable, if any of the element is larger than variable assign that element into variable, finally we will get largest elements.

Find Smallest and Largest elements from One Dimensional Array using C program

/*Program to find largest & smallest element among all elements.*/ 

#include <stdio.h>

/** funtion	:	readArray() 
    input	:	arr ( array of integer ), size 
    to read ONE-D integer array from standard input device (keyboard). 
**/ 
void readArray(int arr[], int size) 
{ 
	int i =0; 

	printf("\nEnter elements : \n"); 

	for(i=0; i < size; i++) 
	{ 
		printf("Enter arr[%d] : ",i); 
		scanf("%d",&arr[i]); 
	} 
} 

/** funtion	:	getLargest() 
    input	:	arr ( array of integer ), size 
    to get largest element of array. 
**/ 
int getLargest(int arr[], int size) 
{ 
	int i=0,largest=0; 

	largest = arr[0]; 

	for(i=1;i < size; i++) 
	{ 
		if( arr[i] > largest ) 
			largest = arr[i]; 
	} 

	return largest; 
} 

/** funtion	:	getSmallest() 
    input	:	arr ( array of integer ), size 
    to get smallest element of array. 
**/ 
int getSmallest(int arr[], int size) 
{ 
	int i=0,smallest=0; 

	smallest = arr[0]; 

	for(i=1;i < size; i++) 
	{ 
		if( arr[i] < smallest ) 
			smallest = arr[i]; 
	} 

	return smallest; 
} 

int main() 
{ 
	int arr[10]; 

	readArray(arr,10); 
	 
	printf("\nLargest  element of array is     : %d"  , getLargest    (arr,10)	); 
	printf("\nSmallest element of array is     : %d\n", getSmallest   (arr,10)	); 

	return 0; 
} 
    

Output

    Enter elements : 
    Enter arr[0] : 11 
    Enter arr[1] : 22 
    Enter arr[2] : 33 
    Enter arr[3] : 44 
    Enter arr[4] : 55 
    Enter arr[5] : 66 
    Enter arr[6] : 77 
    Enter arr[7] : 66 
    Enter arr[8] : 56 
    Enter arr[9] : 56 

    Largest  element of array is     : 77 
    Smallest element of array is     : 11 
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.