Baby names and birth weights Introduction Babies are weighed

                                                                                      “Baby names and birth weights”

Introduction

Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low birth weight indicates increased risk for complications and such babies are given specialized care until they gain weight. Public health agencies, such as the Centers for Disease Control and Prevention (CDC) in the United States, keep records of births and birth weights. In this project you will write principled object-oriented C++ code to read a data file recording birth information and answer questions based on this data.

Objective

You are given data in text files - each line in a text file is a baby girl’s first name followed by her birth weight in grams (an integer). The text file could contain a large number of such lines. Write code to answer the following questions:

- How many births are recorded in the data file?

- How many babies have a given first name?

- How many babies are born with a low birth weight (less than 2,500 grams)?

- Which baby name is the most popular?

The code

You are given skeleton code with many blank spaces. Your assignment is to fill in the missing parts so that the code is complete and works properly. The code is in three files:

- main.cpp contains the main function and a set of unit tests, implemented using assert statements - each assert statement compares the output of your code with the expected correct answer. Since the provided code is only a template, all the tests will fail immediately with runtime errors. As you add functionality and fix any bugs that may arise, the tests will be able to get further and further. Do not edit this main.cpp file.

- Baby.h contains code for a “Baby” data class. This class is incomplete and you should complete it.

- MedicalRecord.h contains a class whose methods can return answers to the questions asked in this project. You can use (dynamic) arrays but not any of the STL containers (such as std::vector). This class is incomplete and you should complete it.

Hints

MedicalRecord.h will require the use of an array-based data structure of Baby objects. Note that private data members are also incomplete. Feel free to add private member variables to both MedicalRecord.h and Baby.h as you need. There is more than one way to solve this problem!

In summary, do the following:

- Fill in the missing blanks in the skeleton code. Test your progress with the test programs or critic tool. Push your changes to GitHub at regular intervals.

- Check that your code works one last time.

main.cpp contains

Baby.h contains

MedicalRecord.h contains

baby_data_small.txt contains

baby_data_large.txt contains

Solution

//Baby.h contains
#include <string>

using namespace std;

// class that contains information related to a single birth or baby name
class Baby
{
public:
Baby()
{ // default constructor
name = \"\";
weight = 0;
};

Baby(string s, int w)
{ // constructor
name = s;
weight = w;
}

// a \"getter\" method
int getWeight()
{
return weight; // TO BE COMPLETED
}

// a \"getter\" method
string getName()
{
return name; // TO BE COMPLETED
}

private:
string name;
int weight;
};


//MedicalRecord.h contains
#include <string>
#include <stdexcept>
#include \"Baby.h\"

using namespace std;

class MedicalRecord
{
public:
// default constructor
MedicalRecord()
{
totalBirth = 0;
lowWeigh = 0;
}

// destructor
~MedicalRecord()
{
cout << \"baby array deleted\" << endl;

}

// Load information from a text file with the given filename.
void buildMedicalRecordfromDatafile(string filename)
{
ifstream myfile(filename.c_str());

if (myfile.is_open())
{
cout << \"Successfully opened file \" << filename << endl;
string name;
int weight;
while (myfile >> name >> weight)
{
// cout << name << \" \" << weight << endl;
Baby b(name, weight);
addEntry(b);

if(weight < 2500)
lowWeigh++;
}
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[totalBirth] = {0};
for (int i = 0; i < totalBirth; ++i)
{
for (int j = 0; j < totalBirth; ++j)
{
if(array[i].getName() == array[j].getName())
{
countArray[i]++;
}
}
}

int idMax = 0;
int max = countArray[0];
for (int i = 1; i < totalBirth; ++i)
{
if(countArray[i] > max)
{
max = countArray[i];
idMax = i;
}
}

return array[idMax].getName();
}

// return the number of baby records loaded from the text file
int numberOfBirths()
{
return totalBirth; // TO BE COMPLETED
}

// return the number of babies who had birth weight < 2500 grams
int numberOfBabiesWithLowBirthWeight()
{
return lowWeigh; // TO BE COMPLETED
}

// return the number of babies who have the name contained in string s
int numberOfBabiesWithName(string s)
{
int count = 0;
for (int i = 0; i < totalBirth; ++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[totalBirth] = b;
totalBirth++;
}


int totalBirth;
int lowWeigh;
Baby array[100];
// Add private member variables for your data structure along with any
// other variables required to implement the public member functions


};



//main.cpp contains
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include \"MedicalRecord.h\"


using namespace std;


int main() {
try {
{
// test only the Baby class
Baby babyTest(\"Testname\", 1000);
assert(babyTest.getName() == \"Testname\");
assert(babyTest.getWeight() == 1000);
}

{ // test full code with a small data file
MedicalRecord MR;
MR.buildMedicalRecordfromDatafile(\"baby_data_small.txt\"); // build a medical record from the file of baby names and weights\\

int nBirths = MR.numberOfBirths();
cout << \"Number of births: \" << nBirths << endl;
assert(nBirths == 10);

int nEmma = MR.numberOfBabiesWithName(\"Emma\");
cout << \"Number of babies with name Emma: \" << nEmma << endl;
assert(nEmma == 2);

int nLow = MR.numberOfBabiesWithLowBirthWeight();
cout << \"Number of babies with low birth weight: \" << nLow << endl;
assert(nLow == 2);

string mostPopularName = MR.mostPopularName();
cout << \"Most popular baby name: \" << mostPopularName << endl;
assert (mostPopularName == \"Sophia\");
}

{ // test full code with a large data file
MedicalRecord MR;
MR.buildMedicalRecordfromDatafile(\"baby_data_large.txt\"); // build a medical record from the file of baby names and weights\\

int nBirths = MR.numberOfBirths();
cout << \"Number of births: \" << nBirths << endl;
assert (nBirths == 199604);

int nEva = MR.numberOfBabiesWithName(\"Eva\");
cout << \"Number of babies with name Eva: \" << nEva << endl;
assert (nEva == 566);

int nLow = MR.numberOfBabiesWithLowBirthWeight();
cout << \"Number of babies with low birth weight: \" << nLow << endl;
assert (nLow == 15980);

string mostPopularName = MR.mostPopularName();
cout << \"Most popular baby name: \" << mostPopularName << endl;
assert (mostPopularName == \"Emma\");
}
}
catch (exception &e) {
cout << e.what() << endl;
}

// system(\"pause\");
return 0;
}

/*
output:

Successfully opened file baby_data_small.txt
Number of births: 10
Number of babies with name Emma: 2
Number of babies with low birth weight: 2
Most popular baby name: Sophia
baby array deleted
Successfully opened file baby_data_large.txt
Number of births: 43
a.out: main.cpp:48: int main(): Assertion `nBirths == 199604\' failed.
Aborted (core dumped)
ASSERTION FAILED because baby_data_large.txt file is incomplete
*/

 “Baby names and birth weights” Introduction Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low bir
 “Baby names and birth weights” Introduction Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low bir
 “Baby names and birth weights” Introduction Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low bir
 “Baby names and birth weights” Introduction Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low bir
 “Baby names and birth weights” Introduction Babies are weighed soon after birth and this birth weight is recorded as part of a baby’s medical record. A low bir

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site