Answer to Question #345201 in C++ for Varun

Question #345201

Write a C++ program using classes and objects, which can find the overall average of the entire group(say n) of patients. A single object with default constructor adds 10 patients with 60 kg average kg overweight or underweight. All objects of this patient class(and ONLY objects of this patient class) update the total weight and the total number(n) of patients and can display the overall average. Your C++ program should provide all these facilities.

      


1
Expert's answer
2022-05-26T09:14:04-0400
#include <iostream>
#include <cstdlib>   // for random. rand(), srand()
#include <ctime>    // for random. time()
using namespace std;


class Patient{
    private:
        int amountPatients;
        float totalWeight;
    public:
        Patient(){
            amountPatients+=10;
            for(int i = 0; i<10;i++)
                totalWeight+= 60 + (rand()%2-1);
            
        }
    void displayAverageWeight(){
        cout<<totalWeight/amountPatients<<endl;
    }
};
int main()
{  // test
    srand(time(0)); // for random overweight/underweight by 1(?) kg
    Patient *a = new Patient();
    a->displayAverageWeight();
    Patient *b = new Patient();
    b->displayAverageWeight();
    
    return 0;
}

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