Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FIX MY C++ SO weights.txt displays Average, High, and Low #include #include #include using namespace std; const int MAX_SIZE = 10; // Function

FIX MY C++ SO "weights.txt" displays " Average, High, and Low "

#include

#include

#include

using namespace std;

const int MAX_SIZE = 10;

// Function to read names and weights from file

int readData(string names[], int weightsInPounds[]);

// Function to return pounds in kilograms

float poundsToKilograms(int pounds);

// Function to calculate and store weight in kilograms

void calculateWeightInKilograms(int weightsInPounds[], float weightsInKilograms[], int size);

// Function to print names and weights in a tabular format

void printData(string names[], int weightsInPounds[], int size);

// Function to calculate low, high and average weights

void processData(int weightsInPounds[], int size, int &lowWeight, int &highWeight, float &averageWeight);

// Function to print low, high and average weights

void printProcessedData(int lowWeight, int highWeight, float averageWeight);

// Function to sort data in ascending order by weight using bubble sort

void sortData(string names[], int weightsInPounds[], int size);

int main() {

// Declare arrays and variables

string names[MAX_SIZE];

int weightsInPounds[MAX_SIZE];

int size = 0;

int lowWeight, highWeight;

float averageWeight;

size = readData(names, weightsInPounds);

// Check if the arrays are empty

if (size == 0) {

return 0;

}

cout

printData(names, weightsInPounds, size);

cout

processData(weightsInPounds, size, lowWeight, highWeight, averageWeight);

printProcessedData(lowWeight, highWeight, averageWeight);

cout

sortData(names, weightsInPounds, size);

cout

printData(names, weightsInPounds, size);

return 0;

}

// Function to read names and weights from file

int readData(string names[], int weightsInPounds[]) {

// Get filename from the user

string filename;

cout

cin >> filename;

// Open file

ifstream inFile(filename);

// Check if file opened successfully or not

if (!inFile) {

cin >> filename;

cout

return 0;

}

// Check if file is empty

inFile.seekg(0, ios::end);

if (inFile.tellg() == 0) {

cout

return 0;

}

inFile.seekg(0, ios::beg);

// Read names and weights from the file

int size = 0;

while (size

inFile >> names[size];

if (inFile.eof())

break;

inFile >> weightsInPounds[size];

size++;

}

// Close file

inFile.close();

return size;

}

// Function to return pounds in kilograms

float poundsToKilograms(int pounds) {

return pounds / 2.2;

}

// Function to calculate and store weight in kilograms

void calculateWeightInKilograms(int weightsInPounds[], float weightsInKilograms[], int size) {

for (int i = 0; i

weightsInKilograms[i] = poundsToKilograms(weightsInPounds[i]);

}

}

// Function to print names and weights in a tabular format

void printData(string names[], int weightsInPounds[], int size) {

// Calculate weight in kilograms

float weightsInKilograms[MAX_SIZE];

calculateWeightInKilograms(weightsInPounds, weightsInKilograms, size);

// Print names, weights in pounds and weights in kilograms

cout

for (int i = 0; i

cout

}

}

// Function to calculate low, high and average weights

void processData(int weightsInPounds[], int size, int &lowWeight, int &highWeight, float &averageWeight) {

lowWeight = highWeight = weightsInPounds[0];

int total = 0;

for (int i = 0; i

if (weightsInPounds[i]

lowWeight = weightsInPounds[i];

}

if (weightsInPounds[i] > highWeight) {

highWeight = weightsInPounds[i];

}

total += weightsInPounds[i];

}

averageWeight = (float)total / size;

}

// Function to print low, high and average weights

void printProcessedData(int lowWeight, int highWeight, float averageWeight) {

cout

cout

cout

}

// Function to sort data in ascending order by weight using bubble sort

void sortData(string names[], int weightsInPounds[], int size) {

for (int i = 0; i

for (int j = 0; j

{

if (weightsInPounds[j] > weightsInPounds[j + 1]) {

// Swap weightsInPounds

int temp = weightsInPounds[j];

weightsInPounds[j] = weightsInPounds[j + 1];

weightsInPounds[j + 1] = temp;

// Swap names

string tempName = names[j];

names[j] = names[j + 1];

names[j + 1] = tempName;

}

}

}

}

RESULT SHOULD BE image text in transcribed

Average:High:Low:166.67lbs234lbs67lbs75.76Kilograms106.36Kilograms30.45Kilograms

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions