In: Computer Science
Complete the Vec class to make it similar to the vector.
sample code here.
#include
#include
#include
#include
#include
using namespace std;
int get_mode( vector vec )
{
int numbers[101] = {0};
for ( int e : vec ) numbers[e]++;
int greatest = 0, n = 0;
for ( int i = 0; i <= 100; i++ )
{
if ( numbers[i] > greatest )
{
greatest = numbers[i];
n = i;
}
}
return n;
}
const double g = 9.81;
const double k = 0.24;
double v( double m, double t )
{
return sqrt(m * g / k) * tanh( sqrt(k * g / m) * t );
}
double d( double m, double t )
{
return m / k * log( cosh(sqrt(k * g / m) * t) );
}
int main()
{
cout << "Please enter the skydiver mass: ";
double m;
cin >> m;
cout << "k = 0.24kg/m, m = 95.0kg, g = 9.81m/s^2" << endl;
cout << endl;
cout << "| Time | Velocity | Distance |" << endl;
for ( int t = 1; t <= 8; t++ )
{
cout << "| " << setw(4) << t << " | ";
cout << setw(8) << fixed << setprecision(2) << v(m, t) << " | ";
cout << setw(8) << fixed << setprecision(2) << d(m, t) << " |" << endl;
}
return 0;
}
The complete source code using the Vec class is given below:
#include <iostream>
#include<vector>
#include<math.h>
#include<iomanip>
using namespace std;
class Vec
{
//data member declaration
const double g = 9.81;
const double k = 0.24;
public:
//method to get the mode
int get_mode(vector<float> vec)
{
int numbers[101] = {0};
for ( int e : vec ) numbers[e]++;
int greatest = 0, n = 0;
for ( int i = 0; i <= 100; i++ )
{
if ( numbers[i] > greatest )
{
greatest = numbers[i];
n = i;
}
}
return n;
}
//method to get the velocity
double v( double m, double t )
{
return sqrt(m * g / k) * tanh( sqrt(k * g / m) * t );
}
//method to get the distnce
double d( double m, double t )
{
return m / k * log( cosh(sqrt(k * g / m) * t) );
}
};
int main()
{
//create object of class Vec
Vec v1;
//get user input
cout << "Please enter the skydiver mass: ";
double m;
cin >> m;
//display the k, m, and g
cout << "k = 0.24kg/m, m = 95.0kg, g = 9.81m/s^2" <<
endl;
cout << endl;
//display the table
cout << "| Time | Velocity | Distance |" << endl;
for ( int t = 1; t <= 8; t++ )
{
cout << "| " << setw(4) << t << " |
";
cout << setw(8) << fixed << setprecision(2)
<< v1.v(m, t) << " | ";
cout << setw(8) << fixed << setprecision(2)
<< v1.d(m, t) << " |" << endl;
}
return 0;
}
OUTPUT: