In: Computer Science
Write a program that calculates the average of upto 100 English distances input by the user. Create an array of objects of the Distance class, as in the ENGLARAY example in this chapter. To calculate the average, you can borrow the add_dist() member function from the ENGLCON example in Chapter 6. You’ll also need a member function that divides a Distance value by an integer. Here’s one possibility:
void Distance::div_dist(Distance d2, int divisor) {
float fltfeet = d2.feet + d2.inches/12.0; fltfeet /=
divisor;
feet = int(fltfeet);
inches = (fltfeet-feet) * 12.0;
}
the code is
#include<iostream>
#include<string>
#include<iomanip>
#include<conio.h>
using namespace std;
class Distance1{
int feet; float inches;
public:
Distance1(): feet(0), inches(0) {}
void getdist(){
cout<<"\nEnter feet: "; cin>>feet;
cout<<"Enter inches: "; cin>>inches;}
void add_dist(Distance1 d2, Distance1 d3);
void div_dist(Distance1 d2, int divisor);
void showdist() const { cout<<feet<<"\'-"<<inches<<'\"';}};
void Distance1::add_dist(Distance1 d2, Distance1 d3)
{
inches=d2.inches+d3.inches; feet=0;
if(inches>=12.0){ inches -= 12.0; feet++;}
feet += d2.feet+d3.feet;
}
void Distance1::div_dist(Distance1 d2, int divisor)
{
float fltfeet=d2.feet+d2.inches/12.0;
fltfeet /= divisor; feet=int(fltfeet);
inches=(fltfeet-feet)*12.0;
}
int main(void)
{
cout<<"-------------------------------------------------------------------------------\n"<<endl;
Distance1 dist[100], tmp; int i=0;
dist[i].getdist(); tmp.add_dist(tmp, dist[i++]);
tmp.div_dist(tmp, i); cout<<"\nAverage is: "; tmp.showdist();
}
I am also attaching the output please find the output
hope this is helpful
please please please upvote if it is helpful