-2

I can't find the logic error in my code. is the entire thing wrong mathematically? or is it partially correct.

float x,term,fx;
int i, nterms;
cin>>x>>nterms;
for(i=1;i<=nterms;i+=2){ 
term=1;
fact=1;
for(j=1;j<=i;j++){
        term=term*x;
        fact=fact*j;
}
sign=-1*sign;
fx+=sign*term/fact;
}

cout << fixed << showpoint;
cout << setprecision(6);
cout<<fx;
xycik
  • 1
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 28 '18 at 20:39
  • A possible hint though: Uninitialized local variables really *are* uninitialized. They will have an *indeterminate* (and seemingly random) value. Using that indeterminate value in any way leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). Now think a little bit about your variables, and how you use them. – Some programmer dude Oct 28 '18 at 20:40
  • Oh, and you should definitely take some time to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Oct 28 '18 at 20:51

1 Answers1

0

I fixed your code step by step, I can't go further because I'm not sure what you want to do in your code. If you tell us what they are, I'll be able to fix this code.

#include <iostream>
#include <iomanip>
#include <stdio.h>

int main() {
  long double x,term,fx, fact;
  int i, j, nterms;
  int sign;

  std::cin>>x>>nterms;
  term=1;
  fact=1;
  sign=1;
  fx = 0;
  for(i=1;i<=nterms;i+=2){ 
    for(j=1;j<=i;j++){
      term=term*x;
      fact=fact*j;
    }
    sign=-1*sign;
    fx+=sign*term/fact;
  }
  std::cout << std::setprecision( 6 )  << fx << std::endl;
}

Some remarks here:

  1. It seems that using namespace was used for std. Use it carefully, since a conflict between some namespaces could occur. This page (french) suggest two use case.

    a. Importing the symbols one by one:

    using std::cout; 
    using std::endl; 
    

    b. Use them only in a local scope (in a curly brace block for example):

    void a_function() { 
        // std will be imported only within this block 
        using namespace std;   
       cout << "hello there" << endl; // will work
    }
    
  2. Declare your variables before using them. Compare my answer with your code sample.

  3. Be careful with the min and max values a type can handle. Your function seems to handle very huge or small values. I changed their type to long double (but it's just moving the issue further). You have the ranges. Have a look this SO answer. It leads to limit.h, which shows numeric limits. Pick the safest to store your values (or sets a max and min value for user inputs).
Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • sign is supposed to determine the + or - for the next value. and I was told show point was for the accuracy of the output. – xycik Oct 28 '18 at 21:38
  • Also Thank you for the help – xycik Oct 28 '18 at 21:39
  • I think using a bool would be better here. it is an alternating pattern the first term is x-x^3/3!+x^5/5! and so forth – xycik Oct 28 '18 at 21:46
  • bool sign = true; //for + and false for negative Then using a if else for implementing it into the equation. The sign is decided by the number of terms in the sequence. so if nterms=3 the signs would be - + - – xycik Oct 28 '18 at 21:48
  • oh ok thank you for the clarification on that. Also sorry if my answers are not clear. I am not a native English speaker – xycik Oct 28 '18 at 21:51
  • The sign would change each term starting with - – xycik Oct 28 '18 at 22:01
  • I have tried writing this equation"fx=x-x^3/3!+x^5/5!-x^7/7!......" in many different ways and so far they all fail in certain test inputs. – xycik Oct 28 '18 at 22:09
  • it is consistently outputting -1. not sure why – xycik Oct 28 '18 at 22:41