Help Needed: Nothing is Printed

Discussion in 'C++' started by Chimera9, Apr 26, 2010.

  1. Chimera9

    Chimera9 New Member

    Joined:
    Apr 26, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    using namespace std;
    # include <iostream>
    # include <string>
    # include <iomanip>
    # include <vector>
     
    double a;
    double b;
    double h;
     
    double f(double x) {
         return x* - 1;
    }
    double sign(double x) {
         if (x < 0) {
              return 0;
         }
            else {
                 return 1;
            }
    }
    double FindRoot (double a,double b,double h) {
    double s;
    double x;
    s=sign(f(a));
    while (s = sign(f(x))) {
      for (x=0; x < b; x=(a+b)/h)
    return x;
    }
    }
    double root=FindRoot (a, b, h);
    void PrintResults ( double a, double b, double root) {
    cout << "A root of the function f(x)=x^2-1 between " << a << " and " << b << " is " << root << endl << flush;
    }
     
    int main( ) {
    PrintResults (-2,0,0.01);
      return 0;
    }
    I'm unsure why nothing's being printed. I'm guessing something's wrong with the format of PrintResults or my formula for finding the root is incorrect. Help wanted urgently. Thank you.
     
    Last edited by a moderator: Apr 27, 2010
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    one error is this ---> while (s == sign(f(x))) {
    and another is this -->return x*x - 1;
     
  3. Chimera9

    Chimera9 New Member

    Joined:
    Apr 26, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I fixed those errors (thanks for catching those, by the way), but still nothing is being printed.
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Found the error :)
    Change :
    Code:
    double root=FindRoot (a, b, h);
    to
    Code:
    double root;
    First, you are trying to initialize a global var root outside any procedure.
    Second, the vars that you pass as args : a, b, h to FindRoot are un-initialized !

    Change the code and it should print.
     
  5. davidk

    davidk New Member

    Joined:
    Mar 25, 2010
    Messages:
    16
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    http://www.firmdev.com/firm_framework
    Place this line
    Code:
    double root=FindRoot (a, b, h);
    inside your PrintResults() function, or initialize 'root' variable inside main().
     
  6. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    another error is this

    no need for for command just put return 0; its the same!
    No command is executed after a return!!!

    and of course as davidk and SaswatPadhi said change your code like this

    Code:
    using namespace std;
    # include <iostream>
    # include <string>
    # include <iomanip>
    # include <vector>
     
    [COLOR=Red]//[/COLOR]double a;
    [COLOR=Red]//[/COLOR]double b;
    [COLOR=Red]//[/COLOR]double h;
      .......
    int main( ) {
    [COLOR=Red]double a=-2,b=0,h=0.01;[/COLOR]
        [COLOR=Red]double root=FindRoot (a, b, h);
        PrintResults (a,b,root);[/COLOR]
    .............
    
     
  7. Chimera9

    Chimera9 New Member

    Joined:
    Apr 26, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for all the help guys! I finally got it right with a little input from my instructor though. But again, thanks for taking the time to help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice