2. Write a program that implements the algorithm to find the smallest number in
ID: 3561917 • Letter: 2
Question
2. Write a 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
Please enter a number (0 to quit): 5
Current smallest number is 3
Please enter a number (0 to quit): 1
Current smallest number is 1
Please enter a number (0 to quit): 0
The smallest number in your list was 1. Thank you. Goodbye.
Test your program by supplying positive and negative numbers. What happens if you enter 0 as the first number?
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter a number (0 to quit):";
cin>>n;
int sm = n;
while(n){
if( n<sm )sm = n;
cout<<"Please enter a number (0 to quit):";
cin>>n;
}
cout<<"The smallest number in your list was"<<sm;
return 0;
}
##for 2nd question
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter a number (0 to quit):";
cin>>n;
int sm = n;
while(n){
if( n<sm )sm = n;
cout<<"Current smallest number is"<<sm<<" ";
cout<<"Please enter a number (0 to quit):";
cin>>n;
}
cout<<"The smallest number in your list was"<<sm<<" Thank you. Goodbye. ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.