How can you get 400 from 4 and 2 without clinging to anything else?


Based on the following prototype, create the function mag (), which increases the order of the value of the variable num to the level specified by the order variable:

void mag (long &num, long order); 

For example, if the num variable is 4 and the order variable is 2, then after executing the mag () function, the num variable should become equal to 400. Write a demo program indicating that the function works.

  • one
    If I understand correctly, you need to multiply num by 10 until it lies in the range [10 ^ order, 10 ^ (order + 1)). - dzhioev
  • @zhioev, thanks. - mikle09
  • four
    Can simply multiply num by 10 ^ order ? - avp
  • ... well, or (which is the same), the order times multiplied by 10 with a positive order , or -order divided by 10 with a negative. (What to do when order == 0 remains as an exercise.) - VladD
  • 2
    > I can’t imagine how you can get 400 from 4 and 2 without clinging to anything else. Write between them e - Michael M

2 answers 2

I can offer this solution: If I understood the problem correctly.

For all occasions. If there is a negative order, or you need to throw exceptions or set the maximum value of the type when it is exceeded.

 //#define USE_EXCEPTION // выбрасывать исключение при превышении значения long #define NEGATIVE_ORDER // при отрицательном порядке уходит в минус, иначе значение 0 void mag(long &num, long order) { if(order > 0) { for(long i = 0; i < order; i++) { if(((long long)num * 10) > LONG_MAX){num=LONG_MAX; #ifdef USE_EXCEPTION throw(std::exception("Превышено максимальное значение LONG 2147483647")); #endif break;}else{num *= 10;} } } if(order < 0) { bool negative = false; for(long i = 0; i > order; i--) { #ifdef NEGATIVE_ORDER if(negative==true || (num / 10) <= 0) #else if(5<3) #endif { negative = true; if(((long long)num * 10) > LONG_MAX) { num=LONG_MAX; #ifdef USE_EXCEPTION throw(std::exception("Превышено минимальное значение LONG -2147483647")); #endif break; }else { num = num * 10; } } else { num = num / 10; } } if(negative) { num = -num; } } } int main() { std::system("chcp 1251"); try { {// long num = 214748364; long order = 1; mag(num,order); std::cout << "\nmag=" << num; } {// переполнение long num = 4; long order = 30; mag(num,order); std::cout << "\nmag макс=" << num; } { long num = 400; long order = -2; mag(num,order); std::cout << "\nmag отрицательный=" << num; } { long num = 400; long order = -40; mag(num,order); std::cout << "\nmag мин=" << num; } } catch(std::exception e) { std::cout << "\n\n#!#ИСКЛЮЧЕНИЕ#!#\a \n--" << e.what() << "--"; } std::cout << "\n"; std::system("pause"); return 0; } 
  • 1. And for negative order values? 2. It would be nice to detect overflow. - VladD
  • 3. Overflow in the sense of throwing an exception? Or set max value? And how to count in a negative order? order = -2 num = 4 ?? After the first division by 10, num = 0. In the minus does not want to go. code updated. - manking
  • 3: I would throw an exception, but how to distinguish the legal max. value from illegal? Yes, and error reporting is necessary so that they could not give up. 1: Yes, you are right, returning a whole type is not designed for division, so order < 0 must also be an error. - VladD
  • 3: here for an integer type, the exact LONG_MAX cannot be achieved. And you can compare if (num == LONG_MAX) {cout ("exceeded");} Only if the constant LONG_MAX itself is different. For example with such data: LONG_MAX: 2147483640 value num: 214748364 order: 1 - manking pm
  • 3: for my taste, the exception is still more correct. otherwise, the caller has a chance to forget to check the value. although this is already a matter of personal preference, of course. - VladD
 void mag(long &num,long order) { for (; order; order--) { num=num*10; } } int main(int argc, const char * argv[]) { long x=4; mag(x, 2); cout<<x; return 0; }