Question

In: Computer Science

Write a function posnum that prompts the user to enter a positive number and loops to...

Write a function posnum that prompts the user to enter a positive number and loops to error-check. It returns the square root of the positive number entered by the user. It calls a subfunction in the loop to print an error message. The subfunction has a persistent variable to count the number of times an error has occurred.

Here is an example of calling the function:

>> squarerootvalue = posnum

Enter a positive number: -5

Error #1: Follow instructions!

Does -5.00 look like a positive number to you?

Enter a positive number: -33

Error #2: Follow instructions!

Does -33.00 look like a positive number to you?

Enter a positive number: 4

squarerootvalue = 2

Solutions

Expert Solution

i used c++ langauge.Here is the following code:

============================================

#include <iostream>
#include <cmath> // included to use the sqrt function

using namespace std;

//defining the two functions for finding square root and printing message
void posnum();
void printError(int x);

//persistant variable used to print the error number
static int errorNum=0;

//main function
int main() {
posnum();
}

//definition of functions
void posnum()
{
int num,Squarerootvalue;
cout<<"\nEnter a positive number: ";
cin>>num;
  
if(num>0)
{
Squarerootvalue=sqrt(num);
cout<<"Squarerootvalue =" <<Squarerootvalue<<endl;
}
else
{
printError(num);
posnum();
}

}

void printError(int x)
{
errorNum++;
cout<<"Error #"<<errorNum<<": Follow instructions!"<<endl;
cout<<"Does "<<x<<" look like a positive number to you?";
  
}

please do upvote and comment for doubts.

Looking forward to help you :-)


Related Solutions

In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”. Example1: Input1: my dog and myself are going to my friend Input2: my Output: 0 11 31 Example 2: Input1: Programming is fun Input 2: my Output: not found
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT