C++ challenge, helps with logic

Asked

Viewed 221 times

5

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

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

I thought I’d make recursion, but I could not develop the logic. Anyway, I have no idea how to start. If any can give me some hint would be grateful.

  • 2

    Please, who is negative, comment something.

  • Explain a little better... the function should receive a parameter n and he must be the limit and another i? and then return y?

  • 2

    I think the reason you’re being negative is because it’s a challenge that’s been proposed to you, it’s the same as someone doing your homework. But now the important thing, should explain better the value of i and n

  • 1

    I believe that’s right @krystalgamer, he is "cheating" on a test, but we can help even without giving the answer... but as you mentioned... we need more information...

  • 1

    I think that ... and i^n together means that for any i the expression returns a value - but in my mathematical ignorance this is seeming to pile burst... ;)

3 answers

5

Well, we have three variables in that context: y is the value to be discovered, i is likely to be an informed value for the calculation and n would be the amount of times the calculation should be done.

Analyzing the problem:

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

I could notice a pattern. Initially we have the value of i, and then a subtraction by the sum of i elevated to the successor of every power (i^2) + (i^3) and soon after this double is subtracted by the sum of another double following the same pattern: ((i^2) + (i^3)) - ((i^4) + (i^5)).

In case I have intended correctly, the following code in C# theoretically would 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 in C++.

In case I didn’t understand it right, please just comment first of -1.

  • 1

    Why did you answer in c#??

  • 1

    @krystalgamer My guess is that it is the language he has most familiarity, and if you have to choose one to write the example, why not use the one you know more?

  • 3

    @Gabe that doesn’t make any sense. I know C and a little C#. That doesn’t mean that when I ask a C question I get answered in C#. I ask garlic and answer bugles. For some reason there are tags

  • @krystalgamer I don’t disagree with you, but in an open question asking for code examples, this can happen. It is not entirely unusual that questions involving the creation or revision of algorithms end up having answers that indicate the correct algorithm but without writing it in the language that was requested. Whether or not they help is something the AP should decide - maybe even editing the answer with the right language.

  • 1

    Well @krystalgamer I realized that the question Zezinho made is part of a task given by the programming teacher, as he himself clarifies in the question. I believe the teacher is trying to familiarize him with the language, so by responding in another language, I’m helping with the logic, but making him strive to learn more of the syntax of the language proposed by the teacher.

3

This is the implementation of the programme:

#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 question would be to use the sum expression for a geometric progression. It would be a basic and interesting exercise for the user to implement.

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;
}

See 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 portion signal is positive; otherwise it is negative. Therefore, it is possible to rewrite the expression like this: y = -[(-i)^1] - [(-i)^2] - [(-i)^3] - [(-i)^4] - [(-i)^5] - ... - [(-i)^n].

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

(OBS.: To simplify the function, I am assuming the parameter n is positive, that is, n >= 1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.