GeorgeIVV
GeorgeIVV

Reputation: 25

Equations In While Loops

This is what I am trying to do: Use the following formula to determine how many feet you will fall per second. Then, print out the elevation of you for each second you are in the air. The last line of output should be the first second that you are back on the ground.

D = ½ * g * t2 Assume earth’s gravity is 32 ft/sec2.

I'm having a lot of trouble with trying to use equations with while loops in my code. What I am trying to do is have an output like this for example:

What is the height?

120

Time Altitude

1........104

2........ 56

3........ 0

This is my code right now and I've tried many different things but nothing seems to work.

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

#define GRAVITY 32
int main() {
    int height, time = 1;
    double distance;

    printf("What is the height?\n");
    scanf("%d", &height);

    printf("Time \tAltitude\n");

    while (height > 0) {
        if (height > 0) {
            distance = .5 * 32 * pow(time, 2);
            printf("%d \t%d", time++, height - distance);
            time++;
        }
        else
            printf("%d \t0", time);
        break;
    }

    return 0;
}

Right now my output is always:

What is the height?

(Any number I enter)

Time Altitude

1......0

So for some reason it is not entering the first part of the while loop.

Upvotes: 0

Views: 2077

Answers (3)

3442
3442

Reputation: 8576

You have several issues...

  • You're not passing the correct formatting values to printf() in the while loop (use \t%f instead of \t%d).

  • You always break out of the while with a break, unconditionally, whatever happens it will break...

  • You don't change the value of height so that the while can exit at some point...

  • You're incrementing time twice!

  • For the purposes of fixing all the above, height shall have type double.

  • You're miss-using several operators and functions all over the place, mostly due to the above issues.

  • Several design issues (what's the purpose of GRAVITY, if you're not using it at all? Why don't you put '\n' at the end of printf()s?).

Now, given that, the final code may look like... (Note: not checking scanf(), but you should!)

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

#define GRAVITY 32

int main() {
    int time = 1;
    double distance = 0;

    printf("What is the height?\n");
    double height;
    scanf("%lf", &height);
    printf("Time\tHeight\n");

    while(height > (distance = 0.5 * GRAVITY * pow(time, 2)))
        printf("%d\t%012lf\n", time++, height - distance);

    printf("Impacted after %lf seconds\n", sqrt(height / (0.5 * GRAVITY)));
    return 0;
}

Edit: In petition of the OP, the first output line that always printed "Time: 0, height: input height" was removed.

Upvotes: 3

Sakib Ahammed
Sakib Ahammed

Reputation: 2480

For break statement, your loop is terminated for first run.

Your code is just simple modification. You can try this:

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

#define GRAVITY 32

int main() {
    int time = 0;
    int height;
    double distance;

    printf("What is the height?\n");
    scanf("%d", &height);
    printf("Time\tHeight\n");

    while(height > 0) {
        printf("%d\t%d\n", time++, height);
        distance = .5 * GRAVITY * pow(time, 2);
        height -= distance;
    }

    printf("%d\t0\n", time);
    return 0;
}

Upvotes: 1

rmpbklyn
rmpbklyn

Reputation: 21

  1. You did not initialize the height variable, assuming it will be zero by default therefore the while is never executed.
  2. If you did enter height, the program would never end because you are not resetting the value. It will only break when the height is 0 or less value.
  3. You don't need a while loop to do the calculation.
  4. If you want to run the program recursively then put the printf and scanf in a while loop, and have instructions to enter '-1' to exit.

Upvotes: -1

Related Questions