Question: Write a program that tests integers to see if they are rising numbers. A rising number is one in which all digits are in nondecreasing
Write a program that tests integers to see if they are rising numbers. A rising number is one in which all digits are in nondecreasing order. By this, I mean that the leftmost digit may be anything and all other digits must be equal to or larger than the digit immediately to their left. For example, 25569 is a rising number and 314 is not a rising number (1 is not equal to or larger than 3). The program should allow testing as many values as the user wishes. Also, for each input value (whether rising or not), the program should output the value of the next-larger rising number (the smallest rising number that is greater than the users input). Your program should be designed using functions for key (hierarchical) ideas. Use my tool functions as appropriate. In addition to functions for the main control structure of your program (inputting doing again, etc.), the program should include and use the following two functions, which are program-specific math like functions they take a parameter and produce a single value. They do no console input/output:
One function should receive an integer parameter and return a Boolean value thatindicates whether or not the parameter is a rising number. Note: x%10 gives the value of the rightmost digit of x. x/10 gives all digits except the rightmost digit (thus, x = x/10) removes one digit from x). Using these in a loop allows you to look at each in a number. By remembering the previous digit each time around, you can check for the desired property.
The other function should receive an integer parameter and return an integer which is the next rising number after the parameter. To find the next rising number, use a loop that counts up from the parameter's value, testing each value (using the rising number test function described above), until a rising number is found. Do not construct the next rising number by gluing digits together.
tools namespace tools { int getInt(std::string prompt); std::string getLine(std::string prompt); std::string getString(std::string prompt); float getFloat(std::string prompt); int getBoundedInt(std::string prompt, int lowBound, int highBound); char getChar(std::string prompt); bool getBool(std::string prompt); bool isLowerCase(char c); bool isUpperCase(char c); bool isLetter(char c); char getLetter(std::string prompt); int width(int value); } { { string getLine(string prompt) { while (true) { string userAnswer; cout << prompt; getline(cin, userAnswer); if (!cin.fail()) return userAnswer; cin.clear(); cin.ignore(); // get rid of what killed it cout << "Input failed, try again" << endl; } } string getString(string prompt) { while (true) { string userAnswer; cout << prompt; cin >> userAnswer; cin.ignore(999, ' '); if (!cin.fail()) return userAnswer; cin.clear(); cin.ignore(); cout << "Input failed, try again" << endl; } } float getFloat(string prompt) { while (true) { float userAnswer; cout << prompt; cin >> userAnswer; cin.ignore(999, ' '); if (!cin.fail()) return userAnswer; cin.clear(); cin.ignore(); cout << "Input failed, try again" << endl; } } int getBoundedInt(string prompt, int lowBound, int highBound) { while (true) { int userAnswer = getInt(prompt); if (lowBound <= userAnswer && userAnswer <= highBound) return userAnswer; cout << "Input must be in the range " << lowBound << " to " << highBound << endl; } } int getInt(string prompt) { while (true) { int userAnswer; cout << prompt; cin >> userAnswer; cin.ignore(999, ' '); if (!cin.fail()) return userAnswer; cin.clear(); cin.ignore(); cout << "Input failed, try again" << endl; } } char getChar(string prompt) { while (true) { char userAnswer; cout << prompt; cin >> userAnswer; cin.ignore(999, ' '); if (!cin.fail()) return userAnswer; cin.clear(); cin.ignore(); cout << "Input failed, try again" << endl; } } bool getBool(string prompt) { while (true) { switch (getChar(prompt)) { case 'Y': case 'y': return true; case 'N': case 'n': return false; } cout << "please enter y or n" << endl; } } bool isLowerCase(char c) { return 'a' <= c && c <= 'z'; } bool isUpperCase(char c) { return 'A' <= c && c <= 'Z'; } bool isLetter(char c) { return isLowerCase(c) || isUpperCase(c); } char getLetter(string prompt) { while (true) { char userAnswer = getChar(prompt); if (isLetter(userAnswer)) return userAnswer; cout << "Please enter a letter." << endl; } } int width(int value) { int spaceForSign; if (value >= 0) spaceForSign = 0; else { spaceForSign = 1; value = -value; } // count digits: accumulation int digitCount = 0; int digits = value; do { ++digitCount; // the digit I am about to remove // is digits % 10 digits = digits / 10; // remove a digit } while (value != 0); return digitCount + spaceForSign; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
