Hello again. I have yet another hurdle with my program. I have to incorperate a
ID: 3547620 • Letter: H
Question
Hello again. I have yet another hurdle with my program. I have to incorperate a function that displays the average of a chosen array. Here is a short version of the program:
#include <iostream> //pre-processor directs - iostream talks to screen
#include <fstream> //pre-processor directs - allows accessing and inputing to data files
#include <string> //pre-processor directs - enables string class functions
#include <cmath> //pre-processor directs - allows math functions
const int size=30;
using namespace std; //introduces namespace std
void velcFts (double [], double [], double []);
void velcMph (double[], double[]);
void ArrayMean (double[], double, double);
int main ()
{
int i=0;
ifstream inFile;
double time[size], dist[size], velcf[size]={0.0}, velcm[size]={0.0}, avgv;
inFile.open("CAR1a.txt");
while (!inFile.eof()) //Reads the rest of the file into arrays time[i] and dist[i]
{
inFile>>time[i]>>dist[i];
i++;
}
velcFts (time, dist, velcf);
velcMph (velcf, velcm);
ArrayMean (velcm, size, avgv);
cout<<"Average velocity = "<<avgv<<" mph"<<endl;
for(int k=0; k<i; k++)
cout<<time[k]<<", "<<dist[k]<<", "<<velcf[k]<<", "<<velcm[k]<<", "<<endl;
inFile.close();
system("pause");
return 0;
}
//******************Velocity (ft/s) function definition*************************
void velcFts (double time[size], double dist[size], double velcf[size])
{
for (int i=1;i<size;i++) //initiates, defines and sets counter
{
velcf[i] = (dist[i]-dist[i-1])/(time[i]-time[i-1]);
}
}
//******************Velocity (Mph) function definition*************************
void velcMph (double velcf[size], double velcm[size])
{
for (int i=0;i<size;i++) //initiates, defines and sets counter
{
velcm[i] = (velcf[i]*0.681818182);
}
}
//*******************Average funtion definition*********************************
void ArrayMean (double a[size], double size, double avg)
{
double sum=0;
for (int i=0;i<size;i++)
{
sum=sum+a[i];
avg=sum/size;
}
}
This program runs with no errors, but the value in cout<<"Average velocity = "<<avgv<<" mph"<<endl; is always 0. Any help would be greatly appreciated.
Explanation / Answer
#include <iostream> //pre-processor directs - iostream talks to screen
#include <fstream> //pre-processor directs - allows accessing and inputing to data files
#include <string> //pre-processor directs - enables string class functions
#include <cmath> //pre-processor directs - allows math functions
const int size=30;
using namespace std; //introduces namespace std
void velcFts (double [], double [], double []);
void velcMph (double[], double[]);
void ArrayMean (double[], double, double&);
int main ()
{
int i=0;
ifstream inFile;
double time[size], dist[size], velcf[size]={0.0}, velcm[size]={0.0}, avgv;
inFile.open("input.txt");
while (!inFile.eof()) //Reads the rest of the file into arrays time[i] and dist[i]
{
inFile>>time[i]>>dist[i];
i++;
}
velcFts (time, dist, velcf);
velcMph (velcf, velcm);
ArrayMean (velcm, i, avgv);
cout<<"Average velocity = "<<avgv<<" mph"<<endl;
for(int k=0; k<i; k++)
cout<<time[k]<<", "<<dist[k]<<", "<<velcf[k]<<", "<<velcm[k]<<", "<<endl;
inFile.close();
system("pause");
return 0;
}
//******************Velocity (ft/s) function definition*************************
void velcFts (double time[size], double dist[size], double velcf[size])
{
for (int i=1;i<size;i++) //initiates, defines and sets counter
{
velcf[i] = (dist[i]-dist[i-1])/(time[i]-time[i-1]);
}
}
//******************Velocity (Mph) function definition*************************
void velcMph (double velcf[size], double velcm[size])
{
for (int i=0;i<size;i++) //initiates, defines and sets counter
{
velcm[i] = (velcf[i]*0.681818182);
}
}
//*******************Average funtion definition*********************************
void ArrayMean (double a[size], double size, double& avg)
{
double sum=0;
for (int i=0;i<size;i++)
{
sum=sum+a[i];
}
avg=sum/size;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.