C++ and Order of Operations

Code help, language discussions, and software development advice.
Post Reply
User avatar
ccb056
Site Administrator
Posts: 981
Joined: January 14th, 2004, 11:36 pm
Location: Texas
Contact:

C++ and Order of Operations

Post by ccb056 »

will C++ process this according to the order of operations:

Code: Select all

V=(X+Y)/(1+((X*Y)/(C^2)));
Tebow2000
Registered User
Posts: 1099
Joined: January 19th, 2004, 7:56 am
Location: New Orleans, Louisiana
Contact:

Post by Tebow2000 »

I will just do better memorizing them!
Redcode Hosting redcodehosting.com | Unix Shared Hosting | sales[aT]redcodehosting[dOt]com
Smartweb
Registered User
Posts: 622
Joined: January 15th, 2004, 2:11 am
Contact:

Post by Smartweb »

yeah, all computer languages follow order of operations.
User avatar
ccb056
Site Administrator
Posts: 981
Joined: January 14th, 2004, 11:36 pm
Location: Texas
Contact:

Post by ccb056 »

so, if I just used v*x-a+b/c it would follow order of operations on any lanyuage rather than execute from left to right?
Smartweb
Registered User
Posts: 622
Joined: January 15th, 2004, 2:11 am
Contact:

Post by Smartweb »

right
User avatar
ccb056
Site Administrator
Posts: 981
Joined: January 14th, 2004, 11:36 pm
Location: Texas
Contact:

Post by ccb056 »

do they also follow the paren () rule?
Smartweb
Registered User
Posts: 622
Joined: January 15th, 2004, 2:11 am
Contact:

Post by Smartweb »

Yes
User avatar
ccb056
Site Administrator
Posts: 981
Joined: January 14th, 2004, 11:36 pm
Location: Texas
Contact:

Post by ccb056 »

do you see anything wrong with this code?

Code: Select all

#include <iostream>
int main ()
{
int C=299792458;
int X=0;
int Y=0;
int V=0;
/*
V=(V1+V2)/(1+((V1*V2)/(C^2)))
*/
while (X<C)
{
V=(X+Y)/(1+((X*Y)/(C^2)));
cout<<V<<"\n";
X++;
cout<<V<<"\n";
Y++;
cout<<V<<"\n";
}
return 0;
}
Smartweb
Registered User
Posts: 622
Joined: January 15th, 2004, 2:11 am
Contact:

Post by Smartweb »

Well, it'll return about a billion lines in the command line; but if that is what you intended, then yes, it will work.
User avatar
ccb056
Site Administrator
Posts: 981
Joined: January 14th, 2004, 11:36 pm
Location: Texas
Contact:

Post by ccb056 »

ok, I rewrote it a little and it wont work, here is what I'm working with

Code: Select all

#include <iostream>
int main ()
{
double C=299792458;
double X=0;
double Y=0;
double V=0;
/*
V=(V1+V2)/(1+((V1*V2)/(C^2)))
*/
while (X<C)
{
cout<<"V1="<<X<<"\n";
cout<<"V="<<(X+X)/(1+((X*X)/(C^2)))<<"\n";
X++;
}
return 0;
}
The compiler (Dev-C++) says this:
line 14 invalid operands `double' and `int' to binary `operator ^'
Smartweb
Registered User
Posts: 622
Joined: January 15th, 2004, 2:11 am
Contact:

Post by Smartweb »

What happens if you change C^2 to C*C
User avatar
ccb056
Site Administrator
Posts: 981
Joined: January 14th, 2004, 11:36 pm
Location: Texas
Contact:

Post by ccb056 »

ha, its works with c*c, bout not c^2

why did it work before with c^2 and not with the new version?
User avatar
ccb056
Site Administrator
Posts: 981
Joined: January 14th, 2004, 11:36 pm
Location: Texas
Contact:

Post by ccb056 »

it's taking a while to finish processing this code:

Code: Select all

#include <iostream>
int main ()
{
double C=299792458;
double X=0;
double Y=0;
double V=0;
/*
V=(V1+V2)/(1+((V1*V2)/(C^2)))
*/
while (X<C)
{
cout<<"V1="<<X<<"\n";
cout<<"V="<<(X+X)/(1+((X*X)/(C*C)))<<"\n";
X++;
}
return 0;
}
what's funny is the two velocities in question (V1 and V2) are the same, eventually, I want to run through all combinations of V1 and V2 in steps of .1c

I need to figure out how to do the step stuff first...

after I have that info, I will migrate into a 3d graph in which I hope to find a 'cone'

I then want to find the volume and area of the cone
Post Reply