Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The question states:

A 3000 lb vehicle travelling on a road at 100 ft/sec requires a force equal to its mass times its acceleration to stop ( F = ma).
The acceleration is given by
a= (v_o^2-v_i^2)/2d

Where vi = 100 ft/sec
vo = 0 ft/sec.
Write a program that will calculate the retarding force needed to stop the car at distances of 10, 20, 30, 40, 50, …, 1000 ft. Display the distance value and the corresponding retarding force in tabular form. The units of force are Newtons (N). Your solution must include a user-defined function that returns the force and takes as input initial and final velocity and distance.
double force(double velocityInitial, double velocityFinal, double distance, double mass);

Not sure how to fix the errors i got which are:

26: 'mass' is used uninitialized in this function
26: 'acceleration' is used uninitialized in this function
30: 'distance' is is used uninitialized in this function

What I have tried:

My code is:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double force(double velocityInitial, double velocityFinal, double distance, double mass);

int main()
{

double forceFormula;
int distance, mass, acceleration;

forceFormula = mass * acceleration;

printf("\nDistance:\t\tForce in Newtons:\n"
"------------------------------------------\n");
printf("%d\t\t\t%.2lf\n", distance, forceFormula);
printf("------------------------------------------\n");



return 0;
}


double force(double velocityInitial, double velocityFinal, double distance, double mass)
{
velocityInitial = 100;
velocityFinal = 0;
mass = 3000;

double acceleration, forceFormula;



for( distance = 10; distance <= 1000; distance += 10)
{

acceleration = ((velocityFinal * velocityFinal) - (velocityInitial * velocityInitial))/ 2 * distance;

forceFormula = mass * acceleration;
}

return forceFormula;

}
Posted
Updated 18-Mar-20 2:30am

This is your fourth question, and the third today: all with the same problem.

And that's simple: you aren't thinking, you're trying to get others to do that for you. And that's a bad idea because you don't learn by looking at others work - you learn by doing something for yourself.

So, I'm not going to tell you what code you need to fix: instead, I'm going to point you at a permanent solution. Google for the name of your C system (probably an IDE) and the word "debugger" and start reading how to use it. The debugger will enable to you watch what your code is doing while it is running, looking at variable & memory contents and where flow control is actually going - and why. It's the best tool in your box, so get used to using it - it's where you are going to spend a lot of your development time.

So get yourself to Google, and start using the debugger.
 
Share this answer
 
Comments
Patrice T 15-Mar-20 23:02pm    
Looks like comment in S2 is also for you.
Quote:
Need help fixing my code in C.

No ! There is nothing to fix because you didn't stated any problem.
You obviously have learned nothing from the 3 previous questions.

When you ask for help, you have to say what is your problem, giving complete error messages is the minimum.
If you read carefully the error messages, you will see that the compiler don't just say 'you have an error ! na nana na ...", The compiler try to tell you the kind of error the best it can and it also gives you a position. This should narrow the search for a solution.

You need to learn how to exploit those error messages, search on documentation for details on the error.

I guess you will have to learn debugger pretty soon too. It is incredible helpful to understand what is unexpected in your code running.

[Update]
Quote:
I'm not sure what it was about the question that made you guys angry

You are not listening the comments done.
Quote:
I guess I'm not the only one that doesn't like C as much.

That is why not everyone have a C compiler on its computer and if have 1 installed, it is not necessary the same as yours.
Quote:
I'm not sure where your confusion lies when you say that "there is nothing to fix because you didn't stated any problem."

No confusion, sarcasm because your didn't stated a problem, so, no problem, no fix. :)

you finally added the error messages, 26 and 30 are the lines numbers where errors are. Problem, in your code the lines with the errors are 13 and 17.
This means that the errors comes from another program, they don't match.
This will not motivate helpers.
Quote:
I am confused as to what the errors even mean, or how to even fix them.

Google is your friend, paste the compiler name and error message, you will get a lot of explanations.

Advice: do not learn by solving problems, find a tutorial and follow it, do exercises, experiment, this way you will learn the basics.
 
Share this answer
 
v2
Comments
Member 14770192 15-Mar-20 19:06pm    
Thank you guys for responding. I'm not sure what it was about the question that made you guys angry, I guess I'm not the only one that doesn't like C as much. Regarding Griff's statement that I am not thinking and trying to get others to do it for me I would argue that that's wrong since I am doing the code myself and posting what I have done along with the errors to see if you guys, with a lot more experience than me, can detect and help me fix. If I wanted to get someone to do it for me I would not even attempt them, I would just pay someone to do them, but I am not. I appreciate the advice Griff on using a debugger, I will follow up on that since I do think it will make it even easier for me to understand what the program is doing. Practice, I appreciate you helping me with a problem earlier on today I am grateful for the time and patience you had while helping me. I'm not sure where your confusion lies when you say that "there is nothing to fix because you didn't stated any problem." I clearly wrote on the 12th line of the body of the question: "Not sure how to fix the errors i got which are:" I am confused as to what the errors even mean, or how to even fix them. Anyways I appreciate the fact that you guys help people like myself who are learning coding.
OriginalGriff 16-Mar-20 4:07am    
You aren't going to learn to code by throwing code together and panicking when it doesn't work. Asking other people to fix every trivial problem with your code isn't a viable development strategy, it's a route to total failure - because as projects get more and more complex, teh code volume gets grows commensurately. And the tasks you are working on at the moment are deliberately trivial to get you into the mindset of fixing your code - if you don't learn how to do that on trivial examples, you will be completely overwhelmed when you try to produce a 100,000 line-of-code "normal app".
Think of it as driving a car: you start with an instructor in a little gutless thing on quiet roads so you can learn the reflexes and skills you need in relative safety. If you don't learn them there, buy get a chauffeur to do that bit for you then when you jump in the Ferrari and roar off, you are going to die in very short order, and probably take a couple of dozen others with you...
It's the same thing: driving and coding are skills; you develop them by using them, not watching someone else do it!

So start thinking about your problems and seeing what solutions you can come up with instead of relying on others to get you out of a hole!
Stefan_Lang 16-Mar-20 5:58am    
...the compiler don't just say 'you have an error ! na nana na ..."

Now I wonder if there *is* a compiler like that - it would have some entertaining value ;-)
Patrice T 16-Mar-20 6:04am    
lol
You should love C, instead of disliking it. :-)
Try
C
#include <stdio.h>


double force( double vo, double vi, double d, double m);

int main()
{
  const double M = 3000;
  const double Vo = 100;
  const double Vi = 0;

  printf("\nDistance:\t\tForce in Newtons:\n");

  for (double d = 10.0 ; d < 1010.0; d+=10.0)
  {
    double f = force( Vo, Vi, d, M);
    printf("\t%4.0f\t\t%7.3f\n", d, f);
  }
  return 0;
}

double force( double vo, double vi, double d, double m)
{
  double f = (vo*vo-vi*vi)/2.0/d;
  return f;
}
 
Share this answer
 
1. Try harder when specifying the topic name. Everyone posting a qquestion here needs help. That is not useful information.

2. Try harder understanding your compiler. The messages it sent you are clear enough and not hard to understand, or fix. Think! Look up the documentation of the compiler if needed.

3. Learn the basics. Really, the things you have issues with are at the very bottom of programming in general, and C specifically. It doesn't make sense for me or anyone else to provide you with a specific answer to this program - what you need is learn and understand the raw basics of programming! Understanding requires thinking, no one else can do that for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900