stackoverflow. I’m a newbie at C++ and I’ve got one last issue with my assignment. I’m trying to write a program that calculates the speed at which an object falls from a base height, and displays that information as the height of the object versus the amount of time (in seconds) that it has been falling. This is the code I have so far:
#include <stdio.h>
int main() {
int acceleration, altitude, time;
double distance;
acceleration = 32;
time = 0;
printf("What is the altitude you are dropping your object from?\n");
scanf("%d", &altitude);
printf("Time Altitude\n");
while (altitude > 0){
distance = ((0.5 * acceleration) * (time * time));
altitude = altitude - distance;
printf("%d %d\n", time, altitude);
time++;
if (altitude <= 0){
altitude = 0;
}
}
return 0;
}
I know the equation for distance is slightly off, but what I’m more concerned about at the moment is that the program does not print out an altitude of 0 when the object hits the ground. Instead, it prints out -104, and since negative distance isn’t achievable, I’d like to fix this.
So my question is this: what is wrong with my while loop/ nested if loop that is causing the program to not print out 0 for the final entry in the table?
Alter the altitude before printing.