C++ Help. Hello, I am struggling to finish the code for the following prompt: Th
ID: 3825501 • Letter: C
Question
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
#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 << "Fraction array could not be constructed from fractiondata1.txt" << endl;
exit(-1);
}
//initialize and output the second array of Fraction structs
success = readDataAndShowFractions("fractiondata2.txt", fractions2);
if (!success)
{
cout << "Fraction array could not be constructed from fractiondata2.txt" << endl;
exit(-1);
}
//make sure the arrays are of equal size
if (fractions1.nFractions != fractions2.nFractions )
{
cout << "Fraction arrays are not the same size" << endl;
exit(-1);
}
//loop through the Fraction arrays and output the results of
//adding the fractions at each position
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = add(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
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 < size; i++)
{
// read in the numerator and denominator, checking to see that we are not at
// the end of file. Note that this isn't perfectly safe, because if a denominator
// is missing, our data will be off.
*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 << "Fraction data from array" << endl;
for (int i = 0; i < fractions.nFractions; i++)
{
cout << tostring(fractions.fractions[i]) << endl;
}
cout << endl;
}
// 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:
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)Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <sstream>
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);
Fraction subtract(Fraction op1, Fraction op2);
Fraction multiply(Fraction op1, Fraction op2);
Fraction divide(Fraction op1, Fraction op2);
double getDecimal(Fraction);
//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 << "Fraction array could not be constructed from fractiondata1.txt" << endl;
exit(-1);
}
//initialize and output the second array of Fraction structs
success = readDataAndShowFractions("fractiondata2.txt", fractions2);
if (!success)
{
cout << "Fraction array could not be constructed from fractiondata2.txt" << endl;
exit(-1);
}
//make sure the arrays are of equal size
if (fractions1.nFractions != fractions2.nFractions )
{
cout << "Fraction arrays are not the same size" << endl;
exit(-1);
}
//loop through the Fraction arrays and output the results of
//adding the fractions at each position
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = add(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// subtract
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = subtract(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// multiply
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = multiply(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// divide
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = divide(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// decimal value fraction 1
for (int i = 0; i < fractions1.nFractions; i++)
{
cout << tostring(fractions1.fractions[i]) << " = "<<getDecimal(fractions1.fractions[i])<<endl;
}
// decimal value fraction 2
for (int i = 0; i < fractions2.nFractions; i++)
{
cout << tostring(fractions2.fractions[i]) << " = "<<getDecimal(fractions2.fractions[i])<<endl;
}
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;
}
//subtract the two fractions together, creates a new Fraction from the result and
//returns it
Fraction subtract(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;
}
//multiply the two fractions together, creates a new Fraction from the result and
//returns it
Fraction multiply(Fraction op1, Fraction op2)
{
int num;
int denom;
denom = op1.denominator * op2.denominator;
num = op1.numerator * op2.numerator;
Fraction result;
result.numerator = num;
result.denominator = denom;
reduce(result);
return result;
}
//multiply the two fractions together, creates a new Fraction from the result and
//returns it
Fraction divide(Fraction op1, Fraction op2)
{
int num;
int denom;
denom = op1.denominator * op2.numerator;
num = op1.numerator * op2.denominator;
Fraction result;
result.numerator = num;
result.denominator = denom;
reduce(result);
return result;
}
// function to get decimal value
double getDecimal(Fraction f){
return (double)f.numerator/(double)f.denominator;
}
//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 < size; i++)
{
// read in the numerator and denominator, checking to see that we are not at
// the end of file. Note that this isn't perfectly safe, because if a denominator
// is missing, our data will be off.
*infile >> numerator;
if (infile->eof())
return false;
*infile >> denominator;
if (infile->eof())
return false;
//Add a struct for the new fraction
fractions[i] = newFraction(numerator, denominator);
}
return true;
}
void printFractions(FractionArray fractions)
{
cout << "Fraction data from array" << endl;
for (int i = 0; i < fractions.nFractions; i++)
{
cout << tostring(fractions.fractions[i]) << endl;
}
cout << endl;
}
// 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];
stringstream ss;
ss << number;
//sprintf_l(buf, "%d", number);
//return string(buf);
return ss.str();
}
string tostring(Fraction f)
{
string num = tostring(f.numerator);
string denom = tostring(f.denominator);
return num + "\" + denom;
}
###########
Fraction data from array
1
1
3
1
2
29
3
2
2
Fraction data from array
2
1
1
4
2
29
58
2
2
1 + 2 = 7
1 + 1 = 1
3 + 1 = 5
1
+ 4 = 59Z
2 + 2
= 17
29 + 29 = 49
3 + 58 = 59.
2 + 2 = 20
2 + 2 = 4
1 + 2 = 1-6
1 + 1 = 0
3 + 1 = 1
1
+ 4 = 37-132
2 + 2
= 7
29 + 29 = 0
3 + 58 = -11.
2 + 2 = 8
2 + 2 = 0
1 + 2 = 1
1 + 1 = 1
3 + 1 = 38
1
+ 4 = 1
2 + 2
= 1
29 + 29 = 481
3 + 58 = 15.
2 + 2 = 4
2 + 2 = 49
1 + 2 = 3
1 + 1 = 1
3 + 1 = 3
1
+ 4 = 118
2 + 2
= 12
29 + 29 = 1
3 + 58 = 24
2 + 2 = 7
2 + 2 = 1
1 = 0.5
1 = 0.25
3 = 0.75
1
= 0.0833333
2 = 0.4
29 = 0.222222
3 = 0.428571
2 = 0.666667
2 = 0.666667
2 = 0.666667
1 = 0.25
1 = 0.5
4 = 0.363636
2
= 0.166667
29 = 0.222222
58 = 0.625
2 = 0.285714
2 = 0.666667
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.