#include <iostream> #include <cstdlib> #include <cmath> using namespace std; int main() { int n, j = 0; cout << "Amount of numbers: "; cin >> n; const int length = n; int numbers[length]; if (length >= 1) { // Элементы с чётными индексами получают значение индекса, с нечётными - квадрата индекса while (j < length) { if (j % 2 == 0){ numbers[j] = j; } else { numbers[j] = pow(j, 2); } j++; } // Выводим элементы массива j = 0; while (j < length) { cout << numbers[j] << endl; j++; } } system("pause>nul"); return 0; } 

enter image description here

  • 2
    In an amicable way, it should not even compile - not in C ++ arrays with a length determined at runtime ... - Harry
  • @Harry so the length is set before execution, in the line int numbers [length]; - Homo Civicus
  • Yes? And what is it equal to? five? ten? 22? - Harry
  • one
    You already choose one thing. "The length is set before execution" or "the length is entered by the user". Because it is introduced at runtime ... By the way, if you do it normally, then everything works - ideone.com/f3GZbS And, by the way, write normally - j*j , and not these perversions, pow(j,2) - can also affect , check out ... - Harry
  • one
    CodeBlocks is a medium, not a compiler. The compiler is there gcc. The ability to declare arrays of run-time size is an extension of the gcc compiler. For C ++, this extension has nothing to do. In C ++ this is not allowed. - AnT pm

4 answers 4

The return type of the std::pow() function is either float , double , or long double , i.e. a floating point value is returned.

In your example, you pass two arguments of type int , and both of them inside the pow function are pow to double , and the return value is also double .

Then in your example, the value returned by the pow function and having the type double is double to the type int . In C ++, floating point types are cast to integer types by dropping the fractional part (the so-called rounding towards zero).

Since the process of exponentiation is carried out in floating point numbers, then you need to be prepared for the fact that the result is not entirely accurate . For example, if the exact result of some calculation is 25 , then in the general case the resulting value may turn out to be, for example, 24.999999999999996 . Casting this value to type int drop the fractional part and the result will be 24 .

I was unable to repeat the output of your program using the std::pow() function from the <cmath> header file (probably because I used Visual Studio). I suppose that, perhaps, in your case, the implementation of the function pow() has something like this:

 template <typename T1, typename T2> double pow_bad(T1 base, T2 exp) { return std::exp( double(exp) * std::log(double(base)) ); } 

or something like that. One of the possible results of the above function on your input data will be :

 i^2.0 = pow_bad(i, 2) int( pow_bad(i, 2) ) 1^2.0 = 1 1 3^2.0 = 9.0000000000000018 9 5^2.0 = 24.999999999999996 24 7^2.0 = 48.999999999999993 48 9^2.0 = 81.000000000000028 81 11^2.0 = 121.00000000000003 121 13^2.0 = 169 169 

Although the output does not exactly coincide with the output of your program, I think the essence is clear.


Try using the debugger to go inside the pow function and see how it works inside. It is possible that there will be nothing interesting there, but it’s worth a try.

  • Thank you, it is clear paint) - Homo Civicus

If you want it to work with the size of the array that the user enters, then you need to replace

 const int length = n; int numbers[length]; 

on

 int *numbers= new int[n] 

and before

 system("pause>nul"); 

add

 delete[] numbers 

read about dynamic memory in c ++ https://ru.wikipedia.org/wiki/New_(C%2B%2B)

    The pow function does not multiply the value of xn times, since it must also calculate the degree using fractional exponents. Use another function or write your own. The pow function (possibly using logarithms) uses floating-point values ​​to calculate the degree value, so int does not work as you think it should work. Also, your function for calculating the degree of int will obviously work faster.

      Replace to check

       numbers[j] = pow(j, 2); 

      on

       numbers[j] = j*j; 

      Because apart from the used non-standard expansion, this is the only weak point ...

      PS I don’t understand two students' love - to float instead of double and to pow where it is completely useless ...

      • Yes, with this option, the correct values ​​are displayed. Apparently, the pow () function does not work as I expected. - Homo Civicus