Set problem for people learning C ++,
Write a short program to simulate a ball falling out of a tower. To start, the user needs to ask the initial tower height in meters. Suppose that the normal weight (9.8 m / s2), and that the ball does not have an initial speed. Ask the program to display the height of the ball above the ground after 0, 1, 2, 3, 4, and 5 seconds. The ball must not be underground (height 0).
Before starting C ++, I had a reasonable, but above all self-learning, knowledge of Java. Therefore, looking at the problem, it seems that it should be divided into
- input class
- output class
- class of calculations
- Class of physical constants (recommended by the question installer)
- controller class ('main')
The input class asks the user for the initial height, which will be passed to the controller. The controller would give this and a few seconds (5) to the computation class, which would create an array of results and return it to the controller. The controller will pass an array of results to the output class, which will print them to the console.
I will put the actual code below, but it may not be needed.
You can probably already see the problem while trying to return an array. I do not ask how to get around this problem, it exists here and here . I ask, is the problem a result of poor design ? Should my program be structured differently for performance, maintenance, or style reasons so that I don't try to return an array similar to an object?
( , );
main.cpp
#include <iostream>
#include "dropSim.h"
using namespace std;
int main()
{
double height = getHeight();
int seconds = 5;
double* results = calculateResults(height, seconds);
outputResults(results);
return 0;
}
getHeight.cpp
#include <iostream>
using namespace std;
double getHeight()
{
cout << "What height should the experiment start at; ";
double height;
cin >> height;
return height;
}
calculateResults.cpp
#include "constants.h"
#include <cmath>
#include <iostream>
using namespace std;
double getPosition(double height, double time);
double* calculateResults(double height, int seconds)
{
double positions[seconds + 1];
for(int t = 0; t < seconds + 1; t++)
{
positions[t] = getPosition(height, t);
}
return positions;
}
double getPosition(double height, double time)
{
double position = height - 0.5*constants::gravity*pow(static_cast<double>(time), 2);
if( position < 0) position = 0;
return position;
}
outputResults.cpp
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void outputResults(double* results){
string outputText = "";
int numResults = sizeof(results)/sizeof(results[0]);
for(int t = 0; t < numResults; t++)
{
ostringstream line;
line << "After " << t << " seconds the height of the object is " << results[t] << "\r";
outputText.append(line.str());
}
cout << outputText;
}
, , ;
dropSim.h
#ifndef DROPSIM_H_
#define DROPSIM_H_
double getHeight();
double* calculateResults(double height, int seconds);
void outputResults(double* results);
#endif
constants.h
#ifndef CONSTANTS_H_
#define CONSTANTS_H_
namespace constants
{
const double gravity(9.81);
}
#endif