Hi, please look below at the part that is boxed in red which is located in Medic
ID: 3797793 • Letter: H
Question
Hi, please look below at the part that is boxed in red which is located in MedicalRecord.h; I want to construct a dynamic arrays that hold unspecified amount of data in my text files. Since my text files has many data whereby I cannot count how many there are.
Baby.h
Baby.h
#pragma once
#include <string>
using namespace std;
// class that contains information related to a single birth or baby name
class Baby {
public:
Baby()
{ // default constructor
};
Baby(string s, int w)
{
name = s;
weight = w;
}
// a "getter" method
int getWeight() {
return weight;
}
// a "getter" method
string getName() {
return name;
}
private:
string name;
int weight;
};
MedicalRecord.h
Medicalrecord.h
#pragma once
#include <string>
#include <stdexcept>
#include "Baby.h"
using namespace std;
class MedicalRecord {
public:
// default constructor
MedicalRecord() {
// TO BE COMPLETED
totalweight = 0;
lowweight = 0;
countname = 0;
}
// destructor
~MedicalRecord()
{
cout << "Array has been deleted" << endl;
// TO BE COMPLETED
}
// Load information from a text file with the given filename.
void buildMedicalRecordfromDatafile(string filename) {
ifstream myfile(filename);
int index = 0;
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string name;
int weight;
while (myfile >> name >> weight) {
cout << name << " " << weight << endl;
// cout << name << " " << weight << endl;
Baby b(name, weight);
addEntry(b);
index += 1;
}
countname = index;
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return the most frequently appearing name in the text file
string mostPopularName()
{
int *countarray = new int[countname];
for (int i = 0; i < countname; i++)
{
countarray[i] = 0;
}
for (int i = 0; i < countname; i++)
{
for (int j = 0; j < countname; j++)
{
if (array[i].getName() == array[j].getName())
{
countarray[i]++;
}
}
}
int max = 0;
for (int i = 0; i < countname; i++)
{
if (countarray[max] < countarray[i])
{
max = i;
}
}
return array[max].getName();
}
// return the number of baby records loaded from the text file
int numberOfBirths()
{
return countname; // TO BE COMPLETED
}
// return the number of babies who had birth weight < 2500 grams
int numberOfBabiesWithLowBirthWeight()
{
int numbBabies = 0;
for (int i = 0; i < countname; i++)
{
if (array[i].getWeight() < 2500)
{
numbBabies++;
//cout << array[i].getName();
}
}
return numbBabies;
// TO BE COMPLETED
}
// return the number of babies who have the name contained in string s
int numberOfBabiesWithName(string s)
{
// TO BE COMPLETED
// ask for help
int count = 0;
for (int i = 0; i < countname; i++)
{
if (array[i].getName() == s)
count++;
}
return count;
}
private:
// update the data structure with information contained in Baby object
void addEntry(Baby b)
{
array[index] = b;
index++;
// TO BE COMPLETED
}
/// omit Baby array[10000];
bool lowweight;
int totalweight;
int countname;
int index = 0;
// Add private member variables for your data structure along with any
// other variables required to implement the public member functions
};
#pragma once clude using namespace std class that contains information related to a single birth or baby name class Baby public: Baby default constructor E Baby (string s, int w) name s; weight w a getter method E int get Weight f return weight; a getter method string getName return name; private string name; int eightExplanation / Answer
#include <string>
#include <stdexcept>
#include "Baby.h"
using namespace std;
class MedicalRecord {
public:
// default constructor
MedicalRecord() {
// TO BE COMPLETED
totalweight = 0;
lowweight = 0;
countname = 0;
}
// destructor
~MedicalRecord()
{
cout << "Array has been deleted" << endl;
// TO BE COMPLETED
}
// Load information from a text file with the given filename.
void buildMedicalRecordfromDatafile(string filename) {
ifstream myfile(filename);
int index = 0;
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string name;
int weight;
while (myfile >> name >> weight) {
cout << name << " " << weight << endl;
// cout << name << " " << weight << endl;
Baby b(name, weight);
addEntry(b);
index += 1;
}
countname = index;
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return the most frequently appearing name in the text file
string mostPopularName()
{
int *countarray = new int[countname];
for (int i = 0; i < countname; i++)
{
countarray[i] = 0;
}
for (int i = 0; i < countname; i++)
{
for (int j = 0; j < countname; j++)
{
if (array[i].getName() == array[j].getName())
{
countarray[i]++;
}
}
}
int max = 0;
for (int i = 0; i < countname; i++)
{
if (countarray[max] < countarray[i])
{
max = i;
}
}
return array[max].getName();
}
// return the number of baby records loaded from the text file
int numberOfBirths()
{
return countname; // TO BE COMPLETED
}
// return the number of babies who had birth weight < 2500 grams
int numberOfBabiesWithLowBirthWeight()
{
int numbBabies = 0;
for (int i = 0; i < countname; i++)
{
if (array[i].getWeight() < 2500)
{
numbBabies++;
//cout << array[i].getName();
}
}
return numbBabies;
// TO BE COMPLETED
}
// return the number of babies who have the name contained in string s
int numberOfBabiesWithName(string s)
{
// TO BE COMPLETED
// ask for help
int count = 0;
for (int i = 0; i < countname; i++)
{
if (array[i].getName() == s)
count++;
}
return count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.