How Should I Modify My C++ Code to Start Time Calculation from Zero?

  • Context: C/C++ 
  • Thread starter Thread starter physics=world
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary

Discussion Overview

The discussion revolves around modifying a C++ code snippet to adjust the time calculation so that it starts from zero and increments up to a user-defined total time. The focus is on coding techniques and logic adjustments within the context of programming and physics calculations.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a code snippet that calculates velocity and distance based on a total time input, but notes that the time starts from the user-defined value and decrements.
  • Another participant suggests using a separate variable to track time starting from zero, proposing to increment this variable instead of decrementing the input time.
  • A later reply seeks clarification on whether the input variable should be removed, indicating some confusion about the proposed changes.
  • The original poster acknowledges the suggestion and recognizes that the input variable "t" should not be used in the calculations as it is currently implemented.

Areas of Agreement / Disagreement

Participants express differing views on how to handle the input variable "t" and its role in the calculations. There is no consensus on the best approach, as some participants propose changes while others seek clarification.

Contextual Notes

The discussion does not resolve the specifics of how to implement the proposed changes, leaving open questions about the best coding practices in this scenario.

physics=world
Messages
109
Reaction score
0
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
count << "Enter the total time: ";
double t;
cin >> t;

count << "Enter the step-size: ";
double step;
cin >> step;

double steps = ceil(t/step);

const double a = 9.806; // accel. due to gravity // m/(sec^2)

for (int count = 0; count <= steps; count++) {


double v = (a*t); // calculation for velocity

const double half = 0.5;
double x = (half)*(a)*(pow(t, 2)); // calculation for distance

count << "t: " << setprecision(4) << fixed << t << "\tx: " << x << "\tv: " << v << endl;

t -= step;
}

return 0;This is the result I'm getting:

Enter the total time: 0.1
Enter the step-size: 0.01
t: 0.1000 x: 0.0490 v: 0.9806
t: 0.0900 x: 0.0397 v: 0.8825
t: 0.0800 x: 0.0314 v: 0.7845
t: 0.0700 x: 0.0240 v: 0.6864
t: 0.0600 x: 0.0177 v: 0.5884
t: 0.0500 x: 0.0123 v: 0.4903
t: 0.0400 x: 0.0078 v: 0.3922
t: 0.0300 x: 0.0044 v: 0.2942
t: 0.0200 x: 0.0020 v: 0.1961
t: 0.0100 x: 0.0005 v: 0.0981
t: 0.0000 x: 0.0000 v: 0.0000

___________________________________

What I am trying to do is to begin from zero and work my way down to 0.1000.
I still need to enter 0.1 for the total time (which is variable "t" in the code).

How would I code it in order to make it begin from zero?
 
Technology news on Phys.org
Hey physics=world.

You are setting your t as what you input. Instead what you should do is use another variable (say tstep), set it equal to zero and then instead of using t -= step you use tstep += step.

Take a look at your code again to see what's happening.
 
chiro said:
Hey physics=world.

You are setting your t as what you input. Instead what you should do is use another variable (say tstep), set it equal to zero and then instead of using t -= step you use tstep += step.

Take a look at your code again to see what's happening.

Are you saying get rid of my input "t"?
 
physics=world said:
Are you saying get rid of my input "t"?

I see what your talking about. The "t" in both equation is not suppose to be the value taken from the user.
Thanks for helping me! :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
4K