Question

In: Computer Science

c++ Write a program that print stars, Max and Min values. It should use the following...

c++

Write a program that print stars, Max and Min values. It should use the following functions:

  1. (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100.
  2. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message instead.
  3. (5 pts) main ( ) should call getNum ( ) function each time to read the user input. Pass each user input into printStars ( ) function to print stars. This function will loop to process series of integers.
    • (2 pts) When the user input is -99, that's a signal to terminate the loop (end of the user inputs (sentinel value)). Remember, sentinel value should not be processed as part of user inputs.
    • (2 pts) main ( ) should also display overall Min and Max values of user inputs.
  4. (1 pt) Write the function prototypes for getNum ( ) and printStars ( ) 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....

Solutions

Expert Solution

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;
}


Related Solutions

Write a Python program that computes certain values such as sum, product, max, min and average...
Write a Python program that computes certain values such as sum, product, max, min and average of any 5 given numbers along with the following requirements. Define a function that takes 5 numbers, calculates and returns the sum of the numbers. Define a function that takes 5 numbers, calculates and returns the product of the numbers. Define a function that takes 5 numbers, calculates and returns the average of the numbers. Must use the function you defined earlier to find...
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Write a C++ program for the following problem: Calculate and print the area and volume of...
Write a C++ program for the following problem: Calculate and print the area and volume of a cone inside a While  loop that goes from 1 to 20 with a step of .5. (the step is 1/2 or Point 5, so you go 10, 10.5,11, 11.5) Note: Your loop variable will need to be a double data type Use two decimal places on all numbers that are double data type. This will be a table with 3 columns. Don't worry about...
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of...
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of columns. Also, create an addition column that is based on the other columns. There will be no error checking required for this lab. You are provided with a data file named (surprise!) data.txt. Data is arranged in columns where each item of data is twelve columns wide (so it is easy to extract data items using slicing): Name Height(m) Weight(kg) Joe 1.82 72.57 Mary...
Method calling in c# I need to write methods for calling the min and max numbers...
Method calling in c# I need to write methods for calling the min and max numbers using this code; Console.WriteLine("Calling highest method."); Console.WriteLine("Highest number is: {0}", highest(3)); Console.WriteLine("Calling lowest method."); Console.WriteLine("Lowest number is: {0}", lowest(3));
Write a perl program that replicates the actions of a pez dispenser. The program should print...
Write a perl program that replicates the actions of a pez dispenser. The program should print out the contents of the dispenser showing that it is empty, prompt a user for 10 pez candy flavors/colors, print out the contents of the dispenser showing that it is full, dispense the candies one at a time, and then print out the contents of the dispenser showing that it is empty. The dispenser should only take in 10 candies and should load and...
In C++ Instructions: Use a void function to print the following message (should be in welcome...
In C++ Instructions: Use a void function to print the following message (should be in welcome function) Welcome to the Event Scheduling program create 3 int arrays with 3 positions (one array for days one array for moths and one array for years) (should be in main) Create a file that contains the following (you can just create the file or write the file in the program) 1 / 26 / 2021 12 / 13 / 2020 2 / 1...
Instructions (in C++): 1 ) Use a void function to print the following message (should be...
Instructions (in C++): 1 ) Use a void function to print the following message (should be in welcome function) Welcome to the Event Scheduling program 2 ) create 3 int arrays with 3 positions (one array for days one array for moths and one array for years) (should be in main) 3 ) Create a file that contains the following (you can just create the file or write the file in the program) 1 / 26 / 2021 12 /...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT