Urgent Help me!!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
int x,n;
cout<<"Please enter values for x : ";
cin>>x;
cout<<"Please enter values for n : ";
cin>>n;
float product=2.0;
float sum = n;
float fact=2.0;
float c = 1.0;

for (int i=2; i<=n ; i++)
{
sum = 2.0/(x-n);
product= product *= i;
fact=fact *=i/(x-2);

if(i%2==0) //every two times
sum+=(product/fact);
else
sum-=(product/fact);
}



cout<<"The answer is: "<<sum<<endl;
system("pause");
return 0;
}

WHY AM I GETTING inf as result????
Last edited on
Hi, it would help us help you, if you put [code] and [/code] tags around your code, to let it be properly formatted. You can still edit your post and add them now.

What input are you entering for x and n?

And can you explain specifically what instructions you're trying to tell the computer to do on these two lines?
1
2
        product= product *= i;
        fact=fact *=i/(x-2);


You should turn on warnings for your compiler, your code has undefined behavior
19:30: warning: operation on 'product' may be undefined [-Wsequence-point]
20:28: warning: operation on 'fact' may be undefined [-Wsequence-point]
14:11: warning: unused variable 'c' [-Wunused-variable]
Last edited on
i dont understand. where should i use that tags?
Edit your post and add [code] {your code here} [/code]

It will look like this:
{your code here}

See my edit. Right now you have undefined or unspecified behavior (depending on C++14 vs C++17 rules, either way it should be avoided).

Did you mean to do
1
2
3
        product *= i;
        fact *=i/(x-2);
        

instead?

Also, you never answer my question, What input are you entering for x and n?
Last edited on
If x==n or x==2, then you have division by 0.
x value must always be higher than n value.

i tried to do factorial in it.
Normally what does inf means?

Feel free to edit the code...
line 14 removed, no problem.
Last edited on
Assuming the change in my previous post was applied (I don't know if that's actually what you intend):

Let's say x = 5, n = 4.
i =2
sum = 2.0 / (5 - 4) = 2.0 / 1 = 2.0

fact *= i/(x-2); --> fact = fact * (2 / 3)
2 / 3 is integer division, so you're doing fact = fact * 0 essentially.
Then you divide by fact... and boom -- you get infinity.

If you still want to do division, but not integer division, then do
fact *= static_cast<double>(i)/(x-2);
Again... not sure if that's actually what you intend.

You are also re-assigning "sum" each iteration within the loop. Probably not what you want.

______________________________________


What would really, really help is if you gave us some test cases. Because you haven't made it clear what you're trying to do. Because a normal factorial isn't a function of two variables.
Given: x = 5, n = 4, what should sum be?
Given: x = 6, n = 4, what should sum be?
Given: x = 6, n = 5, what should sum be?
etc.
Last edited on
Write a program to prompt the user to key in x and n value to calculate the result for the following formula:

Result = 1+ 2!/(x-2) - 3!/(x-3) + 4!/(x-4) - ....... n!/(x-n)

Note: x value must be greater than n value.

this is the question..
sorry for not posting it earlier.
you got any other way to solve this question?
A loop, yes, but yours differs quite much from the expected.

See, what happens:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
  int x = 6;
  int n = 4;
  float product = 2.0;
  float sum = n;
  float fact=2.0;

  for ( int i=2; i<=n ; i++ ) {
    sum = 2.0/(x-n);
    product *= i;
    fact *= static_cast<float>(i)/(x-2);

    if(i%2==0) //every two times
      sum+=(product/fact);
    else
      sum-=(product/fact);

    std::cout << "Term " << i << " factorial " << product << " divider " << fact << " sum " << sum << '\n';
  }
  cout<<"The answer is: "<<sum<<endl;
}

Term 2 factorial 4 divider 1 sum 5
Term 3 factorial 12 divider 0.75 sum -15
Term 4 factorial 48 divider 0.75 sum 65
The answer is: 65

But the expectation is:
+2!/(x-2) =  2/4 = +0.5
-3!/(x-3) = -6/3 = -2
+4!/(x-4) = 24/2 = +12
Final = 11.5
i dont get it !!
i need it urgently!!
please help me
Read again, what you have to add to a sum and compare that to the things the following program shows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>

int main()
{
  int n = 9;
  int x = 17;
  unsigned long long factorial = 1ULL;
  int sign = 1;
  std::cout << "Term 1 # 1\n";
  for ( int i=2; i<=n ; ++i ) {
    factorial *= i;
    std::cout << "Term " << i << " sign " << std::setw(2) << sign << " # " << i;
    std::cout << "! == " << std::setw(7) << factorial;
    std::cout << " # (x-" << i << ") == " << x - i << '\n';
    sign = -sign;
  }
}

Term 1 # 1
Term 2 sign  1 # 2! ==       2 # (x-2) == 15
Term 3 sign -1 # 3! ==       6 # (x-3) == 14
Term 4 sign  1 # 4! ==      24 # (x-4) == 13
Term 5 sign -1 # 5! ==     120 # (x-5) == 12
Term 6 sign  1 # 6! ==     720 # (x-6) == 11
Term 7 sign -1 # 7! ==    5040 # (x-7) == 10
Term 8 sign  1 # 8! ==   40320 # (x-8) == 9
Term 9 sign -1 # 9! ==  362880 # (x-9) == 8
but still im not getting the answer correct...

i want the code to ask user to input value x and value n.
then it shows the answer.
thank you for helping.
factorials get too big to fit into integers very quickly. Does your code work for a small value of N, like n < 10 ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   double x;
   int n;
   cout << "x: ";   cin >> x;
   cout << "n: ";   cin >> n;
   double answer = 1;
   for ( int i = 2, s = 1; i <= n; i++, s = -s) answer += s * tgamma( i + 1.0 ) / ( x - i );
   cout << "Answer: " << answer << '\n';
}


x: 17
n: 9
Answer: -41326
Topic archived. No new replies allowed.