hi,
recently i have a big home work for final, and it has include all stuffes I have learn over a semeter ( my first c++ course).
I write a complete working program, but my define classes like struct because the classes must have members in public, otherwise it won't work.
However, homework requires that class members of the first class (Altitude) to be set as private or protected.
can anyone help me to work around with this. I am very confuse with classes. The code below is very long. be kind to me and read all through.
Thank
**************************
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
ofstream out; //global input/output variable (it is good in this case because
ifstream in; //only function opening and print access to it)
class Altitude //contains original datas of time and altitude
{
public:
Altitude();
double time[26]; //original datas from input file store in here
double alti[26]; //
};
class Velocity: public Altitude //inherrited from class Altitude
{
public:
Velocity();// constructor
double velo[26]; //contains data points of velocity
double alti_V[26]; // storage data points of altitude calculated intergrate of velocity
};
class Accelerationublic Altitude // inherritted from class Altitude
{
public:
Acceleration(); //constructor
double acce[26];
double velo_A[26]; // storage data points of velocity calculated from acceleration
};
void openingfile(); //function opens input file and stores the datas to arrays
void computeVelo(double time[] ,double alti[], double velo[]); // function calculates velocity
void computeAcce(double time[], double alti[], double velo[], double acce[]); //function calculates acceleration
void acce_Velo_Cal(double velo[], double acce[], double velo_A[], double alti_V[], double ti[]); //function intergrates acceleration
//and intergrates velocity
void print(double time[], double alti[], double acce[], double velo_A[], double velo[], double alti_V[]);
void main()
{
int i = 0;
Altitude ti;
Velocity ve;
Acceleration ac;
openingfile(); //function open input file
while(!in.eof())
{
in>>ti.time[i]>>ti.alti[i];
i = i+1;
}
//functions call
computeVelo(ti.time , ti.alti, ve.velo);
computeAcce(ti.time, ti.alti, ve.velo, ac.acce);
acce_Velo_Cal(ve.velo, ac.acce, ac.velo_A, ve.alti_V, ti.time);
print(ti.time, ti.alti, ac.acce, ac.velo_A, ve.velo, ve.alti_V);
in.close();
out.close();
return;
}
//function open the input file
void openingfile()
{
char *fname; // a dynamic pointer
fname = new char[20]; //allocated memory when exe
cout<<"******************************************* *************************************"<<endl;
cout<<"************************ ENGINEERING 50 FINAL PROJECT***************************\n\n"<<endl;
cout<<"This program will read a data table with two columes of time Vs altitude "
<<"from a file. It would prompt user to provide the input and output"
<<"file names. After that the program will calculate the acceleration,"
<<"and velocity by numerical method. Thus, it regenerates from calculated "
<<"acceleration to get velocity and from velocity to get altitude. As a "
<<"result,the output file contain final datas that let engineers to interpret"
<<"the precision or accuracy between forward and backward programing. Last,"
<<"The comparision will be plotted using Excel and Matlab software.\n"<<endl;
cout<<"\nPlease enter the input file name \n(need including the file extention): ";
cin>>fname;
in.open(fname);
if (in.fail()) //check the input process
{
cout<<"\nInput file opening failed.\n";
exit(1);
}
delete []fname; //free the memory
return;
}
//function print out the results of calculation
void print (double time[], double alti[], double acce[], double velo_A[], double velo[], double alti_V[])
{
char *gname; //another pointer
gname = new char[20]; //and memory allocated
out<<fixed<<showpoint<<setprecision(2);
cout<<"\nPlease enter the output file name, \n(need including the file extention): ";
cin>>gname;
out.open(gname);
if (out.fail())
{
cout<<"\nOutput file opening failed.\n";
exit(1);
}
out<<setw(7)<<"Time (s)"<<setw(13)<<"Alti (ft)"<<setw(15)<<"Velo (ft/sec)"
<<setw(17)<<"Acce (ft/sec)"<<setw(20)<<"Intgr_A (ft/sec)"<<setw(20)<<"Intgr_V (ft/sec)"<<endl;
out<<"******************************************** ***********************************************"<< endl;
out<<"\n"<<endl;
for (int i = 0; i<26;i++)
{
out<<setw(6)<<time[i]<<setw(14)<<alti[i]<<setw(14)<<velo[i]<<setw(14)<<acce[i]
<<setw(20)<<velo_A[i]<<setw(20)<<alti_V[i]<<endl;
}
delete []gname;
return;
}
Altitude::Altitude() //intialize all array adresses to zero
{
double time={0.0};
double alti={0.0};
}
Velocity::Velocity()
{
double velo={0.0};
double alti_V={0.0};
}
Acceleration::Acceleration()
{
double acce = {0.0};
double velo_A = {0.0};
}
void computeVelo(double time[26] , double alti[26], double velo[26])
{
double *delta;
delta = new double;
velo[0]=0.0; //initial velocity is zero
for(int i=1;i<=24;i++)
{
*delta = (time[i] - time[i-1]);
velo[i] = (alti[i+1] - alti[i-1])/(2*(*delta)); //the rest of data point using formula
// ( x(i+1) - x(i-1)) / (2 * delta x)
}
velo[25] = (alti[25]-alti[25-1])/(*delta); //last data point using formular : (xi - x(i-1))/delta x
cout<<"Velocity is calculated!"<<endl;
delete delta;
return ;
}
//Function compute acceleration
void computeAcce(double ti[],double al[],double velo[],double acce[])
{
double *delta;
delta = new double;
acce[0]=0.0; //initial acceleration = 0
for(int i=1;i<=24;i++)
{
*delta = (ti[i] - ti[i-1]); //delta x = x2-x1
acce[i]=(al[i-1]-(2*al[i])+al[i+1])/(pow(*delta,2)); //acceleration = ( Y(i-1)-2*Y(i)
// + Y(i+1) )/(square of delta x)
}
acce[25]= (velo[i]-velo[i-1])/(10); //constant delta = 10 use for last point
cout<<"Acceleration is calculated!"<<endl;
delete delta;
return ;
}
void acce_Velo_Cal(double velo[], double acce[], double velo_A[], double alti_V[], double ti[])
{
int j=0 ; //since delta is constant and = 10 in this case
double *delta;
delta = new double;
velo_A[0] = 0.0;
for(int i=1;i<26;i++)
{
*delta = ti[i]-ti[i-1];
velo_A[i]= velo_A[i-1] + *delta*acce[i]; //sum up of area before and it's value to get
// point by point velocity.
}
cout<<"Intergration of acceleration is calculated!"<<endl;
alti_V[0] = 60.0;
for (j = 1; j<26; j++)
{
*delta = ti[j]-ti[j-1];
alti_V[j]= alti_V[j-1] + velo[j]*(*delta); //sum of area before and its value to get point
// by point altitude.
}
cout<<"Intergration of velocity is calculated!"<<endl;
delete delta;
return ;
}