Write a C++ program that implements the algorithm to find the smallest number in
ID: 3561881 • Letter: W
Question
Write a C++ program that implements the algorithm to find the smallest number in a list of numbers. The program should continue to read numbers from the keyboard until a value of 0 (zero) is entered. It should then print the smallest number found and exit.
3.Extend the program you wrote in #2 above so that it continuously displays the current smallest number, instead of only at the end. An example program execution is shown below:
Please enter a number (0 to quit): 3
Current smallest number is 3
Currently have this code that finds the smallest number in the list but have no idea on how to keep it going. The current code I have is:
int main(void)
{
int a[8] = {4,5,10,23,2,1,0,2};
list<int> l(a, a+8);
cout << "Smallest number: " << *min_element(l.begin(), l.end()) << ' ';
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n=2,min;
printf("Please enter a number (0 to quit): ");
scanf("%d",n);
if(n==0)
exit(1);
min = n;
while(1)
{
printf("Please enter a number (0 to quit): ");
scanf("%d",n);
if(n==0)
break;
if(n<min)
min = n;
}
printf("Smallest number is %d",min);
return 0;
}
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n=2,min;
printf("Please enter a number (0 to quit): ");
scanf("%d",n);
if(n==0)
exit(1);
min = n;
printf("Current smallest number is %d",min);
while(1)
{
printf("Please enter a number (0 to quit): ");
scanf("%d",n);
if(n==0)
break;
if(n<min)
min = n;
printf("Current smallest number is %d",min);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.