This is a C++ program that finds the smallest element in an array of integers. The program first prompts the user to enter the size of the array, and then initializes an integer array of that size, called arr. It then prompts the user to enter the elements of the array one by one, which are stored in arr.
Next, the program calls a function called smallestelement, which takes in the array arr and its size n as parameters. Inside the function, the program initializes a variable temp to the first element of the array. It then loops through the remaining elements of the array using a for loop and compares each element with temp. If the element is smaller than temp, the program updates the value of temp to that element.
Finally, the function returns the value of temp, which is then outputted by the main function.
#include <iostream> using namespace std; int smallestelement(int arr[],int n) { int temp=arr[0]; for(int i=0;i<n;i++) { if(temp>arr[i]) { temp=arr[i]; } } return temp; } int main() { int n; cout<<"Enter the size of array: "; cin>>n; int arr[n-1]; cout<<"Enter array elements: "; for(int i=0; i<n; i++) { cin>>arr[i]; } int smallest = smallestelement(arr,n); cout<<"Smallest Element is: "<<smallest; return 0; }To download raw file Click Here
Enter the size of array: 5 Enter array elements: 11 12 13 14 15 Smallest Element is: 1
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions