The task is to write a program that requests four integers a, b, c and d. The program should output two values ​​of arithmetic expressions separated by spaces in the first line:

  1. (a + b) 3
  2. (c - d) 4

In the second line, the program should output three values ​​of arithmetic expressions through a space:

  1. (a + 2ab + b) 2
  2. (c - (3cd) 2 + 5) 2
  3. 6 (b 2 - 4ac) 2

Code:

#include <iostream> using namespace std; int main() { int a, b, c, d; cout << (a+b) * (a+b) * (a+b) * (a+b) << " " << (cb) * (cb) * (cb) * (cb) << endl; cout << ((a + (2*a*b) + b) * (a + (2*a*b) + b)) << " " << (c - ((3*c*d) * (3*c*d)) + 5) * (c - ((3*c*d) * (3*c*d)) + 5) << " " << 6 * (((b*b) - 4*a*b)) * ((b*b) - 4*a*b) << endl; return 0; } 

This code should output:

 27 1 49 18496 384 

And displays:

 0 0 0 25 0 

Please help me: what is wrong?

  • 2
    You did not even initialize the variables! - Andrej Levkovitch
  • one
    The program must request four numbers. Where does she do it? - Enikeyschik
  • 2
    Hmm, people apparently do not even try to find a solution before going to the forum. And, after all, in theory, you won’t complain about the question, there is a code. - pinguin
  • This example clearly shows that in the local area it is always better to initialize the declared objects of built-in types. In the opposite case, they may have a default value, and may contain garbage - AR Hovsepyan
  • one
    @AR Hovsepyan: Initializing is very good and correct if you have a meaningful and informative initializer. If the initializer is not ready yet, then initialization “with whatever” will only lead to sweeping errors under the carpet. - AnT

1 answer 1

Where does the program have these values ​​from? If from the console, it should look like this:

 #include <cmath> #include <iostream> using namespace std; int main() { int a,b,c,d; cin >> a >> b >> c >> d; cout << pow(a+b, 3) <<" " << pow(cb, 4)<<endl; //аналогично второе выражение return 0; } 

To make the code not so bulky, use pow() from cmath

  • one
    Using pow to calculate the powers of integers must be punished severely! That's what the question author did exceptionally correctly - that’s what he did without pow . It was necessary to write less cumbersome, of course - without repeating the same difference. But without pow anyway. - AnT
  • one
    pow(a, b) of cmath is actually exp(a * log(b)) . For integer calculations is not suitable. - AnT