Help with a C++ Project

Hi! I am trying to figure out this problem.

Write a program that asks a doctor to enter the number of patients that walked into his/her clinic at the end of the day.
(On different days, the clinic may get different numbers of patients.) Then the program should ask the doctor
to enter each patient’s age and store it in a dynamically allocated array.
Once the doctor has finished entering all the ages,
the program should display all the ages in one line, separated by commas (see the screenshot).


The program should also display the average age, highest and
lowest ages among all of the patients.
(You should free the dynamically allocated memory at the right location).
At the end of the program, it should ask the doctor if he/she wants to continue.


this is my code so far:


#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;


double getAverage(double*,int);

int main()
{
int numPatients=0; //variable to store user input for #of patients

double *PatientAge = new double[numPatients]; //dynamically allocated array


cout<<"-----------------------------------------------\n\n";
cout<<"How many patients walked into the clinic today: ";
cin>>numPatients;


for(int count=0; count<numPatients; count++)
{
cout<<"Enter the age of patient "<<count+1<<": ";
cin>>*(PatientAge+count);
}

return 0;
}



I don't know how to calculate and return the average, min, and max! Maybe a while loop?

Any advice or help would be awesome, I am new to c++ and struggling with pointers and arrays.



Last edited on
You've made a start, that;s good.

You need to ask for the number of patients before using that value to allocate the array. It's best to create functions to calculate the average, min and max. Assuming they've been implemented, main looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {
    cout << "How many patients walked into the clinic today: ";
    cin >> numPatients;
    auto* patientAge = new int[numPatients];

    for (int count=0; count < numPatients; ++count) {
        cout << "Enter the age of patient " << (count + 1) <<": ";
        cin >> patientAge[count];
    }

    cout << "Average age: " << average(patientAge, numPatients) << endl;
    cout << "Minimum age: " << min(patientAge, numPatients) << endl;
    cout << "Maximum age: " << max(patientAge, numPatients) << endl;

    delete [] patientAge;
}


All we have to do now is figure out how to implement average(), min() and max(). I'll do average(), and you can do the rest.

The formula is described here: https://www.calculator.net/average-calculator.html

My array has integer values, but the average will be a double.
1
2
3
4
5
6
double average(int* patientAge, int numPatients) {
    double sum = 0;
    for (int count = 0; count < numPatients; ++count)
        sum += patientAge[count];
    return sum / patientAge;
}


So you were right, you need a loop. There should be plenty of examples of min/max functions around. The C++ Standard Template Library also has an implementation, but I guess you'll be expected to write your own.
Topic archived. No new replies allowed.