Nogurenn
Nogurenn

Reputation: 878

Why does the other function output the wrong values?

I'm writing a root finding program. I'm already at this bug for hours. The code seems right to me, but the terminal output is wrong. And this newbie has a lot of arguments present in his functions. I hope you guys don't mind.

Yes, I've compiled the codes over and over again.

These are the troublesome lines. Below these are all the lines that I think are relevant. Instead of it outputting exp(-x) - x and Newton-Raphson respectively, it gives me Restart selection and exp(-4*x) - x.

printf("%25s:%20s\n", "Equation", a[eq-1].c_str());
printf("%25s:%20s\n", "Method", b[meth-1].c_str());

Here are the relevant variables in main().

vector<string> functName;
vector<string> methodName;
vector<string> advSettings;

int c_eq = 0;
int c_met = 0;
int c_settings = 0;
double c_guess1 = 0;
double c_guess2 = 0;

// defaults
int sigFigs = 6;
int iter = 1000;
int iterMode = 0;
int perIter = 0;
int plotMode = 0;

functName.push_back("exp(-x) - x");
functName.push_back("exp(-2*x) - x");
functName.push_back("exp(-3*x) - x");
functName.push_back("exp(-4*x) - x");

methodName.push_back("Newton-Raphson");
methodName.push_back("False Position");
methodName.push_back("Bisection");
methodName.push_back("Secant");

advSettings.push_back("Proceed with settings");
advSettings.push_back("Restart selection");
advSettings.push_back("Change advanced settings");

Here is the first function that outputs things properly.

template <typename inputType>
void basicInterface(const vector<string> &a,
                    const vector<string> &b,
                    int eq,
                    int meth,
                    inputType &root1,
                    inputType &root2)
{
    cout << "Input the corresponding number of your choice.\n";
    cout << "Choose an equation to solve:\n";
    for (int i = 0; i < a.size(); i++)
        cout << "[" << i+1 << "] " << a[i] << endl;          // line of interest
    cout << " >>> ";
    inputCheck(eq,1,4);

    cout << "Choose a method to use:\n";
    for (int i = 0; i < b.size(); i++)
        cout << "[" << i+1 << "] " <<  b[i] << endl;         // line of interest
    cout << " >>> ";
    inputCheck(meth,1,4);

    // more stuff
}

The lines of interest output the following in my Terminal, which are correct.

[1] exp(-x) - x
[2] exp(-2*x) - x
[3] exp(-3*x) - x
[4] exp(-4*x) - x

[1] Newton-Raphson
[2] False Position
[3] Bisection
[4] Secant

This other function, which is supposed to show all stuff,

template <typename inputType>
void showSettings(  const vector<string> &a,
                    const vector<string> &b,
                    int eq,
                    int meth,
                    inputType root1,
                    inputType root2,
                    int sigs,
                    int showPerLoop,
                    int plotRoots,
                    int loopMode,
                    int minLoops)
{
    cout << "Requirements satisfied.\n";
    printf("%25s:%20s\n", "Equation", a[eq-1].c_str());
    printf("%25s:%20s\n", "Method", b[meth-1].c_str());
    if(meth-1 == 1)
        printf("%25s:%20f\n", "Initial Guess", root1);
    else
    {
        printf("%25s:%20f\n", "Initial Guess 1", root1);
        printf("%25s:%20f\n", "Initial Guess 2", root2);
    }
    printf("%25s:%20d\n", "Minimum Sig Figs", sigs);
    printf("%25s:%20d (1 if true)\n", "Show Root per Iteration", showPerLoop);
    printf("%25s:%20d (1 if true)\n", "Show Root Graph", plotRoots);
    printf("%25s:%20d (1 if true)\n", "Iteration Mode", loopMode);
    printf("%25s:%20d\n", "Minimum Iterations", minLoops);
}

almost gives perfect output.

                 Equation:   Restart selection        // where did these
                   Method:       exp(-4*x) - x        // come from?
          Initial Guess 1:            1.000000
          Initial Guess 2:            0.000000
         Minimum Sig Figs:                   6
  Show Root per Iteration:                   0 (1 if true)
          Show Root Graph:                   0 (1 if true)
           Iteration Mode:                   0 (1 if true)
       Minimum Iterations:                1000

The following are the lines I used to call the two functions.

    basicInterface( functName,
                    methodName,
                    c_eq,
                    c_met,
                    c_guess1,
                    c_guess2);

    showSettings(   functName,
                    methodName,
                    c_eq,
                    c_met,
                    c_guess1,
                    c_guess2,
                    sigFigs,
                    perIter,
                    plotMode,
                    iterMode,
                    iter);

Upvotes: 0

Views: 280

Answers (1)

user1781290
user1781290

Reputation: 2864

You posted a lot of code. But it seems these two lines produce your output.

printf("%25s:%20s\n", "Equation", a[eq-1].c_str());
printf("%25s:%20s\n", "Method", b[meth-1].c_str());

eq and meth are both 0. This causes access to the vectors at index -1. Use at instead of operator [] to check the passed bounds if unsure. at will throw an exception if the index is not valid; operator [] will fail silently instead and produce UB.

Upvotes: 1

Related Questions