passable?

Pages: 12
Lines 45 and 49 do not need the "const"


They don't but good practice is for variables that don't/shouldn't change to be defined as const. The OP asked for comments, and that is one of mine.
@seeplus,

Good point. I must have been thing about a different program at the time.

Andy
Hello, now I have completed gave up on this one.

My math understanding is at low of low. This is the answer from quiz. I tried to write it myself. But I realized this is too complicated. There are something that I don't understand.

Write a short program to simulate a ball being dropped off of a tower. To start, the user should be asked for the height of the tower in meters. Assume normal gravity (9.8 m/s2), and that the ball has no initial velocity (the ball is not moving to start). Have the program output the height of the ball above the ground after 0, 1, 2, 3, 4, and 5 seconds. The ball should not go underneath the ground (height 0).

Your program should include a header file named constants.h that contains a symbolic constant to hold the value of gravity (9.8).

Use a function to calculate the height of the ball after x seconds. The function can calculate how far the ball has fallen after x seconds using the following formula: distance fallen = gravity_constant * x_seconds2 / 2

Sample output:

Enter the height of the tower in meters: 100
At 0 seconds, the ball is at height: 100 meters
At 1 seconds, the ball is at height: 95.1 meters
At 2 seconds, the ball is at height: 80.4 meters
At 3 seconds, the ball is at height: 55.9 meters
At 4 seconds, the ball is at height: 21.6 meters
At 5 seconds, the ball is on the ground.

Note: Depending on the height of the tower, the ball may not reach the ground in 5 seconds -- that’s okay. We’ll improve this program once we’ve covered loops.
Note: The ^ symbol isn’t an exponent in C++. Implement the formula using multiplication instead of exponentiation.
Note: Remember to use double literals for doubles, eg. 2.0 rather than 2.

This is answer which is what I'm suppose to do.

To start with.

line 18. towerheight - distancefallen what exact does that do? where did it lead to or come from?

line 14 and 20. I didnt know you could have parameter/argument and return too.

line 20 the return, where does return goes to? I know parameter/argument return to line 34. But return??

also direction said to make sure it start at 0?

Im confused...

this one is header

1
2
3
4
5
6
#ifndef CONSTANTS_H
#define CONSTANTS_H
 
constexpr double gravity { 9.8 }; // in meters/second squared
 
#endif 


this is main

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
#include <iostream>
#include "constants.h"
 
// gets height from user and returns it
double getTowerHeight()
{
	std::cout << "Enter the height of the tower in meters: ";
	double towerHeight{};
	std::cin >> towerHeight;
	return towerHeight;
}
 
// Returns height from ground after "seconds" seconds
double calculateHeight(double towerHeight, int seconds)
{
	// Using formula: [ s = u * t + (a * t^2) / 2 ], here u(initial velocity) = 0
	double distanceFallen { (gravity * (seconds * seconds)) / 2.0 };
	double currentHeight { towerHeight - distanceFallen };
 
	return currentHeight;
}
 
// Prints height every second till ball has reached the ground
void printHeight(double height, int seconds)
{
	if (height > 0.0)
		std::cout << "At " << seconds << " seconds, the ball is at height: " << height << " meters\n";
	else
		std::cout << "At " << seconds << " seconds, the ball is on the ground.\n";
}
 
void calculateAndPrintHeight(double towerHeight, int seconds)
{
	double height { calculateHeight(towerHeight, seconds) };
	printHeight(height, seconds);
}
 
int main()
{
	const double towerHeight { getTowerHeight() };
 
	calculateAndPrintHeight(towerHeight, 0);
	calculateAndPrintHeight(towerHeight, 1);
	calculateAndPrintHeight(towerHeight, 2);
	calculateAndPrintHeight(towerHeight, 3);
	calculateAndPrintHeight(towerHeight, 4);
	calculateAndPrintHeight(towerHeight, 5);
 
	return 0;
}
Last edited on
1. towerheight - distancefallen is the height of the ball above the ground.
2. parameter etc - live and learn
3. return transfers control back to the place in the program where it was called from, in this case main()
4. g is simply in a separate header file called constant.h by the look of it which needs to be included
towerheight - distancefallen what exact does that do? where did it lead to or come from?

An object starts at height A. It falls a distance B. What height will it be at now?

line 14 and 20. I didnt know you could have parameter/argument and return too.

Then you've not been bothering to read the things people have told you, because you've been told this several times - including by me in http://www.cplusplus.com/forum/beginner/273606/ .

line 20 the return, where does return goes to? I know parameter/argument return to line 34. But return??


The argument does NOT go back to line 34.

The return value is passed back to line 34.

Did you read the tutorial on functions I linked you to in one of your previous threads?
Last edited on
I did read, it may didn't stick in me. I'll have to read again. I have to do thing over and over to memorized in my head.
LOL
The example is from learn.cpp if you don’t already know.
I'll stop posting, didn't mean to waste everybody's time. I'll reread the chapter.

Thank for the help
Hello soulworld05,

If you have finished the original question edit your OP and change the icon to the green check.

This new program should have its own thread so a not to be missed or otherwise obscured be the original thread.

You are not the first person to post this problem, at the moment I am not sure what would be best to search for, but it would be worth a little time.

In "main" you have 6 lines that call the same function with a different 2nd parameter. This can be done in a for loop. At least to start with.

Andy
@soulworld05: Awesome job on the first problem. It's a massive improvement. Very clear and easy to read. Dude, well done.

This second problem is sending up some red flag warnings: was that a live quiz that the teacher gave that on? What was the time limit they gave you? It seems the teacher expects his students to have taken some basic physics classes before reaching him, and it worries me that he might make some assumptions about the math levels as well.

Again, I recommend you go to his office hour and talk to him. He might genuinely be unaware that his students are taking his classes ahead of physics and higher maths. Don't be confrontational about it, but let him know your own situation, ask what classes he expects his students to take before this class. It might be a recurring theme in his class that he wants to tie in other classes into his own.

There have been teachers that I've talked to a couple of times and decided to drop their classes because they expect you to have knowledge that I just did not have yet. It didn't seem fair to be graded against classes that I'd never taken before which had information not covered in class. There are some stubborn tenured teachers out there that are worth avoiding or at least waiting till later before taking their classes. I hope he just made a mistake on presenting that problem on a quiz. Otherwise, it sucks, but if it's early enough you could get your money back, or at least get the class off your GPO.

Or you can struggle through, I've put in extra effort to learn the side topic as well as the class material. That's doable so long as he doesn't do this sort of thing on live-timed quizes.
Last edited on
What a load of drivel
Hello againtry. Do you think this teacher is being fair on this second problem or not? I'd be genuinely worried if this problem came up on a real-time quiz.
Last edited on
I appreciate the link, I was also able to find similar info. It's expressed clearly in the answer that the teacher provided for the quiz after the fact (line 16). But if this is a timed quiz, no internet allowed, what the heck would you do? What do you recommend the OP do at this point if they haven't taken the other classes yet?

I haven't taken classes since a year or two before lockdowns started, so I don't know how a 'quiz' is defined these days, but this is seriously worrying.

Oh, sorry for high jacking the topic a bit, I'm going a bit meta on the class as a whole. Does this one problem forecast a bad class for OP going foreword?

Hello, now I have completed gave up on this one.

My math understanding is at low of low. This is the answer from quiz. I tried to write it myself. But I realized this is too complicated. There are something that I don't understand.


I want to say "hang in there" especially after you put in so much work on the first assignment, but I personally went after all the computer courses I could my first couple semesters of college and ignored physics and math, and that was not a good experience. So I'm just putting out there that timing of when you take a class is important, your teacher might recommend that this semester is not a good point to start this class. He could go on to let you know which classes are recommended before tackling his class again. There's no shame in saying this is overwhelming, and that you will come back in a year or so when you are better prepared. Talk to your teacher to find out if this is the case or not. One ten minute conversation could save you months of pain and misery... Something I wish I had known earlier.
Last edited on
Dry up @newbieg. Your unsolicited, ill-informed and potentially dangerous advice on how a student and their teacher conduct themselves is completely off topic. Turn me pink as much as you like. It won’t change the fact that you would be better of concentrating on the programming aspects rather than indulging in writing the sort of lame piffle your posts have been so far.

Yes, you are hijacking, and it’s of absolutely no value. Stick to your equally lame videos.
Anyway, I'm hoping he made a simple mistake in using that problem on a quiz.
And how is that constructive? The quiz is at learncpp.com
Beyond asking questions on this or that forum which are both open to all-comers, there is no teacher or college, dope.
I love you againtry, you are just too fun.
I actually don't know who's been reporting you. Don't expect you to believe me, but figured I'd mention it.
Thank you for pointing out this was not a college class, I was seriously concerned for OP, having to drop a class that you actually care about is heart-wrenching.
Have a great night.
Last edited on
This is simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
	const double g {9.8};
	double th {};
	int t {};

	std::cout << "Height of tower: ";
	std::cin >> th;

	for (double h {}; (h = g * t * t / 2) <= th; ++t)
		std::cout << "At " << t << " seconds, height is " << th - h << '\n';

	std::cout << "At " << t << " seconds, the ball is on the ground\n";
}


which displays:


Height of tower: 100
At 0 seconds, height is 100
At 1 seconds, height is 95.1
At 2 seconds, height is 80.4
At 3 seconds, height is 55.9
At 4 seconds, height is 21.6
At 5 seconds, the ball is on the ground

Topic archived. No new replies allowed.
Pages: 12