Could somebody please, how to write this c++ program? Here are the fractions.cpp
ID: 3824694 • Letter: C
Question
Could somebody please, how to write this c++ program?
Here are the fractions.cpp
Start with the program fractions.cpp Modify it to 1. Dynamically allocate two arrays to hold Fration structs and initialize data in each array from files The Fractions are created for each array by reading data from two separate files The first value in each file is the size of the array Each data file contains a value indicating how many fractions are in the file, followed by the numerator and denominator for each. Elfractiondatalbt a fractiondata2tt E3 2 2 3 3 1 4 3 1 4 4 3 4 4 1 2 5 1 12 5 4 11 2 5 6 2 12 7 29 7 29 8 3 7 8 5 8 9 2 3 9 2 7 100 2 3 10 2 3 2. Construct a method that takes an array of Fractions and outputs the contents. Use the tostring method to print each fraction void printFractions(Fraction fractions) 3. Construct methods to add, subtract, multiply, and divide two Fractions and return a Fraction that represents the result. For example, here is a signature for an add method: Fraction add(Fraction &op1;, Fraction &op2;); (If you want to experiment with sending pointers to Fraction structs and dynamically allocating the result struct, you are welcome to do that 4. Loop through your Fraction arrays and output the result of applying each operation (add, subtract, multiply, and divide) to the Fractions at parallel positions in the arrays. Fraction result add (fractions1.fractions Oil, fractions2.fractions [i]) coutExplanation / Answer
Following is the code, for first four parts of the question.
#include<iostream>
#include<fstream>
#include<malloc.h>
#include<string>
#include<iomanip>
using namespace std;
struct Fraction
{
int numerator;
int denomenator;
};
string tostring(int number)
{
const int size = sizeof(int)*4;
char buf[size +1];
sprintf(buf,"%d",number);
return string(buf);
}
string toString(Fraction f)
{
return tostring(f.numerator)+"\"+tostring(f.denomenator);
}
void printFractions(Fraction * fractions, int count)
{
for(int i =0;i<count;i++){
cout<<toString(fractions[i])<<endl;
}
}
// Operations on fractions
Fraction add(Fraction f1, Fraction f2){
Fraction result;
result.denomenator = f1.denomenator*f2.denomenator;
result.numerator = f1.numerator*f2.denomenator +
f2.numerator*f1.denomenator;
return result;
}
Fraction subtract(Fraction f1, Fraction f2){
Fraction result;
result.denomenator = f1.denomenator*f2.denomenator;
result.numerator = f1.numerator*f2.denomenator -
f2.numerator*f1.denomenator;
return result;
}
Fraction multiply(Fraction f1, Fraction f2){
Fraction result;
result.denomenator = f1.denomenator*f2.denomenator;
result.numerator = f1.numerator*f2.numerator;
return result;
}
Fraction divide(Fraction f1, Fraction f2){
Fraction result;
result.numerator = f1.numerator*f2.denomenator;
result.denomenator = f2.numerator*f1.denomenator;
return result;
}
int main()
{
ifstream file1,file2;
//open files to read,
//enter the names of your files with absolute path, if needed
file1.open("file1.txt");
file2.open("file2.txt");
int count1,count2,a,b;
//get count of fractions
file1>>count1;
file2>>count2;
//dynamically allocate memory for fractions array for both files
Fraction *firstFileFractions = (Fraction*)malloc(sizeof(Fraction)*count1);
Fraction *secondFileFractions = (Fraction*)malloc(sizeof(Fraction)*count2);
//initialize the first array
for(int i=0;i<count1;i++){
file1>>a>>b;
firstFileFractions[i].numerator=a;
firstFileFractions[i].denomenator=b;
//cout<<"a="<<a<<" b="<<b<<endl;
}
//initialize the second array
for(int i=0;i<count2;i++){
file2>>a>>b;
secondFileFractions[i].numerator=a;
secondFileFractions[i].denomenator=b;
//cout<<"a="<<secondFileFractions[i].numerator<<" b="<<secondFileFractions[i].denomenator<<endl;
}
//apply each operation
Fraction (*arrayFunctions[4])(Fraction,Fraction)={add,subtract,multiply,divide};
char operations[4]={'+','-','*','/'};
for(int i_operation=0;i_operation<4;i_operation++)
{
for(int i_fraction=0;i_fraction<count1;i_fraction++)
{
cout<<toString(firstFileFractions[i_fraction])
<<operations[i_operation]
<<toString(secondFileFractions[i_fraction])
<<"="
<<toString((*arrayFunctions[i_operation])(firstFileFractions[i_fraction],secondFileFractions[i_fraction]))
<<endl;
}
}
//printFractions(firstFileFractions,count1);
//close all the files
file1.close();
file2.close();
return 0;
}