Help with C++ program execution.


 
Thread Tools Search this Thread
Top Forums Programming Help with C++ program execution.
# 1  
Old 11-01-2011
Help with C++ program execution.

//Find the root of the equation (x^2)-2 by bisection method.

Code:
#include<iostream>
using namespace std;
double a,x;
double f(double x)
{
    return ((x*x)-2);
}                                         //Suppose the function is (x*x)-2.
void calcx(double a1,double b1)
    {
        x = (a1+b1)/2;
        while((f(x)*f(a))!=0)                //If f(x)*f(a)=0, then the root is 'x' itself!
        {
            if(f(x)*f(a) < 0)
            {
                if(x==1.41421509)                               //The answer should be 1.414... so given like this!
                {
                    cout<<"Process completed!";
                    cout<<"Result is: "<<x;
                }
                a1=x;                                       //The value 'a' is replaced by x and 'b' remains the same.
                calcx(a1,b1);                               //Then, we'll do recurrsion for range (x,b)
            }
            else if(f(x)*f(a) > 0)
            {
                if(x==1.41421509)
                {
                    cout<<"Process completed!";
                    cout<<"Result is: "<<x;
                }
                b1=x;
                calcx(a1,b1);                               //Here, recurrsion is done for range (a,x).
            }
        }
    }
int main()
{
    double b,res;                               
    cout<<"Enter the values of 'a' and 'b': ";
    cin>>a>>b;                                            //The range already given.
    calcx(a,b);
    return 0;
}



OUTPUT:

Segmentation fault

Q. Can i know why this kind of message is being displayed?
# 2  
Old 11-01-2011
Your program will recurse forever, consuming hypothetically infinite amounts of memory. "if(x==1.41421509)" will probably never return true, because floating point values don't have infinite precision. You should do
Code:
if(fabs(x-1.41421509) < .001)

instead, to see if it's approaching a value precise enough. Adjust ".001" to taste.

Since your computer probably won't let you have infinite amounts of stack space, recursing forever causes it to crash.

Why not use a loop instead of having calcx() call itself?

Code:
do
{
    // Calculate result
    // Set new ranges
    // Only if not precise enough, loop through again
} while(fabs(x-1.41421509) >= 0.001);

---------- Post updated at 10:53 AM ---------- Previous update was at 10:50 AM ----------

You should probably also have a count value, so if it gets stuck, it'll give up after a few thousand loops.
# 3  
Old 11-01-2011
I think that's probably one of the most misunderstood things about floats. Using the equals operator on them is generally a bad idea. As Corona pointed out, you should test within a range of the value you want. The easiest way to do that is how he showed you, where, in his example the .001 would be the precision. The smaller that number (.000001 for example) the "closer" to your target you are. If you use, for example, .1, then you could be further from your target.

Of course, as with anything, this could still cause an issue. I mean, if you increase/decrease the value too much at each iteration you may miss your "window" of precision, never to return. So, you need to be aware and compare within an appropriate precision for the algorithm you're running.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execution of compressed program

I need UNIX scripts for polling, Uncompressing files and moving files between directory. Also trying to save file paths and any other variables in an independent file (.env) and use these at runtime by executing this file in the main script. (3 Replies)
Discussion started by: new2script
3 Replies

2. Shell Programming and Scripting

Automating execution of commands inside a program

I have a program dnapars I execute the program from command line as following: ./dnapars The program then prompts me some message as a user menu from where I have to select a series of options in the order R U Y R. And then I copy the output file (outfile) in another result file. I wrote the... (3 Replies)
Discussion started by: deeptisjains
3 Replies

3. Programming

popen hangs program during cmd execution

How can I get around this? when my program reaches the following popen job it halts the program until the ping/netstat/ipconfig/traceroute is completed then resume to the rest of the program... FILE *in; extern FILE *popen(); char buff; char newline; char nstat; char nping; ... (5 Replies)
Discussion started by: Jess83
5 Replies

4. Shell Programming and Scripting

Execution problem with repeat the same program with two set of same data

I got a program named as "fastq_to_fasta". I got a long list of file all named as AB1 and AB2. My input file is : 071022_L1_AB1.fq 012121_L1_AB1.fq 021213_L1_AB1.fq 012153_L1_AB1.fq 071022_L1_AB2.fq 012121_L1_AB2.fq 021213_L1_AB2.fq 012153_L1_AB2.fq . . . . . . My desired... (10 Replies)
Discussion started by: patrick87
10 Replies

5. Programming

Debugging Program during execution

I have made use of 'valgrind' and -finstrument-functions compiler option for debugging / analyzing code. Both the options lets us know the line / file being executed to some extent. Is there a generic way that lets program dump the file:line it is getting executed dumped to a log file during... (3 Replies)
Discussion started by: uunniixx
3 Replies

6. Solaris

Program execution order like FIFO queue

Hi all: I have a problem with a C++ service runing on solaris 10. This service only set a signal on oracle table. When the service detect a cut off on the line (tcp/ip), trigger a cobol program for set the signal OFF. When the line is on again, the service trigger the same cobol program for set... (0 Replies)
Discussion started by: hcastellani
0 Replies

7. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

8. UNIX for Dummies Questions & Answers

multifile c program compilation and execution

i am using unix os and my program is divided in two text files .:):) how to compile and make one executable file, using unix command. (1 Reply)
Discussion started by: alokmishra8
1 Replies

9. Linux

unix command for multifile program execution

how can I compile and execute multifile program in unix os.:o (0 Replies)
Discussion started by: alokmishra8
0 Replies

10. Programming

execution small C++ program in UNIX

Hi my friends I am beginner unix programmer I have written small c++ program in text editor and I have change it mode to 555 to make it executable file to use it in unix O.P. #include<iostream.h> main() { cout<<"Hello World"; } but some syntax erroe came for << can any one help... (5 Replies)
Discussion started by: bintaleb
5 Replies
Login or Register to Ask a Question