I'm trying to print the output of the function after my values are sent to the function. The cout statement needs a string but I'm not sure how I can return a string from my reduce_fraction function while keeping the math correct. In my add_fraction function, you'll see that I simply want to print the added fraction then the reduced fraction right below it. The compiler returns no errors but the output just shows the "Improper Fraction" answer.
#include <iostream>
#include <string>
using namespace std;
void reduce_fraction (int top, int bottom)
{
for (int i = top * bottom; i > 1; i--) {
if ((top % i == 0) && (bottom % i == 0)) {
bottom /= i;
top /= i;
}
}
}
void add_fraction (int numerator, int numerator2, int denominator, int
denominator2)
{
int top;
int bottom;
top = numerator2 * denominator + denominator2 * numerator;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified Fraction -> ";
reduce_fraction(top, bottom);
}
int main()
{
int numerator;
int denominator;
int numerator2;
int denominator2;
char operation;
cout << "Input the numerator: ";
cin >> numerator;
cout << "Input the denominator: ";
cin >> denominator;
cout << "Input the numerator2: ";
cin >> numerator2;
cout << "Input the denominator: ";
cin >> denominator2;
cout << "Input the operation: ";
cin >> operation;
if (operation == '+'){
add_fraction(numerator, numerator2, denominator, denominator2);
}
return 0;
}
Use reference to reflect the changes in top and bottom
and print those in your add_fraction function after calling reduce_fraction
void reduce_fraction ( int & top, int & bottom)
{ ~~~ ~~~
//...
}
Then,
reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
Related
I'm doing a problem with recursive functions for a class project. I'm trying to write a program that uses a recursive function that calculates a modulus without actually using the modulus operator. I'm trying to use a reference parameter because I think that's the easiest way to do this, however my program is not functioning as intended.
//Michael Hery
//COP 2001
//11/15/2017
//Recursive Modulus
#include <iostream>
#include <iomanip>
using namespace std;
int recursiveMod(int &, int);
int main()
{
int num;
int den;
int displayNum;
int total;
cout << "Please input the numerator (an integer) >> ";
cin >> num;
while (num < 0)
{
cout << "Integer must be greater than or equal to zero. >> ";
cin >> num;
}
displayNum = num;
cout << "Please input the denominator (an integer) >> ";
cin >> den;
while (den <= 0)
{
cout << "Integer must be greater than zero. >> ";
cin >> den;
}
cout << "You have entered " << num << " % " << den << endl;
total = recursiveMod(num, den);
cout << "Modulus result: " << displayNum << " % " << den << " = " << total << endl;
system("PAUSE");
return 0;
}
int recursiveMod(int &num, int den)
{
if (num == 0)
return 0;
num = num - den;
if ((num - den) > den)
{
recursiveMod(num, den);
}
return num;
}
There are two errors with your code. Firstly you need to include cstdlib for system("pause") to work.
#include <cstdlib>
Secondly, in your
int recusive mod(int &num, int den)
the condition should be num > den and return must be put.
if (num > den)
{
return recursiveMod(num, den);
}
The problem is this line:
if ((num - den) > den)
For example, if num is 4 and den is 3, then 4-3 is 1 and 1 is not greater than 3, so you will return 4. What you want is
if (num > den)
Try this:
int recursiveMod(int num, int den)
{
if (num < 0) return recursiveMod(num + den, den);
// Don't forget the case of num = 2 and den = 7
if (num < den) return num;
return recursiveMod(num - den, den);
}
I cannot seem to figure out how to make this work any help would be greatly appreciated as i have been trying for days :(
#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong (int input, int sum)
{
if (input == sum)
cout << input << " is an Armstrong number" << endl;
if (input != sum)
cout << input << " is not an Armstrong number" << endl;
}
cubeOfDigits does not return input and sum to isArmstrong, (return input,sum) error is as follows: expression result unused [-Wunused-value]
int cubeOfDigits (int input, int sum, int number1, int number2, int number3)
{
cout << "Enter an integer between 0-999" << endl;
cin >> input;
number1 = input / 100;
number2 = input % 100;
number3 = number2 % 10;
sum = pow(number1, 3) + pow(number2, 3) + pow(number3, 3);
isArmstrong(input, sum);
return input,sum;
}
main calls cubeOfDigits
int main(void)
{
cout << "Welcome" << endl;
cubeOfDigits(input, sum, number1, number2, number3);
return 0;
}
I think what you intended was..
#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong(int input, int sum){
if(input == sum)
cout << input << " is an Armstrong number" << endl;
if(input != sum)
cout << input << " is not an Armstrong number" << endl;
}
int cubeOfDigits (int input)
{
number1 = input/100;
number2 = input % 100;
number3 = number2 % 10;
number2 = number2/10;
sum = pow(number1,3) + pow(number2,3) + pow(number3,3);
return sum;
}
int main(void){
cout << "Welcome" << endl;
cout << "Enter an integer between 0-999" << endl;
cin >> input;
isArmstrong(input,cubeOfDigits(input));
return 0;
}
change the cubeofdigits function from (int) cubeofdigits to (void) cubeofdigits and then remove the return statement from the end of cubeofdigits function .then it must work.
I ran this codes only my zybook class, it gives me the wrong answers and tells me to use floating numbers for the functions. How can I use floating point numbers for square function.
edit:
sorry for the confusion
#include <iostream>
#include <string>
using namespace std;
float squareRoot( float s) {
float xn;
if (s == 0) {
xn = 0;
}
else {
xn = s / 2.0;
int counter = 1;
while (counter <= 10) {
xn = (xn + (s/ xn)) / 2.0;
counter = counter + 1;
}
}
return xn;
} int square(int what) {
return what* what;
}
int main() {
float sideA, sideB, sideC;
string answer = "yes";
while (answer == "yes") {
cout <<" Enter side A: ";
cin >> sideA;
cout << sideA << endl;
cout <<" Enter side B: ";
cin >> sideB;
cout << sideB << endl;
sideC = squareRoot( square( sideA) + square( sideB));
cout <<"Side C is " << sideC << endl;
cout <<"Continue? ";
cin >> answer;
cout << answer << endl;
}
return 0;
}
Your function:
int square (int what){
return what * what;
}
should instead return a float. When you pass the floating point numbers to this function, it returns them as integers so instead of 3.3 and 4.4 getting squared, 3 and 4 do.
You should return a float and change what to a float as well.
I've looked at some of the other questions like this, but it seems like the functions should be all set up properly. Everything compiles correctly when run in CodeLite, but then compiling on a Linux server using g++, I get an undefined reference to function add_fraction, subtract_fraction, multiply_fraction, divide_fraction error in 'main.cpp' How can I fix this? Thanks!
main.cpp
#include <iostream>
#include <string>
#include "header.h"
using std::cout;
using std::cin;
using std::endl;
int main()
{
int numerator;
int denominator;
int numerator2;
int denominator2;
char operation;
cout << "Input the numerator: ";
cin >> numerator;
cout << "Input the denominator: ";
cin >> denominator;
cout << "Input the numerator2: ";
cin >> numerator2;
cout << "Input the denominator: ";
cin >> denominator2;
cout << "Input the operation: ";
cin >> operation;
if (operation != '+' || '-' || '*' || '/'){
cout << "Please input a valid operator: ";
cin >> operation;
}
if (operation == '+'){
Rational addFrac;
addFrac.add_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '-'){
Rational subFrac;
subFrac.subtract_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '*'){
Rational multFrac;
multFrac.multiply_fraction(numerator, numerator2, denominator, denominator2);
}
if (operation == '/'){
Rational divideFrac;
divideFrac.divide_fraction(numerator, numerator2, denominator, denominator2);
}
return 0;
}
header.h
#ifndef HEADER_H
#define HEADER_H
class Rational{
public:
void reduce_fraction(int &top, int &bottom);
void add_fraction(int numerator, int denominator, int numerator2, int denominator2);
void subtract_fraction(int numerator, int denominator, int numerator2, int denominator2);
void multiply_fraction(int numerator, int denominator, int numerator2, int denominator2);
void divide_fraction(int numerator, int denominator, int numerator2, int denominator2);
};
#endif // HEADER_H
header.cpp
#include "header.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
void Rational::reduce_fraction (int & top, int & bottom)
{
for (int i = top * bottom; i > 1; i--) {
if ((top % i == 0) && (bottom % i == 0)) {
bottom /= i;
top /= i;
}
}
}
void Rational::add_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = numerator2 * denominator + denominator2 * numerator;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::subtract_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = denominator2 * numerator - denominator * numerator2;
bottom = denominator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::multiply_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = numerator * numerator2;
bottom = denominator * denominator2;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
void Rational::divide_fraction (int numerator, int numerator2, int denominator, int denominator2)
{
int top;
int bottom;
top = denominator2 * numerator;
bottom = numerator2 * denominator;
cout << "Improper Fraction -> ";
cout << top << "/" << bottom << endl;
cout << "Simplified (Rational) Fraction -> ";
if (top == bottom){
cout << "1" << endl;
}
else {
Rational redu;
redu.reduce_fraction(top, bottom);
cout << top << "/" << bottom << endl;
}
}
Makefile
CXX = g++
CXXFLAGS = -g
default: main
search: main.cpp
${CXX} ${CXXFLAGS} main.cpp -o search
clean:
rm -f *.o main
You are not building header.cpp at all. It's not mentioned in your Makefile. You need to compile it and link it into your executable. Try this:
search: main.cpp header.cpp
${CXX} ${CXXFLAGS} main.cpp header.cpp -o search
I want to simplify a fraction in my application. The fraction is like,
x/y where x and y are integers.
I want to simplify the fraction to its simplest form.
Can anyone please give me hints how to do it.
Thanks in advance.
Compute the greatest common divisor for x and y
Divide both of them by the GCD
Euclid's algorithm is an easy way to compute the GCD.
Divide both by gcd(x,y)
The Binary GCD algorithm is a fast way to compute the GCD on a computer.
#include<iostream>
using namespace std;
struct fraction
{
int n1, d1, n2, d2, s1, s2;
};
void simplification(int a,int b)
{
bool e = true;
int t; int z;
for (int i = (a*b); i > 1;i--)
{ if ((a%i==0)&&(b%i==0))
{
t = a / i;
z = b / i;
}
else
{
e = false;
}
}
cout << "simplest form=" << t << "/" << z << endl;
}
void sum(int num1, int deno1, int num2, int deno2)
{
int k,y;
k = num1* deno2 + num2*deno1;
y = deno2*deno1;
cout << "addition of given fraction = " << k << "/" << y << endl;
simplification(k, y);
}
void sub(int num1, int deno1, int num2, int deno2)
{
int k, y;
k = num1*deno2 - num2*deno1;
y = deno1*deno2;
cout << "Substraction of given fraction = " << k << "/" << y << endl;
}
void mul(int num1, int deno1, int num2, int deno2)
{
int k, y;
k = num1*num2;
y = deno1*deno2;
cout << "multiplication of given fration= " << k<< "/" <<y; cout<< endl;
simplification(k, y);
}
void div(int num1, int deno1, int num2, int deno2)
{
int k, y;
;
k = num1*deno1;
y = deno1*num2;
cout << "division of given fraction" << k << "/" << y << endl;
simplification(k, y);
}
int main()
{ fraction a;
cout << "enter numirator of f1=";cin >> a.n1;
cout << "enter denominator of f1=";cin >> a.d1;
cout << "enter numirator of f2=";cin >> a.n2;
cout << "enter denominator of f2=";cin >> a.d2;
cout << "f1= " << a.n1 << "/" << a.d1 << endl;
cout << "f2= " << a.n2 << "/" << a.d2 << endl;
mul(a.n1, a.d1, a.n2, a.d2);
div(a.n1, a.d1, a.n2, a.d2);
sub(a.n1, a.d1, a.n2, a.d2);
sum(a.n1, a.d1, a.n2, a.d2);
system("pause");
}