looping help

This is a discussion on looping help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; void check(double a, double b); void checkepsilon(double eps); computexmid(double& c, double& d, double eps, ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    looping help

    Code:
    #include <iostream>
    using namespace std;
    void check(double a, double b);
    void checkepsilon(double eps);
    computexmid(double& c, double& d, double eps, double& mid);
    int main()
    
    {
    double xleft = 0;
    double xright = 0;
    double epsilon = 0;
    double mp = 0;
    double xmid = 0;
    cout << "Please enter xleft " << endl;
    cin >> xleft;
    cout << "Please enter xright" << endl;
    cin >> xright;
    check(xleft, xright);
    cout << "Please enter epsilon" << endl;
    cin >> epsilon;
    checkepsilon(epsilon);
    xmid = computexmid(xleft, xright, epsilon, mp);
    
    return 0;
    }
    
    
    void check(double a, double b)
    {
    
    	if(a > b)
    		cout << "Bisection can not be computed (sign error, or xleft is greater than xright)" << endl;
    } 
    
    void checkepsilon(double ep)
    {
    	double eps = 0;
    		if(eps < 0)
    		cout << "Error - epsilon must be a positive number" << endl;
    }
    
    computexmid(double& c, double& d, double eps, double& mid)
    {
    
    	do{mid = (0.5 * (c+d));}
    	while(( d-c) > eps);
    	{
    		if( (d-c) <= eps)
    			cout << "x left is " << c << ", x right is " << d << endl;
    	}
    
    	return mid;
    }
    ok my question is how do i stop computexmid to stop going into infinite loop and return xmid and xleft and xright for me?
    incase you're wondering im trying to do besection for my programming homework.
    if you need to see clearer pics of what im trying to do, check out this out my assignment.
    read assignment 4

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm not familiar with what you are trying to do and the link is giving me an error page, but I can see a problem:
    Code:
    do{mid = (0.5 * (c+d));}
    	while(( d-c) > eps);
    d and c are not changing there, which is why you run into an infinite loop. So basically you need to either change the condition of the loop or change the values of c and d in the loop.

    Also, you should declare a return type for your function (probably double in this case). Right now it probably defaults to int (if it even compiles)

    Maybe you should explain the assignment or at least review do...while loops, because it doesn't look like you quite understand it.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 02:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21