In: Computer Science
c++
Write a program that print stars, Max and Min values. It should use the following functions:
Sample runs :
(Bold underlined numbers below are sample user inputs. Your program should display the exact output given the same user inputs)
Please enter a number : -99 Exiting the program.... // restart the program here and the output is as follows: Please enter a number : 1 * Please enter a number : 2 ** Please enter a number : 3 *** Please enter a number : -99 Min = 1, Max = 3 Exiting the program.... // restart the program here and the output is as follows: Please enter a number : -1 Invalid Please enter a number : 10 ********** Please enter a number : -33 Invalid Please enter a number : 5 ***** Please enter a number : -99 Min = -33, Max = 10 Exiting the program....
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<iostream>
using namespace std;
int getNum(){
int num;
do{
cout<<"Please enter a number:
";
cin >> num;
}while(num<-100);
return num;
}
void printStars ( int n ){
if(n<0) cout<<"Invalid\n";
else{
for(int
i=1;i<=n;i++)cout<<"*";
}
cout<<endl;
}
int main(){
int n;
int max, min;
bool first = true;
while(true){
n = getNum();
if(n==-99)break;
printStars(n);
if(first){
max = min =
n;
first =
false;
}
else if(max<n)max= n;
else if(min>n)min=n;
}
cout<<endl;
if(!first)
cout<<"Min = "<<min<<", Max =
"<<max<<endl;
cout<<"Exiting the program...";
return 0;
}