Must be done in C++. This problem can use loops, arrays, strings, and functions.
ID: 668909 • Letter: M
Question
Must be done in C++. This problem can use loops, arrays, strings, and functions. It needs to be able to accommodate a whole number portion of a fraction (as in the '3' in 3 1/2):
You are developing a Fraction structure for Teacher’s Pet Software. The structure contains
three public data fields for whole number, numerator, and denominator. Using the
same structure, write a main()function that declares an array of five Fraction objects.
Prompt the user for values for each field of each Fraction. Do not allow the user to enter
a value of 0 for the denominator of any Fraction; for each Fraction, continue to prompt
the user for a denominator value until a non-zero value is entered. After all the objects
have been entered:
» Display the whole number, numerator, and denominator for each of the five Fraction
objects as entered.
» Next, display the five Fraction objects all converted to proper form—that is, if the user
entered a value such as 2 6/4, now display it as 3 1/2.
» Next, display the sum of all the Fractions and the arithmetic average of all the
Fractions.
Explanation / Answer
here is the c++ code for the first two parts due to some inconvinience i have to post half part of the solution but i will repost the full solution after some time
#include<iostream>
using namespace std;
struct fraction
{
int whole;
int numerator;
int denominator;
};
struct frac
{
int numerator;
int denominator;
int whole;
};
int gcd(int m,int n)
{
if (m<=1||n<=1||m==n)
{
if(m==n)
return(n);
else if (m<=1)
return(1);
else
return(1);
}
else if(m>n)
return(gcd(m-n,n));
else return(gcd(m,n-m));
}
void display(struct fraction* f)
{
for(int i=0;i<5;i++)
{
cout<<"the whole number for "<<i+1<<" object : ";
cout<<f[i].whole<<endl;
cout<< " the numerator for "<<i+1<<" object : ";
cout<<f[i].numerator<<endl;
cout<<" the denominator for "<<i+1<<" object : ";
cout<<f[i].denominator<<endl;
}
}
int insert()
{
int n=1;
int a;
while(n)
{
cin>>a;
if(a!=0)
n=0;
else
{
cout<<"Please enter the value other than 0 ";
}
}
return a;
}
int main()
{
struct fraction f[5];
for(int i=0;i<5;i++)
{
cout<<"enter the whole for "<<i+1<<" object : ";
cin>>f[i].whole;
cout<< "enter the numerator for "<<i+1<<" object : ";
cin>>f[i].numerator;
cout<<"enter the denominator for "<<i+1<<" object : ";
f[i].denominator=insert();
}
display(f);
struct fraction g[5];
for(int i=0;i<5;i++)
{
g[i].numerator=f[i].whole*f[i].denominator+f[i].numerator;
g[i].denominator=f[i].denominator;
}
int z;
for(int i=0;i<5;i++)
{
z=gcd(g[i].denominator,g[i].numerator);
g[i].denominator=g[i].denominator/z;
g[i].numerator=g[i].numerator/z;
g[i].whole=g[i].numerator/g[i].denominator;
g[i].numerator=g[i].numerator-g[i].denominator*g[i].whole;
}
display(g);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.