#include // Suppose you have a rational number // // num/den // // and you would like to print it in scientific notation // to a specified number of significant digits. // // You cannot simply convert the value to a floating-point // number, because the requested number of significant // digits may exceed the precision of a double. // // Author a program that prints the requested number of // significant digits using only integer arithmetic. // // You do not have to round the result. Simply discard // any remaining digits. // // Examples: // // To 20 significant digits: // // 5313/321 = 1.6551401869158878504 x 10^1 // 531390823/321 = 1.6554231246105919003 x 10^6 // 12/1351345 = 8.8800417361961601219 x 10^-6 // // To 1 significant digit: // // 5313/321 = 1. x 10^1 // 531390823/321 = 1. x 10^6 // 12/1351345 = 8. x 10^-6 // // Terminology: // // In // // 5.3234 x 10^-3, // // 5.3234 is the significand, // 5 is the leading digit, and // -3 is the exponent. // // The digits in the significand are the significant // digits. // // For simplicity, if the numerator is zero, print // // 0. x 10^0 // // If the denominator is zero, return 0. // // If the requested number of significant digits is not // positive, also return 0. // Function declarations int main(); // Function definitions int main() { int num{}; std::cout << "Enter the numerator: "; std::cin >> num; int den{}; std::cout << "Enter the denominator: "; std::cin >> den; int digits{}; std::cout << "Enter the number of significant digits: "; std::cin >> digits; if ( den == 0 ) { return 0; } if ( digits < 1 ) { return 0; } if ( num == 0 ) { std::cout << "0. x 10^0" << std::endl; return 0; } // How will you do this? return 0; } // First, determine the sign. // // If the numerator and denominator have the same sign, // the rational number is positive. // // If they have different signs, print a '-'. // // After dealing with the sign, make both the numerator // and denominator positive so that the remainder of the // algorithm has to deal only with positive integers. // Now that both 'num' and 'den' are positive, we must // find the exponent. // // The exponent is the integer 'e' such that // // num // 10 > ------- >= 1, // den // // after the appropriate power of ten has been applied. // // Equivalently, we want to express // // num/den // // in the form // // e // significand x 10 // // where // // 1 <= significand < 10. // // Write a function that finds this exponent: // // int find_exponent( int num, int den ); // Once we have found the exponent, we must scale the // numerator or denominator so that // // num // 10 > ----- >= 1. // den // // For example: // // 5/67 -> 500/67 // 2/3 -> 20/3 // 5/3 -> 5/3 // 17/3 -> 17/30 // 1732/18 -> 1732/180 // 1802/18 -> 1802/1800 // // This requires multiplying either the numerator or // denominator by an appropriate power of 10. // // Write a function that calculates 10^e: // // int pow10( int e ); // // This function needs to work only when e >= 0. If // e < 0, return 0. // // If the exponent is positive, multiply the denominator // by 10^e. // // If the exponent is negative, multiply the numerator // by 10^(-e). // Next, author a function that prints the significand, // given a positive numerator and denominator satisfying // // num // 10 > ----- >= 1, // den // // together with the number of significant digits to // print: // // void print_significand( // int num, // int den, // int digits // ); // // We proceed using long division: // // num num - digit*den // ----- = digit + -----------------, // den den // // where // // digit = num/den. // // For the first digit, 'digit' is guaranteed to be // between 1 and 9. // // After printing the digit, update the numerator: // // num = num - digit*den; // // and multiply it by 10 to calculate the next digit. // Here is my solution: /* // Function declarations int main(); int find_exponent( int num, int den ); int pow10( int exponent ); void print_significand( int num, int den, int digits ); // Function definitions int main() { // Get the numerator. int num{}; std::cout << "Enter the numerator: "; std::cin >> num; // Get the denominator. // The denominator cannot be zero. int den{}; std::cout << "Enter the denominator: "; std::cin >> den; if ( den == 0 ) { return 0; } // Get the number of significant digits to print. // This must be a positive integer. int digits{}; std::cout << "Enter the number of significant digits: "; std::cin >> digits; if ( digits < 1 ) { return 0; } // Deal with the special case where the numerator // is zero. if ( num == 0 ) { std::cout << "0. x 10^0" << std::endl; return 0; } // At this point, both 'num' and 'den' are non-zero. // // Print a '-' if the rational number is negative. if ( (num < 0) != (den < 0) ) { std::cout << "-"; } // Make both 'num' and 'den' positive. if ( num < 0 ) { num = -num; } if ( den < 0 ) { den = -den; } // There was a significant amount of setup. Once // this is complete, however, the remainder of main() // consists primarily of calls to functions that each // solve one smaller problem. // Find the exponent. int exponent{ find_exponent( num, den ) }; // Scale the numerator or denominator so that // // 1 <= num/den < 10. if ( exponent > 0 ) { den *= pow10( exponent ); } else if ( exponent < 0 ) { num *= pow10( -exponent ); } // Print the requested number of significant digits. print_significand( num, den, digits ); // Print the exponent. std::cout << " x 10^" << exponent << std::endl; return 0; } // int find_exponent( ... ) // // @param num a positive numerator // @param den a positive denominator // // @returns the exponent 'e' required to write // num/den in scientific notation // // @algorithm // If num/den >= 10, repeatedly multiply the denominator // by 10 until // // 1 <= num/den < 10. // // Each multiplication increases the exponent by one. // // If num/den < 1, repeatedly multiply the numerator by // 10 until // // 1 <= num/den < 10. // // Each multiplication decreases the exponent by one. int find_exponent( int num, int den ) { int exponent{ 0 }; while ( num >= 10*den ) { ++exponent; den *= 10; } while ( num < den ) { --exponent; num *= 10; } return exponent; } // int pow10( ... ) // // @param exponent a non-negative integer // // @returns 10^exponent // - If exponent < 0, return 0. int pow10( int exponent ) { if ( exponent < 0 ) { return 0; } int result{ 1 }; for ( int k{ 1 }; k <= exponent; ++k ) { result *= 10; } return result; } // void print_significand( ... ) // // @param num a positive numerator // @param den a positive denominator // @param digits the number of significant digits // to print // // @algorithm // We assume // // 1 <= num/den < 10. // // Use integer division to find each successive digit, // and then use the remainder to calculate the next digit. void print_significand( int num, int den, int digits ) { int digit{ num/den }; std::cout << digit << "."; num = 10*(num - digit*den); for ( int k{ 2 }; k <= digits; ++k ) { digit = num/den; std::cout << digit; num = 10*(num - digit*den); } } */