Jump to content

What's the problem with this code?


Recommended Posts

Figured I'd come to a MS forum because this is a question about Visual C++...

#include "stdafx.h"

#include <iostream>

#include <cmath>

#using <mscorlib.dll>

using namespace std;

using namespace System;

int _tmain()

{

int done=1;

int a, b, c, d, e, f;

double d1, d2, x1new, x2new, x1old, x2old, t1, t2;

//Like Newton Raphson...

cout<<"Input a value for a (in your system of equations):";cin>>a;

cout<<"Input a value for b (in your system of equations):";cin>>b;

cout<<"Input a value for c (in your system of equations):";cin>>c;

cout<<"Input a value for d (in your system of equations):";cin>>d;

cout<<"Input a value for e (in your system of equations):";cin>>e;

cout<<"Input a value for f (in your system of equations):";cin>>f;

x1old=1;

x2old=1;

while(done != 0){

x1new=(c - b*x2old)/a;

x2new=(f - d*x1old)/e;

t1=a*x1new + b*x2new - c;

t2=d*x1new + e*x2new - f;

if(fabs (t1) < 1.0e-6)

d1=1;

else

d1=0;

if(fabs (t2) < 1.0e-6)

d2=1;

else

d2=0;

done=d1+d2-2;

x1old=x1new;

x2old=x2new;

}

cout<<"The x value is:"<<x1new<<endl;

cout<<"The y value is:"<<x2new<<endl;

return 0;

}

It's supposedly a system of equations formula, and unfortunately, I can't see what's wrong with it... if any of you are running Visual's C++ IDE, try it out, no errors at compile time... it runs fine but seems to hang at the calculation.

And yes, I'm a n00b.

Link to comment
Share on other sites


without comments in your code I'm not going to try to figure out the purpose of the formula you are running, but I can tell you it never equals zero. Your code is fine, it's your math thats wrong. Just try set done=0; and loop ends fine. If you need help with the math ask a math wiz or explain what it's supposed to do.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...