Search code examples
c++arraysfunctionloops

Is there anything wrong with my function implementation?


I need to write a function that will take the user input and do the following formula attached, however I keep getting the wrong output and cant seem what is the logical error in my code

Here is my code:

#include <iostream>
using namespace std;
int functX(int x) {
  int fx = 1;

  for (int i = 1; i <= 15; i++) {
    fx *= (x + i);
  }

  return fx;
}

int main() {
  int n;
  cout << " Enter the value you want to sum" << endl;
  cin >> n;

  cout << functX(n) << endl;

  system("Pause");
  return 0;
}

enter image description here


Solution

  • The function f seems to be implemented incorrectly; you seem to have exchanged additiond and multiplication. A correct implementation could look as follows.

    int functX(int x)
    {
        int fx = 0;
        for (int i = 1; i <= 15; i++)
        {
            fx += (i * x);
        }
        return fx;
    }