Question
//////////////////////////////////////////////////////////////////////////////// // School of Computing, Universiti Teknologi Malaysia // SECJ1013- Programming Technique I // Semester 1, 2020/2021 // ASSIGNMENT III // 11 Jan 2021 //
//////////////////////////////////////////////////////////////////////////////// // School of Computing, Universiti Teknologi Malaysia // SECJ1013- Programming Technique I // Semester 1, 2020/2021 // ASSIGNMENT III // 11 Jan 2021 // Pair information // Name: // Matrics No: // Section: // ////////////////////////////////////////////////////////////////////////////////
#include
using namespace std;
#define MAXSTREAM numeric_limits
//------------------------------------------------------------------------------------ // Function : readFile // Purpose : To read inputs from a file consisting of the list of patient's name along // with their weights and heights of the patient. // Parameters: // name : the array to hold the list of patient's names read from the file // weight : the array to hold the list of weights // height : the array to hold the list of heights // count : the number of patients read //------------------------------------------------------------------------------------
// Task 1: Complete the definition of function "readFile" (10 marks)
void readFile(char name[][20], double weight[], double height[], int &count) { char nm[20]; // patient's name double wgt; // patient's weight double hgt; // patient's height
ifstream inputFile;
// complete these lines to open the input file, check the input file whether it is exist or not and display the error message (input validation).
count = 0; while (inputFile.getline (nm, 20, '\t')) {
inputFile >> wgt; inputFile >> hgt; inputFile.ignore(MAXSTREAM, ' '); //ignore newline
// complete these lines to store the read data into arrays accordingly and close the open file.
count++; } }
//------------------------------------------------------------------------------------ // Function : convertMeter // Purpose : To convert centimeter to meter // // Parameters: // a - the input array // b - the array that holds the results of meter // n - the size of array // //------------------------------------------------------------------------------------
// Task 2 : Complete the definition of function convertMeter (5 marks)
void convertMeter(double a[], double b[], int n) {
}
//------------------------------------------------------------------------------------ // Function : calculateBMI // Purpose : to calculate the elements of arrays x and y correspondingly using BMI formula, and puts the result into another array, z. // BMI formula: weight(kg) / (height(m) * height (m)) // Parameters: // x,y - the input arrays // z - the array that holds the results of the calculation. //------------------------------------------------------------------------------------ // Task 3 : Complete the defintion of function calculateBMI (7 marks)
void calculateBMI(const double x[], const double y[], double z[], int n) {
}
//------------------------------------------------------------------------------------ // Function : average // Purpose : To calculate the average elements of an array. // // Parameters: // a - the array // n - the size of array // // Return value: the average of all the elements of the array //------------------------------------------------------------------------------------
// Task 4 : Complete the definition of function average (8 marks)
double average(const double a[], int n) { double sum = 0, ave = 0;
}
int main() { char pName[10][20]; // The list (array) of patient's names double pWeight[10]; // The list (array) of patient's weights double pHeight[10]; // The list (array) of patient's heights in cm double pMeter[10]; // The list (array) of patient's heights in meter double pBMI[10]; // The list of BMI, i.e.,weight / (height x height) double aveWgt; // Total average weight for all patients int n; // The number of patients
// Task 5: Using all the functions defined above, write a function call, //(a) to read file from the input file, (b) to convert meter, (c) to calculate BMI and (d) to calculate average of weights. (9 mark)
// Task 6: Print the list of patient's names along with their weight, height in meter and the calculated BMI to the output file (output.txt). (16 marks) // You should open and close the output file, and use proper formatting output.
cout
// Task 7: Print the number of patients and the average weight of all patients to the program screen. (5 marks).
return 0; }
The Ministry of Health Malaysia is responsible for providing health services to help citizens of Malaysia in sustaining a certain level of health that allows them to lead a productive lifestyle. Assume that you are a programmer working for ministry. You have been asked to write a CH program to facilitate the ministry to develop a health care system for Body Mass Index (BMI) Calculator module. The module is to measure body fat based on height in meter and weight in kilogram that applies to adult men and women. There are seven (7) tasks already listed to complete the program. You are given a partial complete of C++ program Question2.cpp) and an input data file (input.txt). Complete the source code according to the tasks given as follows; TASK 1: Complete the definition of function readFile. This function reads the patient data inputs from the provided input text file named input.txt. The patient data consists of patient's name along with their weight in kilogram and height in centimeter. The read data are then stored into arrays accordingly. Input Validation: Please make sure that the program will only continue reading the file if it is successfully opened, otherwise print the error message and exit the program TASK 2: Complete the definition of function convertMeter, which converts patient's height in centimeter to meter. The formula to convert is as follows: height (meter) - height (cm) / 100 TASK 3: Complete the definition of function calculateBMI, which calculates the patient's Body Mass Index (BMI) based on weight in kilogram and height in meter from two arrays correspondingly and puts the results into another array. The formula to calculate BMI is as follows: BMI = weight (kg)/(height (m) * height(m)) TASK 4: Complete the definition of function average. This function calculates the average value of array elements, which calculates the average weight of patients. TASK 5: Using all functions defined above, write a function call: (a) to read file from the input file, (b) to convert centimeter to meter, (c) to calculate BMI and (d) to calculate average weight of patients. TASK 6: Print the list of patient's names along with their weight in kilogram (kg), height in meter (m), and the calculated BMI to the output file named output.txt as shown in Figure 2.1. Note: You should use proper output formatting (align output to the left and number formatting shows two (2) decimal places). Name Weight (kg) Height (m) BMI Qisha Sumayyah Anis Nabila Adam Zakaria Mohamad Hafetz Asma Safiyyah Ahmad Ali 70.50 30.90 50.10 65.30 45.20 85.60 1.40 1.30 1.55 1.65 1.32 1.72 35.97 18.28 20.85 23.99 25.94 28.93 Figure 2.1: Sample output in output.txt TASK 7: Print the number of patients and the average weight for all patients to the program screen as shown in Figure 2.2. Note: You should use proper output formatting (number formatting for average shows one decimal place) Writing to the output file... Number of patients . 6 Average weight : 57.9 Figure 2.2: Sample output in program screenStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started