Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Help. Hello, I am struggling to finish the code for the following prompt: The sample code for the program is as follows: #include #include

C++ Help. Hello, I am struggling to finish the code for the following prompt:

image text in transcribed

image text in transcribed

The sample code for the program is as follows:

#include #include #include #include #include #include

using namespace std;

struct Fraction { int numerator; int denominator; };

struct FractionArray { Fraction *fractions; //this will point to an array of fractions int nFractions; //this will contain the number of elements in the array };

bool readDataAndShowFractions(string filename, FractionArray &fractions); bool readDataIntoArray(ifstream *infile, Fraction fractions[], int size); Fraction newFraction(int num, int denom); void printFractions(FractionArray fractions); void reduce(Fraction &f); int gcd(int x, int y);

//funtions to perform fraction arithmetic Fraction add(Fraction op1, Fraction op2);

//used to convert an integer to a string //to_string() is defined in the latest version of C++ but is not supported by older compilers

string tostring(int number); string tostring(Fraction f);

int main(int argc, char *argv[]) { //create two structs, one for each array FractionArray fractions1; FractionArray fractions2; bool success;

//initialize and output the first array of Fraction structs success = readDataAndShowFractions("fractiondata1.txt", fractions1); if (!success) { cout

//initialize and output the second array of Fraction structs success = readDataAndShowFractions("fractiondata2.txt", fractions2); if (!success) { cout

//make sure the arrays are of equal size if (fractions1.nFractions != fractions2.nFractions ) { cout

//loop through the Fraction arrays and output the results of //adding the fractions at each position for (int i = 0; i

cin.get();

return 0; }

//adds the two fractions together, creates a new Fraction from the result and //returns it Fraction add(Fraction op1, Fraction op2) { int num1; int num2; int denom;

denom = op1.denominator * op2.denominator; num1 = (denom / op1.denominator) * op1.numerator; num2 = (denom / op2.denominator) * op2.numerator;

Fraction result; result.numerator = num1 + num2; result.denominator = denom;

reduce(result); return result; }

//reduces the given fraction //the fields of the given fraction can be changed //since it is passed by reference void reduce(Fraction &f) { int factor = gcd(f.numerator, f.denominator);

f.numerator = f.numerator / factor; f.denominator = f.denominator / factor; }

//returns the greatest common denominator of two values //(function is recursive) int gcd (int x, int y) { if (y == 0) return x; else return gcd(y, x % y);

} Fraction newFraction(int num, int denom) { Fraction f;

f.numerator = num; f.denominator = denom;

return f; }

/* Reads fraction data from the given file into the array defined in the given struct. Returns true if successful, false if any errors were encountered. */ bool readDataAndShowFractions(string filename, FractionArray &fractions) { //read the size to make the array and create it ifstream *infile; infile = new ifstream(filename, ios::in); if (!infile->is_open()) return false;

//read the number of values into the struct variable and make //sure it is not 0 if (*infile >> fractions.nFractions && fractions.nFractions > 0) { fractions.fractions = new Fraction[fractions.nFractions]; readDataIntoArray(infile, fractions.fractions, fractions.nFractions); }

infile->close(); printFractions(fractions);

return true; }

/* Given a pointer to an open file and an array, reads size data from the file and populates the array.*/ bool readDataIntoArray(ifstream *infile, Fraction fractions[], int size) { int numerator; int denominator;

for (int i = 0; i

*infile >> numerator; if (infile->eof()) return false;

*infile >> denominator; if (infile->eof()) false;

//Add a struct for the new fraction fractions[i] = newFraction(numerator, denominator); }

return true; }

void printFractions(FractionArray fractions) { cout

// tostring() will convert an int value to a string. // to_string() is defined in the latest version of C++ but is not supported by older compilers string tostring(int number) { const int size = sizeof(int) * 4;

char buf[size + 1]; sprintf_s(buf, "%d", number); return string(buf); }

string tostring(Fraction f) { string num = tostring(f.numerator); string denom = tostring(f.denominator); return num + "\\" + denom; }

With the following two txt files:

image text in transcribedimage text in transcribed

For this assignment, you will modify a program that uses two different structs. You will define functions that use data which is contained in struct variables. (If you are interested in seeing more use of pointers and reference variables, the program also demonstrates use of pointers for creating arrays, and use of reference variables to pass parameters.) Ex fractiondata1 fractiondata2bd Download the program and data files linked in the 2 3 assignment. 3 4 Each data file contains a value indicating how 1 12 4 11 many fractions are in the file, followed by the 2 5 2 12 2 9 numerator and denominator for each. 29 3 7 5 8 2 3 2 7 10 2 3 10 2 3 Run the program as is to observe the output, shown here. The data from fractiondata1.txt and fractiondata2.txt is read Fraction data from array and used to create Fraction structs. These structs are used 1/4 /4 populate arrays. 2/9 The results of creating the first Fraction array are shown, followed by the results of creating the second Fraction array. 2/3 Review the functions used to read and then print the Fraction data fron array contents of the fraction array. 2/3 d/4 For example, the for loop in printFractions just loops through 2/12 the array and calls the method to convert the numeric data 2/9 to a string for easier output: 2/3 void print Fractions (F y fractions /2 2/3 1/2 1/4 Fraction data from array endl. cout 3/4 1/2 5/4 1/12 4/11 59/132 2/5 2/12 for fractions 17/30 int i i n Fractions it 59/56 tostring cout fractions fractions [i] endl 2/3 2 20/21 2/3 2/3 4/3 endl. cout After each array is output, the results of looping through those arrays and adding the Fraction values at each array position is shown. A new Fraction struct is created to hold the results of the arithmetic, and code is also provided to reduce the Fraction that results from the arithmetic (for example, note that adding 1/4 +1/4 is reduced to 1/2)

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

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions

Question

Tell the merits and demerits of Mendeleev's periodic table.

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago