(C++) 5.15 LAB: Two smallest numbers with arrays
Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow.
Ex: If the input is:
5 10 5 3 21 2
the output is:
2 3
You can assume that the list of integers will have at least 2 values.
To achieve the above, first read the integers into a array .
Hint: Make sure to initialize the second smallest and smallest integers properly.
#include <bits/stdc++.h>
using namespace std;
int main() {
cout<<"Give Input:"<<endl;
int n;
cin>>n;
int a[100];
cout<<"Give List of Integers:"<<endl;
int i;
for(i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
cout<<a[0]<<" "<<a[1]<<endl;
}
Get Answers For Free
Most questions answered within 1 hours.