Answer to Question #248607 in C++ for Imran

Question #248607
Write a program that calculates the average of up to 100 English distances input by the
user. Create an array of objects of the Distance class, as in the ENGLARAY example . To calculate the average, you can borrow the add_dist() member function
from the ENGLCON example . 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;
}
1
Expert's answer
2021-10-08T07:14:29-0400
#include <iostream>
using namespace std;


class Distance{	
    int dist_feet;
    float dist_inches;
	public:
		Distance(): dist_feet(0), dist_inches(0) { };
		Distance(int feet, int inch): dist_feet(feet), dist_inches(inch) { };
		void getDistance();
		void showDistance() const;
		void add_dist(Distance d);
		void div_dist(int divisor);
};


int main(){
	const int MAX = 100;
	Distance dist[MAX], distance;
	int i = 0;
	char choice;
	do{
		dist[i].getDistance();
		distance.add_dist(dist[i++]);
		cout << "Do you want to continue or stop to see the average (y/n): ";
		cin >> choice;
		cout << endl;
	} while(choice != 'n');
	distance.div_dist(i);
	distance.showDistance();
	return 0;
}


void Distance::getDistance(){
	cout << "Enter the feet of the distance: ";
	cin >> dist_feet;
	cout << "\nEnter the inches of the distance: ";
	cin >> dist_inches;
}


void Distance::showDistance() const{
	cout << dist_feet << "\'-" << dist_inches << '\"' << endl;
}


void Distance::add_dist(Distance d){
	dist_feet += d.dist_feet;
	dist_inches += d.dist_inches;
	if(dist_inches > 11){
		dist_inches -= 12;
		dist_feet++;
	}
}


void Distance::div_dist(int divisor){
	int total_dist = dist_feet * 12 + dist_inches;
	total_dist /= divisor;
	dist_feet = total_dist / 12;
	dist_inches = total_dist % 12;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS