At a glass manufacturing facility, crystal stemware is carefully inspected for correct dimensions,
quality, and production trends. After lengthy studies, the factory is known to produce 15%
defectives. Most of these pieces are discovered through inspection and are reworked or
discarded. Suppose two pieces are randomly selected for inspection. Fill in the blank. (Give your
answer to four decimal places.) Suppose at least one of the pieces has a flaw. The probability that
both are defective is...
Solution
PROBABILITY OF DEFECTIVE = 0.15
PROBABILITY NON DEFECTIVE =1-0.15 = 0.85
PROBABILITY BOTH DEFECTIVE = 2C2*(0.15)^2*(0.85)^0 = 0.0225.
Baby names and birth weights”IntroductionBabies are weighed soon.pdf
1. 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. Donotedit 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!
Baby.h (I already completed this portion)
#pragma once
2. #include
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 (Please answer each line that says “To be completed” and explain thoroughly
on how to add two text files into this header)
#pragma once
#include
#include
#include "Baby.h"
using namespace std;
class MedicalRecord {
public:
// default constructor
3. MedicalRecord() {
// TO BE COMPLETED
}
// destructor
~MedicalRecord() {
// TO BE COMPLETED
}
// Load information from a text file with the given filename.
void buildMedicalRecordfromDatafile(string filename) {
ifstream myfile(filename);
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);
}
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return the most frequently appearing name in the text file
string mostPopularName() {
return "COMPLETE ME"; // TO BE COMPLETED
}
// return the number of baby records loaded from the text file
int numberOfBirths() {
return -1; // TO BE COMPLETED
}
// return the number of babies who had birth weight < 2500 grams
int numberOfBabiesWithLowBirthWeight() {
return -1; // TO BE COMPLETED
}
4. // return the number of babies who have the name contained in string s
int numberOfBabiesWithName(string s) {
return -1; // TO BE COMPLETED
}
private:
// update the data structure with information contained in Baby object
void addEntry(Baby b) {
// TO BE COMPLETED
}
// Add private member variables for your data structure along with any
// other variables required to implement the public member functions
}
baby_data_large.txt
https://raw.githubusercontent.com/CSUF-Computer-Science/spring2017-project-1-cpsc-131-
section-01-
1/master/baby_data_large.txt?token=AYoWp6P6aphlDMGV7S8o7ZMnKQfA_4psks5Ys0jcwA
%3D%3D
baby_data_small.txt
Sophia 4435
Emma 3561
Ava 2918
Sophia 3765
Mia 2204
Sophia 2749
Lourdes 3981
Sophia 2617
Olivia 4352
Emma 840
Main.cpp (Do not edit)
#include
#include
#include
#include
#include "MedicalRecord.h"
#include "Baby.h"
using namespace std;
5. 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();
6. 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");
}
#pragma once
#include
#include
#include "Baby.h"
using namespace std;
class MedicalRecord {
public:
// default constructor
MedicalRecord() {
// TO BE COMPLETED
}
// destructor
~MedicalRecord() {
// TO BE COMPLETED
}
// Load information from a text file with the given filename.
void buildMedicalRecordfromDatafile(string filename) {
ifstream myfile(filename);
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string name;
int weight;
while (myfile >> name >> weight) {
// cout << name << " " << weight << endl;
7. Baby b(name, weight);
addEntry(b);
}
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return the most frequently appearing name in the text file
string mostPopularName() {
return "COMPLETE ME"; // TO BE COMPLETED
}
// return the number of baby records loaded from the text file
int numberOfBirths() {
return -1; // TO BE COMPLETED
}
// return the number of babies who had birth weight < 2500 grams
int numberOfBabiesWithLowBirthWeight() {
return -1; // TO BE COMPLETED
}
// return the number of babies who have the name contained in string s
int numberOfBabiesWithName(string s) {
return -1; // TO BE COMPLETED
}
private:
// update the data structure with information contained in Baby object
void addEntry(Baby b) {
// TO BE COMPLETED
}
// Add private member variables for your data structure along with any
// other variables required to implement the public member functions
}
baby_data_large.txt
https://raw.githubusercontent.com/CSUF-Computer-Science/spring2017-project-1-cpsc-131-
section-01-
1/master/baby_data_large.txt?token=AYoWp6P6aphlDMGV7S8o7ZMnKQfA_4psks5Ys0jcwA
8. %3D%3D
baby_data_small.txt
Sophia 4435
Emma 3561
Ava 2918
Sophia 3765
Mia 2204
Sophia 2749
Lourdes 3981
Sophia 2617
Olivia 4352
Emma 840
Main.cpp (Do not edit)
#include
#include
#include
#include
#include "MedicalRecord.h"
#include "Baby.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");
9. 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");
}
Solution
#include
10. #include
#include
#include
#include "MedicalRecord.h"
#include "Baby.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();
11. 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");
}
#include
#include
#include
#include
#include "MedicalRecord.h"
#include "Baby.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
12. 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");