Can you help me please.
I tried to make a progrma which will solve this problem:
x=Uo*t+1/2*a*t^2
#include <iostream>
using namespace std;
int main()
{
int x,a,t,U;
int InputNum();
{
cout<<"Enter a:\n";
cin>> a;
cout<<"Enter t:\n";
cin>>t;
cout<<"Enter Uo:\n";
cin>>U;
x=U*t+(1/2)*a*(t*t);
}
cout<<"X is:"<<x<<"\n";
return x;
}
But when it gives x it gives only U*t.Tell me my mistake please.
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn''t remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
cout<<"X is:"< return x; // -> wrong!
cout<<"x is: "<< x;
return 0;
Also, x should be double or float.
____________________
Pedro Santos «» Project Y «» GameDevPT
[edited by - Pedro Santos on April 2, 2002 6:02:53 AM]
cout<<"x is: "<< x;
return 0;
Also, x should be double or float.
____________________
Pedro Santos «» Project Y «» GameDevPT
[edited by - Pedro Santos on April 2, 2002 6:02:53 AM]
try this:
[edited by - elis-cool on April 2, 2002 6:11:04 AM]
#include <iostream>using namespace std;int main(){ double x,a,t,U; int InputNum(); // whats this? cout << "Enter a:"; cin >> a; cout << "\nEnter t:"; cin >> t; cout << "\nEnter Uo:"; cin >> U; x = U*t+(1/2)*a*(t*t); cout << \n"X is:" << x;}
[edited by - elis-cool on April 2, 2002 6:11:04 AM]
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Dude! You''re doing integer division. 1/2 is always 0, no matter what. Either use 1.0/2 or use 0.5.
---visit #directxdev on afternet <- not just for directx, despite the name
Or were you meaning this:
[edited by - elis-cool on April 2, 2002 6:18:11 AM]
#include <iostream>using namespace std;double InputNum(); // Prototypeint main(){ double x; x = InputNum(); cout << "X is:" << x;}double InputNum(){ int x,a,t,U; cout << "Enter a:\n"; cin >> a; cout << "Enter t:\n"; cin >> t; cout << "Enter Uo:\n"; cin >> U; x = U*t+(1/2)*a*(t*t); return x;}
[edited by - elis-cool on April 2, 2002 6:18:11 AM]
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
sorry guys my copy&paste was wrong I wrote cout<<"X:"<rturn x;
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
Also what is double and what is float?Moreover Indirect X said that 1/2 = 0 but isn't 1/2 =0.5 and is there a difference betwwen 1/2 nad 1.0/2
[edited by - Mage_gr on April 2, 2002 6:58:22 AM]
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
Also what is double and what is float?Moreover Indirect X said that 1/2 = 0 but isn't 1/2 =0.5 and is there a difference betwwen 1/2 nad 1.0/2
[edited by - Mage_gr on April 2, 2002 6:58:22 AM]
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
quote: Original post by Mage_gr
Also what is double and what is float?
This is an RTFM type question. They are both types for storing floating-point numbers. Double has at least as much precision as float, usually more, often double.
quote:
Moreover Indirect X said that 1/2 = 0 but isn''t 1/2 =0.5
No. 1/2, as it is written in C++ is int 1 divided by int 2. The result will be an int. Since an int is not capable of storing the floating point value 0.5, the value gets rounded down to 0.
quote: is there a difference betwwen 1/2 nad 1.0/2
Yes. 1.0/2 is double 1.0 divided by int 2. The overall type of the expression is of type double, which *is* capable of storing the value 0.5.
Thanks IndirectX and Sabreman for explaining why 1/2 don''t work with C++. I was having simialr problems and just changed it to .5
Jeff D
Jeff D
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Thanks I got it now
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn''t remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn''t remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement