Jump to content

Could someone please tell me what's wrong with this code ?

M.M.S.
Go to solution Solved by fizzlesticks,

 

--snip--

Try not cout-ing anything besides the final answer, the program won't know how to parse your answer if you output "Enter height value".

Question is attached

#include <iostream>
#include <cmath>
using namespace std;

int main(int argc, char* argv[])
{

float g, H;
g = 10;
cout.setf(ios::fixed,ios::floatfield);
cout.precision(3);
cout << "Enter height value" << endl;
cin >> H;


if (H>=1)
cout<< 2 * g * H << endl;
else
cout << "invalid height" << endl;
return 0;
}

post-155468-0-06560900-1414763717_thumb.

Link to comment
Share on other sites

Link to post
Share on other sites

What language is it? :S
Looks similar to c++ but with alot of extra weird extra things

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

Question is attached

the formula to calculate the impact velocity is wrong: if you apply it to the sample input you get 300 m/s instead of 17.321

(sqrt(300) = 17.321)

 

 

What language is it? :S

Looks similar to c++ but with alot of extra weird extra things

that's standard c++

Link to comment
Share on other sites

Link to post
Share on other sites

Question is attached

#include <iostream>

#include <cmath>

using namespace std;

int main(int argc, char* argv[])

{

float g, H;

g = 10;

cout.setf(ios::fixed,ios::floatfield);

cout.precision(3);

cout << "Enter height value" << endl;

cin >> H;

if (H>=1)

cout<< 2 * g * H << endl;

else

cout << "invalid height" << endl;

return 0;

}

From what I know from physics, this formula is wrong. The right one is Δy(height) = (g * t^2) / 2. When you solve and find the time it takes to get to the ground, you should use that in this formula to calculate the velocity just a bit before the body hits the ground: v = g * t. Also, these formulas do not exactly apply in the real world, since there is air, which these equations do not take into account. But they should be pretty accurate if the height is not too high, otherwise you should take air resistance into account.

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

From what I know from physics, this formula is wrong. The right one is Δy(height) = (g * t^2) / 2. When you solve and find the time it takes to get to the ground, you should use that in this formula to calculate the velocity just a bit before the body hits the ground: v = g * t. Also, these formulas do not exactly apply in the real world, since there is air, which these equations do not take into account. But they should be pretty accurate if the height is not too high, otherwise you should take air resistance into account.

i don't think that this exercise wants you to take into account air resistance or pidgeons flying into you as you fall :wacko: anyway the formula he's using takes advantage of the conservation of mechanical energy, so the potential gravitational energy (e = m * g * h) translates into kinetic energy (e = 1/2 * m * v^2)

join the equations substituting e, solve for v and you get v = sqrt(2 * g * h)

Link to comment
Share on other sites

Link to post
Share on other sites

i don't think that this exercise wants you to take into account air resistance or pidgeons flying into you as you fall :wacko: anyway the formula he's using takes advantage of the conservation of mechanical energy, so the potential gravitational energy (e = m * g * h) translates into kinetic energy (e = 1/2 * m * v^2)

join the equations substituting e, solve for v and you get v = sqrt(2 * g * h)

guys i used the equation V^2= U^2 +2 g S 

knowing that V is final velocity 

U initial velocity 

g gravity 

S the height 

U = 0 

so v = sqrt (s g S) 

and yes i don't think he wants me to consider air resisitance , we're beginners lol 

Link to comment
Share on other sites

Link to post
Share on other sites

What language is it? :S

Looks similar to c++ but with alot of extra weird extra things

Yea C++

Link to comment
Share on other sites

Link to post
Share on other sites

the formula to calculate the impact velocity is wrong: if you apply it to the sample input you get 300 m/s instead of 17.321

(sqrt(300) = 17.321)

 

 

that's standard c++

 here the rest of the question 

he actually suggested using the same formula so what do you think ?post-155468-0-20875400-1414769309_thumb.

Link to comment
Share on other sites

Link to post
Share on other sites

 here the rest of the question 

he actually suggested using the same formula so what do you think ?attachicon.gifgsdgsdgsdg.png

so how do i make it calculate sqrt (2gh) ??

Link to comment
Share on other sites

Link to post
Share on other sites

 here the rest of the question 

he actually suggested using the same formula so what do you think ?

well he's using the formula i suggested, so you should use that one because it's correct and simple

if the program still doesn't spit out the correct result, then post again your code (using the code tag, please) and the output you get

Link to comment
Share on other sites

Link to post
Share on other sites

i don't think that this exercise wants you to take into account air resistance or pidgeons flying into you as you fall :wacko: anyway the formula he's using takes advantage of the conservation of mechanical energy, so the potential gravitational energy (e = m * g * h) translates into kinetic energy (e = 1/2 * m * v^2)

join the equations substituting e, solve for v and you get v = sqrt(2 * g * h)

Oh yeah just noticed it is an actual exercise, I thought he was just fiddling around :P And yeah the formula you wrote is correct and simple. OP should just use this. Now looking at his code, I think he should replace 

cout<< 2 * g * H << endl;

with 

cout << sqrt(2 * g * H) << endl;

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

 

Oh yeah just noticed it is an actual exercise, I thought he was just fiddling around :P And yeah the formula you wrote is correct and simple. OP should just use this. Now looking at his code, I think he should replace 

cout<< 2 * g * H << endl;

with 

cout << sqrt(2 * g * H) << endl;

I already did that , and still it doesn't work 

here's the code after fixing it :

#include <iostream>
#include <cmath>
using namespace std;
 
int main(int argc, char* argv[])
{
 
    float g, H;
    g = 10;
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(3);
    cout << "Enter height value" << endl;
    cin >> H;
 
 
if (H>=1)
        cout<< sqrt(2 * g * H) << endl;
else
    cout << "invalid height" << endl;
return 0;
}
Link to comment
Share on other sites

Link to post
Share on other sites

 

--snip--

Try not cout-ing anything besides the final answer, the program won't know how to parse your answer if you output "Enter height value".

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

I really didn't wanna give you the code directly ( cause there's nothing better than learning by yourself   -_-  ) but there's some weird stuff in your code that I don't fully understand so here's my accepted one try match it with yours to find out what's wrong  :)

 

#include <iostream>
#include <cmath>
#include <iomanip>
 
using namespace std;
 
int main()
{
    float h,x;
    float v;
    cin >>h;
    x=20*h;
    v = pow(x,0.5);
    std::cout << std::fixed;
    std::cout << std::setprecision(3);
    std::cout << v;
 
    return 0;
}
Link to comment
Share on other sites

Link to post
Share on other sites

Try not cout-ing anything besides the final answer, the program won't know how to parse your answer if you output "Enter height value".

Thank you , that was really the problem :) 

Link to comment
Share on other sites

Link to post
Share on other sites

 

I really didn't wanna give you the code directly ( cause there's nothing better thing than learning by yourself   -_-  ) but there's some weird stuff in your code that's I don't fully understand so here's my accepted one try match it with yours to find out what's wrong  :)

 

#include <iostream>
#include <cmath>
#include <iomanip>
 
using namespace std;
 
int main()
{
    float h,x;
    float v;
    cin >>h;
    x=20*h;
    v = pow(x,0.5);
    std::cout << std::fixed;
    std::cout << std::setprecision(3);
    std::cout << v;
 
    return 0;
}

 

3ala fekra ba2a ana ba2ali yomein ba7el fiha :D ana mesh ba5ali 7ad yektebli el code kollo :P 

el fekra enak esta5demt power ana esta5demt square root , kan el moshkela f eno mesh 3awezni akenni bakalem  user :l

thank you :)

Link to comment
Share on other sites

Link to post
Share on other sites

3ala fekra ba2a ana ba2ali yomein ba7el fiha :D ana mesh ba5ali 7ad yektebli el code kollo :P

el fekra enak esta5demt power ana esta5demt square root , kan el moshkela f eno mesh 3awezni akenni bakalem  user :l

thank you :)

 

mna msh fakr mwdo3 al squar wl power awi l2ni m7drtsh al lap bt3 al ACM aslan ana 3mlt al codes 3la al kont fakro mn course C++ kda kont wa5do fe 2 a3dady fa msh fakr kol 7aga   :D

 

BTW hyigi wa7d y3l2 3la al franko al bnktbo da  :lol:

Link to comment
Share on other sites

Link to post
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

×