#include <iostream>
#include <iomanip>
int fixzero(int); // prototype
usingnamespace std;
int main()
{
int numerator_One;
int denominator_One; // first fraction (input)
int numerator_Two;
int denominator_Two; // second fraction (input)
cout << "Enter the numerator of the first fraction: ";
cin >> numerator_One; // input the numerator for first fraction
cout << "Enter the denominator of the first fraction: ";
cin >> denominator_One; // input the denominator for the first fraction
cout << "Enter the numerator of the second fraction: ";
cin >> numerator_Two; //input the numerator for the second fraction
cout << "Enter the denominator of the second fraction: ";
cin >> denominator_Two; // input the denominator for the second fraction
cout << endl;
fixzero(denominator_One);
fixzero(denominator_Two);
float value_One;
float value_Two;
value_One = float(numerator_One / denominator_One); //value of the first fraction in decimals
value_Two = float(numerator_Two / denominator_Two); //value of the second fraction in decimals
float sum_Decimal = float(value_One + value_Two);
float sum_Denominator = float(denominator_One * denominator_Two);
float new_Numerator_One = float((sum_Denominator / denominator_One) * numerator_One);
float new_Numerator_Two = float((sum_Denominator / denominator_Two) * numerator_Two);
float added_Numerator = float(new_Numerator_One + new_Numerator_Two); //this bit of code adds the two fractions
cout << "The sum of the fractions is " << (int)added_Numerator << " / " << (int)sum_Denominator << " or " << setprecision(3) << sum_Decimal << endl;
cout << endl;
float difference_Decimal = float(value_One - value_Two);
float difference_Denominator = float(denominator_One * denominator_Two);
float new_Numerator_Three = float((difference_Denominator / denominator_One) * numerator_One);
float new_Numerator_Four = float((difference_Denominator / denominator_Two) * numerator_Two);
float subtracted_Numerator = float(new_Numerator_Three - new_Numerator_Four); // this bit of code subtracts the two fractions
cout << "The difference of the fractions is " << (int)subtracted_Numerator << " / " << (int)difference_Denominator << " or " << difference_Decimal << endl;
cout << endl;
float product_Decimal = float(value_One * value_Two);
float product_Denominator = float(denominator_One * denominator_Two);
float product_Numerator = float(numerator_One * numerator_Two); // this bit of code multiplies the two fractions
cout << "The product of the fractions is " << (int)product_Numerator << " / " << (int)product_Denominator << " or " << product_Decimal << endl;
cout << endl;
float quotient_Decimal = float(value_One / value_Two);
float quotient_Denominator = float(denominator_One * numerator_Two);
float quotient_Numerator = float(numerator_One * denominator_Two); // this bit of code divides the two fractions
cout << "The quotient of the fractions is " << (int)quotient_Numerator << " / " << (int)quotient_Denominator << " or " << quotient_Decimal << endl;
cout << endl;
if(value_One >= value_Two)
{
char response;
cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
cin >> response;
if(response == 'n')
{
cout << "Sorry! Fraction one is greater than or equal to fraction two!" << endl;
}
if(response == 'y')
{
cout << "Correct! Fraction one is greater than or equal to fraction two!" << endl;
}
}
if(value_One < value_Two)
{
char response;
cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
cin >> response;
if(response == 'y')
{
cout << "Sorry! Fraction one is less than fraction two!" << endl;
}
if(response == 'n')
{
cout << "Correct! Fraction one is not greater than or equal to fraction two!" << endl;
}
}
}
int fixzero(int number)
{
if(number == 0)
{
return number + 1;
}
}
Can someone please explain why my function isn't changing the numbers from zero to one?
By the way....In the function fixzero() u must "return number" before the end of the function. if the number==0, it returns number+1, but if number!=0 the function does not do anything (it is an int function). Maybe your compiler doesn't give u any warning, but it's better to make this change.
#include <iostream>
#include <iomanip>
int fixzero(int); // prototype
usingnamespace std;
int main()
{
int numerator_One;
int denominator_One; // first fraction (input)
int numerator_Two;
int denominator_Two; // second fraction (input)
cout << "Enter the numerator of the first fraction: ";
cin >> numerator_One; // input the numerator for first fraction
cout << "Enter the denominator of the first fraction: ";
cin >> denominator_One; // input the denominator for the first fraction
cout << "Enter the numerator of the second fraction: ";
cin >> numerator_Two; //input the numerator for the second fraction
cout << "Enter the denominator of the second fraction: ";
cin >> denominator_Two; // input the denominator for the second fraction
cout << endl;
denominator_One = fixzero( denominator_One );
denominator_Two = fixzero( denominator_Two );
float value_One;
float value_Two;
value_One = float(numerator_One / denominator_One); //value of the first fraction in decimals
value_Two = float(numerator_Two / denominator_Two); //value of the second fraction in decimals
float sum_Decimal = float(value_One + value_Two);
float sum_Denominator = float(denominator_One * denominator_Two);
float new_Numerator_One = float((sum_Denominator / denominator_One) * numerator_One);
float new_Numerator_Two = float((sum_Denominator / denominator_Two) * numerator_Two);
float added_Numerator = float(new_Numerator_One + new_Numerator_Two); //this bit of code adds the two fractions
cout << "The sum of the fractions is " << (int)added_Numerator << " / " << (int)sum_Denominator << " or " << setprecision(3) << sum_Decimal << endl;
cout << endl;
float difference_Decimal = float(value_One - value_Two);
float difference_Denominator = float(denominator_One * denominator_Two);
float new_Numerator_Three = float((difference_Denominator / denominator_One) * numerator_One);
float new_Numerator_Four = float((difference_Denominator / denominator_Two) * numerator_Two);
float subtracted_Numerator = float(new_Numerator_Three - new_Numerator_Four); // this bit of code subtracts the two fractions
cout << "The difference of the fractions is " << (int)subtracted_Numerator << " / " << (int)difference_Denominator << " or " << difference_Decimal << endl;
cout << endl;
float product_Decimal = float(value_One * value_Two);
float product_Denominator = float(denominator_One * denominator_Two);
float product_Numerator = float(numerator_One * numerator_Two); // this bit of code multiplies the two fractions
cout << "The product of the fractions is " << (int)product_Numerator << " / " << (int)product_Denominator << " or " << product_Decimal << endl;
cout << endl;
float quotient_Decimal = float(value_One / value_Two);
float quotient_Denominator = float(denominator_One * numerator_Two);
float quotient_Numerator = float(numerator_One * denominator_Two); // this bit of code divides the two fractions
cout << "The quotient of the fractions is " << (int)quotient_Numerator << " / " << (int)quotient_Denominator << " or " << quotient_Decimal << endl;
cout << endl;
if(value_One >= value_Two)
{
char response;
cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
cin >> response;
if(response == 'n')
{
cout << "Sorry! Fraction one is greater than or equal to fraction two!" << endl;
}
if(response == 'y')
{
cout << "Correct! Fraction one is greater than or equal to fraction two!" << endl;
}
}
if(value_One < value_Two)
{
char response;
cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
cin >> response;
if(response == 'y')
{
cout << "Sorry! Fraction one is less than fraction two!" << endl;
}
if(response == 'n')
{
cout << "Correct! Fraction one is not greater than or equal to fraction two!" << endl;
}
}
}
int fixzero(int number)
{
if(number == 0)
{
return number + 1;
}
else
{
return number;
}
}
When the quotient_Decimal is printed it says "1.#J" what does this mean?
By the way: watch out for the second number; let's say u have 1/2 and 0/2
At this point (1/2) / (0/2)= (1/2) / 0.....so u should call fixzero() again . ;)
First it divides 2 integer numbers (let's say 1/2=0 , 1%2=1) and after that it transforms the result in a float ( 0 =0.0 <-float number). U must transform the 2 number into float before executing a float operation....that's the explanation.