C ++ Challenge, help with logic

5

I received a list of exercises from the programming teacher, this is a challenge.

  

write a function with either return the value of the expression
y = i - i^2 + i^3 -i^4 +i^5 - ... +- i^n

I thought about doing recursion , but I could not develop logic. Anyway, I have no idea how to start. If anyone can give me any tips I would be grateful.

    
asked by anonymous 20.09.2015 / 20:53

3 answers

5

Well, we have three variables in this context: y is the value to be discovered, i is probably an informed value to do the calculation and n would be the number of times the calculation should be done. Home Analyzing the problem:

y = i - i^2 + i^3 -i^4 +i^5 - ... +- i^n

I noticed a pattern. Initially we have the value of i , and then a subtraction by the sum of i raised to the successor of each power (i^2) + (i^3) and soon after that pair is subtracted by the sum of another pair following the same pattern: ((i^2) + (i^3)) - ((i^4) + (i^5)) . Home If I have intended correctly, the following code in C # would theoretically solve the problem:

        Console.WriteLine("Informe o valor de i: ");
        double i = double.Parse(Console.ReadLine());

        Console.WriteLine("Informe o valor de n: ");
        int n = int.Parse(Console.ReadLine());

        double y = i;

        for(int x = 2; x <= n; x++)
        {
            y = y - (Math.Pow(i,x) + Math.Pow(i,(x+1)));
            x++;
        }

        Console.WriteLine("O valor de y = " + y);

I did the following Fiddle where you can do tests ...

As a task, just try to run C ++.

  

If I do not get it right, please just comment first   of -1.

    
20.09.2015 / 22:27
3

Here's the program's implementation:

#include<iostream>
#include <cmath>
using namespace std;

double serieDePotencias(double x, unsigned int n){
    double resultado=0;
    for (int i=1; i!=n+1; ++i){
        resultado+=pow(-1,i+1)*pow(x,i);
    }
    return resultado;
}

// Programa principal
int main(){ 
    double valor;
    int potencia;

    // Pede os valores
    cout << "Introduza o valor de x: ";
    cin >> valor;
    cout << "Introduza o valor de n: ";
    cin >> potencia;

    // Resultados
    cout << "Soma da serie: " << serieDePotencias(valor,potencia) << endl;

    return 0;
}

Another way to solve the problem would be to use the sum expression for a geometric progression. It would be a basic and interesting exercise for the user to implement.

    
07.12.2015 / 03:22
2

The function below returns the expected value:

double f(double i, int n)
{
    double result{ 0. };

    for (int j = 1; j <= n; j++)
    {
        result -= pow(-i, j);
    }

    return result;
}

Notice that the sign of each portion of the sum y = i - i^2 + i^3 -i^4 +i^5 - ... +- i^n is determined as follows: if the power operation exponent is odd, the plot signal is positive; otherwise it is negative. Therefore, it is possible to rewrite the expression thus: y = -[(-i)^1] - [(-i)^2] - [(-i)^3] - [(-i)^4] - [(-i)^5] - ... - [(-i)^n] .

And to use the pow(...) function, include the header file <math.h> .

(To simplify the function, I'm assuming that the parameter n is positive, that is, n >= 1 )

    
25.09.2015 / 00:11