C/C++ Dificulty getting problems started in C++

  • Thread starter Thread starter i_wish_i_was_smart
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers around challenges faced in starting a C++ programming task related to calculating the moment MA due to a spring force on a circular bar. The user seeks guidance on structuring their program, expressing difficulty in determining the order of code execution and troubleshooting. Participants emphasize the importance of understanding the problem mechanics through hand calculations and suggest creating a flowchart or pseudocode to outline the program's structure before coding. This approach helps clarify input declarations, equations, and output formatting. They recommend calculating repeated values, like cos(a), only once to enhance efficiency. The conversation highlights the value of thorough documentation within the code to aid future understanding and debugging. Additionally, using tools like MathCAD for verification is mentioned as a beneficial practice. Overall, the discussion underscores the significance of planning and systematic problem-solving in programming.
i_wish_i_was_smart
Messages
91
Reaction score
1
if this is in the wrong forum i truly apologize but this is an engineering question and who better to answer but engineers, i have dificulty getting problems started in C++, i just don't know how to get a problem started, writing the rest of the program is easy but i can't diagnose it properly, any of you know what i mean?? anyways hee is my problem.
moment MA about A due to the force exherted by the spring on the circular bar B is given by MA=h Fx - d Fy
where
Fx = F cosβ
Fy = F sinβ
F = (20) (L-1)
L = [(3-h)ˆ2 + (4cosα)ˆ2]ˆ0.5
β = tanˆ-1[(3-h)/(4cosα)]
d = 4(1-cosα)
h = 4sinα
units force length N and m respectively
write a C++ program to do the following
compute values of MA fo angle a ranging from 30 - 60 degrees in intervals of 0.1 degrees
compute and display the maximum absolute value of MA and the corresponding value of angle a in degrees
can anyone help me get started please
http://memimage.cardomain.net/member_images/5/web/557000-557999/557901_38_full.jpg
picture reads from left to right
4m, 3m, k, angle α, B and A
thanks
 
Last edited by a moderator:
Technology news on Phys.org
Sounds pretty straight forward, a structured program. You are given all the equations you need as well as the input range..

*No matter what language I code in, I always start out by working out the mechanics of the problem. In this kind of program, even hand-calculate a few of the inputs (say the endpoint angles and midpoint). Then when I generate some output, I can verify that my output is correct.
*Next I draw a flow-chart of the program.. (The last C++ instructor I had said , to either draw a flowchart or you could write pseudo-code). It may seem like an added step, but really defines the flow of your program.. You will see ahead of time things you would miss if you jumped right into coding..
In a flowchart you would indicate inputs (declare variable types, define constants etc.), show equations, as well as branching or looping structures, and outputs..
here are a couple of refs that describe the art of flowcharting --->http://www.hci.com.au/hcisite2/toolkit/flowchar.htm
--->http://www.nos.org/htm/basic2.htm
you can pretty much code into any language from there.

*Next step is code in C++
*Be sure to include lots of comments (so you can understand what you did
a month from now)
*Compile and run your code, debug errors
*Verify that your output is correct (as i mentioned above)
*Put finishing touches on your output using formatting syntax.
*Even if not required I like to save a copy of my hand calculations worksheet, flowchart, code, input and output.

That should get you off and running...
 
Last edited by a moderator:
alright thx... there is one thing though, i am alright at determining what type of program i have to write, its the righting process i have trouble with, what code comes before another, it takes me too much time troubleshooting how to write my program... if you have any advice on how to effeciently write a program it would be greatly apreciated thx
 
How much formal CS have you taken? Engineers tend to think 'coding' is easy especially in a high level language but when you get to large, complex systems it is anything but.
 
this is just a first year engineering class here, everything is so compressed i barely learned anything, i only did 1 and 1/2 months of it, i am sure if i use it in my career i'll get better but right now its difficult for me
 
i_wish_i_was_smart,
I've sent you a personal message for my next help on this one..
 
i_wish_i_was_smart said:
alright thx... there is one thing though, i am alright at determining what type of program i have to write, its the righting process i have trouble with, what code comes before another, it takes me too much time troubleshooting how to write my program... if you have any advice on how to effeciently write a program it would be greatly apreciated thx

Not sure if you have read any of my personal messages to you, so will repeat here to see if you are receiving them all right.
Did you try making a flow chart of your problem? (or write pseudocode)?
While attempting to understand the problem, it is useful to do a hand calculation. This is like a simulation for what you will write using C++ code.

I made a hand caculation of the first input on your program. As I did this, I could see "one" order to list the equations in your program, that may be useful (They may differ from the order you find, but that's fine, there is more than one correct program).

Example: We need MA= hFx-dFy
but we don't have h, so h = 4sin(a) could be done before that.
For 'h', we don't have 'a' yet so we know that establishing the input (30-60, by 0.1 increments) must come before calculating h.
The other parameters were arranged using similar logic. Do you follow me?

After listing all the equations (and handcalculating) I noticed that one term occur several times. e.g. cos(a), This is a short program so it probably won't take very long to run. But if you also wanted to write more efficient code, you could calculate cos(a) all by itself [cos_a = cos(a)] and use the variable cos_a in place of cos(a) in the equations. That way the computer doesn't need to calculate cos(a) every time, it would just use the same value it calculated for cos_a, until 'a' changed to a new input value.

For a = 30 degrees, I found MA=92.38
(looking at the equation for MA, what units should that be in?) :rolleyes:
I don't know yet, if my solution is correct. My brain is not as efficient as a computer and could have made a mistake along the way.. If you try a hand calculation and get the same answer, there is a good chance, we both got it right. As you go thru the engineering program, you will find it is best to find a solutions to a problem more than one way. That will strengthen your belief, that you have the right answer.

Do you happen to have a program like MathCAD?
As you enter equations, it looks like real math equations, not like lines of program code. I am going to try this problem in MathCAD and if i get the same answer, that is a reasonable confirmation for the hand calculated output. Also in MathCAD once I have it working for one input, I can change the input value and it quickly finds the solutions for the rest of the equations.
 
I was just in the process of adding my 2 cents when I noticed that Quabache had already posted what I wanted to say. No matter how simple you suspect the code is going to be, definitely get into the habit of drawing up a flow chart. Once you have the flow chart drawn out to handle the problem correctly, writing the code for each section is generally a piece of cake. And definitely use comments liberally. It's an unpleasant feeling to pull something up 6 months later and find that you nearly have to rewrite the code because you couldn't remember what it is you were trying to do.
 

Similar threads

Replies
17
Views
2K
Replies
6
Views
8K
Replies
8
Views
3K
Replies
12
Views
2K
Replies
2
Views
4K
Replies
23
Views
3K
Back
Top