Can someone please help

IF you run this code you will understand what I am trying to say.
The two columns that are displayed when you run this code is distance and time. The time column needs to be reversed. Ascending rather than descending. I've attempted to modify the for statement with plethra of options and nothing seems to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

//***************************
// Function beforeSplat     *
// This function calculates *
// the time taken to fall.  *
//***************************

int beforeSplat(int distance)
{
    return (sqrt((2*distance)/9.8));
}

//*****************************
// Function printHeader       *
// This function displays     *
// the table for information. *
//*****************************

void printHeader(ofstream& Splat)
{
    Splat<<"  Distance \t     Time\n"
         <<"  (meters)\t  (seconds)\n"
         <<"~~~~~~~~~~~~~~~~~~~~~~~"
         <<endl;
}

//*****************************
// Function print             *
// This function displays     *
// the output of information. *
//*****************************

void print(int distance, int time, ofstream& Splat)
{
    Splat<<"|  "<<left<<setw(13)<<distance<<" "<<time<<"   |"<<endl;
}

//*****************************
// Function main              *
//*****************************

int main()
{
    ofstream Splat;
    Splat.open("Everest.out");
    int time;
    printHeader(Splat);

    for(int distance = 12000; distance >= 500; distance -=500)
    {
        time=beforeSplat(distance);
        print(distance, time, Splat);
    }
        Splat<<"~~~~~~~~~~~~~~~~~~~~~~~"
             <<"\n\n\n\t Everest.out has been saved to file.";
    Splat.close();
    return 0;
}
Just make the loop run the other way?
 
for(int distance = 500; distance <= 12000; distance +=500)
What variations have you tried?

Why would the time column require reversal?

Is it that you think it takes less time at 12000 vs 1000 meters? The shorter the distance, the less the time.

That said, if you insisted in reversal, then you're saying that at 500, the answer for 12000 should be displayed, for 1000 the answer for 11500.

12500 - distance

at 500, that would be 12000
at 1000 that would be 11500
at 1500 that would be 11000
at 12000 that would be 500

...reversing distance as applied to the 'beforeSplat' function....

time = beforeSplat( 12500 - distance );

Last edited on
Salem c, if I do what you suggested it changes both columns to opposite order(flips values).

Niccolo, I'm very green with C++, I've attempted to alter the for statement to different values trying to make it do what I want. No, it's not that I think it takes less time. It just makes more sense to have 12000 meters be equal to 10 seconds. IF one falls they won't be falling 12000 meters for 49 seconds... 11500 meters for 48 seconds... The logic of that doesn't make sense. Shouldn't it be 12000 meters 10 secs etc etc?
I don't quite understand what I am doing within my code. I've had a lot of help in getting it written. With that said, the 500 should be next to 49 seconds 12000 meters should be next to 10 seconds.

I hope that helps on understanding what I am asking for.
Last edited on
First, you haven't tried what I posted (in two threads to you)
...it works as you requested, but....

With that said, the 500 should be next to 49 seconds 12000 meters should be next to 49 seconds.


Read that, notice it makes no sense, and try that again.

Also, think, what is Time here?

Is it how long it takes to fall from a certain distance to the ground due to gravity?

If so, why do you think, as your other posts suggest, it takes less time at 12000 meters that it does at 500 meters?
Last edited on
Niccolo, how did you know to include 12500 in the time=beforesplat(12500-distance);?
My mistake, typo
You're misunderstanding the problem. One is on top of Everest, you fall, the fall is about 12000 meters. It makes sense that when one has 500 meters left of falling that also one has been falling for 49 seconds.
Ok, that makes a little more sense, it isn't the time from a particular height, it is the time at each sample toward the ground...ok, so....the reversal I proposed does that.

Now, as to how...well, it's a basic algebra problem.

You want the value of 12000 to equal the value from 500, 11500 to equal that of 1000

Consider, first, what happens with a simple reversal

12000 - distance...

Now, the chart would start at 12000, but the "distance left" is actually 0
Then, 11500 would become...500
11000 would be, 1000....

You see from this it is a tad oversimplified, but the parameter supplied to distance has now been
reversed....the first objective you stated....a reversal.

It changes "distance to go" to "distance we've gone...."

That is, at 12000 we've moved 0, at 11500 we've moved 500, etc....

However, in fact, this isn't an exact reversal of your chart. The chart clearly required that 12000 equate to the value given at 500....this offsets the chart by 500....so....12500 becomes the "initial" value for the reversal, thus 12500 - distance...

Not that I still fully agree here, but....

If Everest were 12500 meters tall, maybe....

Put another way, if Everest is 12000 meters, and the jump begins there, than AT 12000 meters the time traveled to that point (which would be the start) is 0.

Anyway....that's the idea with this reversal itself

What you call things affects how you think them.

There is clearly "total distance" (tot) from the peak to sea level.
There is the "distance that has been already travelled" (tra).
There is "remaining distance" (rem).
tot == tra + rem
rem == tot - tra
There is "time used to travel so far". Time to fall 'tra' meters.

Before the jump tra == 0. Before the jump rem == tot. Before the jump the time is 0.
There will be splash when tra == tot. There will be splash when rem == 0.

It is up to you what you do show:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
#include <cmath>

int main()
{
  using std::setw;
  std::cout << std::fixed;
  std::cout.precision(2);

  constexpr int tot {12000};
  for ( int tra = 0; tra <= tot; tra += 1000 )
    {
        int rem = tot - tra;
        std::cout << setw(6) << tra
                  << setw(6) << rem
                  << setw(8) << sqrt(tra) << '\n';
    }
}
CodeNovice01 wrote:
IF you run this code you will understand what I am trying to say

Nope.

Perhaps it would be best if you worked out the first few INTENDED outputs by hand (well, calculator anyway) and set them out in columns for us - then we might be able to infer what you want.


1
2
3
4
int beforeSplat(int distance)
{
    return (sqrt((2*distance)/9.8));
}

Well, make those int into double ... that's a C++ problem.

The more significant mathematics problem is that this formula (from rearranging one of the suvat eqns) gives you ... the time taken to travel distance FROM REST at acceleration g=9.8 m s-2. In other words, such a function would be better named timeFromStart, not beforeSplat.

If you want remaining time (who knows, you are far from clear) then maybe you want some combination of:
sqrt(2.0*heightOfEverest/g) is the total time to fall (g=9.81 in metre-second units)
sqrt(2.0*(heightOfEverest-currentHeight)/g) is the time from start to the current position
If you want the remaining time (again, who knows) then just subtract them.



PS. It really doesn't help if you start two threads on exactly the same problem.


PPS. Why don't you state the original question, not your interpretation of it.


PPPS. Everest is 8848 m high. Just saying.
Last edited on
Maybe, just maybe ...
CodeNovice01 wrote:
500 should be next to 49 seconds 12000 meters should be next to 10 seconds
CodeNovice01 wrote:
The time column needs to be reversed. Ascending rather than descending
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;


double timeFromStart( double  distance )               // <===== switched to double (and CHANGED NAME)
{
    return sqrt(2*distance/9.81);
}


void printHeader(ostream& Splat)                       // <===== ostream more general than ofstream
{
    Splat << "Height above ground   Time of fall\n"
          << "     (metres)          (seconds)  \n"
          << "----------------------------------\n";
}


void print(int ht, double time, ostream& Splat)
{
    Splat << "|  " << right << setw(10) << ht << "      " << right << setw(12) << (int)(time+0.5) <<"  |\n";
}



int main()
{
    const double heightOfEverest = 12500;             // <===== This is rubbish, but who cares
//  ofstream Splat;
//  Splat.open("Everest.out");
    ostream &Splat = cout;                            // <===== Just while we're playing

    printHeader(Splat);

    for(int h = heightOfEverest; h >= 500; h-=500)
    {
        double time = timeFromStart(heightOfEverest-h);
        print(h, time, Splat);
    }
        Splat<<"----------------------------------\n"
             <<"\n\n\t Everest.out has been saved to file.";
//  Splat.close();
//  return 0;
}


Height above ground   Time of fall
     (metres)          (seconds)
----------------------------------
|       12500                 0  |
|       12000                10  |
|       11500                14  |
|       11000                17  |
|       10500                20  |
|       10000                23  |
|        9500                25  |
|        9000                27  |
|        8500                29  |
|        8000                30  |
|        7500                32  |
|        7000                33  |
|        6500                35  |
|        6000                36  |
|        5500                38  |
|        5000                39  |
|        4500                40  |
|        4000                42  |
|        3500                43  |
|        3000                44  |
|        2500                45  |
|        2000                46  |
|        1500                47  |
|        1000                48  |
|         500                49  |
----------------------------------

Last edited on
we're playing

Yep, in a playground with no air to effectuate some influence on free fall.

but who cares

... and acceleration changes also -- about 0.02% in this case ;)
https://upload.wikimedia.org/wikipedia/commons/5/50/EarthGravityPREM.svg
Values are made up, bogus and simplified, like in most homework.
That is somewhat separate from the issue that the OP has with logic.

@CodeNovice01: are you still in the free fall or have you found solid ground (of thought)?
Topic archived. No new replies allowed.