Question
I need help getting a C++ lab to work. Objectives Create user-defined functions Use user-defined functions Validate user input Create function prototypes Implement pseudocode Overview
I need help getting a C++ lab to work.
Objectives
Create user-defined functions
Use user-defined functions
Validate user input
Create function prototypes
Implement pseudocode
Overview
This assignment gets a start value and an end value from the user. Both values must be validated as integers within specified ranges. The program then displays a list of all the prime numbers between those two values.
General program requirements
Add documentation comments to the start of your program, including file name, course, your name, the date, and a brief description of the purpose of the program.
Include the needed libraries: cmath, iostream, sstream, and string
Create prototypes for the two functions that you will implement after main (getInt and isPrime).
Pseudocode for main function
// Input: Either nothing, or "int argc, char* argv[]" (which represent // the command line arguments) // Output: Returns an integer representing the exit status (0 for success). // Side effects: Gets two integers from the user representing a range // and displays the prime numbers in that range. Use getInt to display a prompt ("Enter starting integer (1 - 100): ") and get an integer from the user within the range 1 through 100. Store the result. Use a stringstream to create a string prompt for the second user input. Example: stringstream s; s << "Enter ending integer (" << (min+1) << " - 1000): "; Then use s.str() wherever you need to turn it into a regular C++ string object Use getInt to display a prompt for the ending value in the range. Use the custom prompt you created in the last step. The range for this value should be from 1 greater than the previous number the user typed in through 1000. Store the result. Display a line stating what will be displayed next (including the first and last values in the range). For each value in the range specified Call isPrime with that value to determine if the value is prime If the value is prime, then display it followed by a newline. Return a success (0) exit status.
Pseudocode for getInt function
// Input: a string prompt, an integer minimum value, and an integer maximum value. // Output: Returns an integer the user entered. // Side effects: Displays the prompt, gets input from the user, and may display error // messages and reprompt the user as needed. Create a Boolean value to tell you if you have a valid value to return. Initialize it to false. Create an integer variable to store the user input. While you do not have a valid value to return do the following: Prompt the user to enter a value. Read an integer value in from the user. If that read did not cause an error then: If the value is less than the minimum, display an appropriate error message. If the value is greater than the maximum, display an appropriate error message. If the value is within range, set the Boolean value to indicate we have valid input. Otherwise Display an error message that an invalid integer has been entered Clear cin's error flags Clear the input buffer. Return the user input.
Pseudocode for isPrime function
// Input: an unsigned integer value. // Output: true if the value sent in is prime, and false if it is not prime. // Side effects: None. Let result be a Boolean variable Let limit be the integer (truncated) square root of the value sent in If the number sent in is 0 or 1, set result to false Otherwise, if the number sent in is 2, set result to true Otherwise, if the number sent in is divisible by 2, set result to false Otherwise (none of the above conditions has been met) Assume the number sent in is prime (result is true) Loop from 3 through limit, incrementing by 2 Check to see if the value sent in is divisible by the loop counter If it is, then Set result to false End the loop and return. Return the result.
Sample Run
Enter starting integer (1 - 100): SpongeBob SquarePants Error: Invalid integer Enter starting integer (1 - 100): -4 Error: Value below minimum of 1 Enter starting integer (1 - 100): 700 Error: Value above maximum of 100 Enter starting integer (1 - 100): 1 Enter ending integer (2 - 1000): 1 Error: Value below minimum of 2 Enter ending integer (2 - 1000): Patrick Star Error: Invalid integer Enter ending integer (2 - 1000): 123 Primes between 1 and 123 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started