Calculating modulus by recursion without using modulo division - c++

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);
}

Related

When the main body calls for a constructor, the constructor calls for a function and when it does tries to call for a function it crashes?

After the constructor is called in the body, the constructor calls for the function and that's when my program crashes. I can't find the reason why...thanks in advance.
There may be other logical errors in the code but I'm currently trying to figure out the reason why it won't call the function from constructor. I'm pretty sure I've set it up correctly but it still crashes.
This program is for an assignment for my class.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Rational
{
public:
//void setNum(int);
//void setDen(int);
void gcd(int, int);
void normalize(int, int, int, bool, bool);
int getNum();
int getDen();
Rational(int num, int den)
{
numerator = num;
denominator = den;
cout << "i made it through the constgructor now sending it to gcd";
gcd(numerator, denominator);
}
Rational(int wholeNumber)
{
wholeNumber = wholeNumber / 1;
}
Rational()
{
numerator = 0;
denominator = 1;
}
private:
int numerator;
int denominator;
};
void Rational::gcd(int numerator, int denominator)
{
int gcd;
bool negNum;
bool negDen;
//check if numerator is negative
//if so change it before finding gcd
if (numerator < 0)
{
negNum = true;
numerator = -1 * numerator;
}
else
{
negNum = false;
}
//check if denominator is negative
//if so change it before finding gcd
if (denominator < 0)
{
negDen = true;
denominator = -1 * denominator;
}
else
{
negDen = false;
}
//finds the gcd of both numbers
for (int i = 0; i <= numerator&&denominator; i++)
{
if (numerator%i == 0 && denominator%i == 0) //if i divides into both #s evenly then its the gcd so far
{
gcd = i;
}
}
//call for normalize function
normalize(numerator, denominator, gcd, negNum, negDen);
}
void Rational::normalize(int numerator, int denominator, int gcd, bool negNum, bool negDen)
{
//if numerator was negative make it negative again
if (negNum = true)
{
numerator = -1 * numerator;
}
//if denominator was negative make it negative again
if (negDen = true)
{
denominator = -1 * denominator;
}
//simplify
numerator = numerator / gcd;
denominator = denominator / gcd;
//if both were negative change them to positive
if (numerator && denominator < 0)
{
numerator = -1 * numerator;
denominator = -1 * denominator;
}
//check if wholeNumber
/*
if (numerator / denominator == 0)
{
isWhole = true;
wholeNumber = numerator / denominator;
}*/
}
/*void Rational::setNum(int numerator)
{
numerator = numerator;
}*/
int Rational::getNum()
{
return numerator;
}
int Rational::getDen()
{
return denominator;
}
int main()
{
int num, den;
char slash;
//Rational f1;
//Rational f2;
//Rational f3;
cout << "Enter a fraction in the format integer_numerator/integer_denominator\n";
cin >> num >> slash >> den;
Rational f1(num, den);
//cout << "sent it off already to constructor\n"; //good
//f1.gcd(num, den);
//cout << "sent it to gcd\n";
cout << "\nYou entered the equivalent of: " << f1.getNum() << "/" << f1.getDen();
cout << "\nEnter a fraction in the format integer_numerator/integer_denominator\n";
cin >> num >> slash >> den;
//put error message here
//else send it to gcd
Rational f2(num, den);
f2.gcd(num, den);
cout << "\nYou entered the equivalent of: " << f2.getNum() << "/" << f2.getDen();
cout << "\nEnter a fraction in the format integer_numerator/integer_denominator\n";
cin >> num >> slash >> den;
//put error message here
//else send it to gcd
Rational f3(num, den);
f3.gcd(num, den);
cout << "\nYou entered the equivalent of: " << f3.getNum() << "/" << f3.getDen();
cout << "\nTesting arithmetic and relational operator overloading\n";
//display math
cout << f1.getNum() << "/" << f1.getDen() << " * " << f2.getNum() << "/" << f2.getDen() << " = " << (f1.getNum() / f1.getDen()) * (f2.getNum() / f2.getDen());
return 0;
}
Your gcd function has a local variable gcd that isn't initialized. It is possible for the function to execute through to the normalize call without assigning a value to gcd. That uninitialized variable's value is then passed as an argument to normalize.
Your loop inside gcd starts from i = 0. Then i is used as a divisor in the loop as in numerator % i. That's a division by zero; % calculates the residue of a division, and so is a kind of division operation.
As an aside, you should know about Euclid's algorithm for GCD. Also the Binary GCD algorithm which uses only bit operations and shifts.

How to return a value to another function

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.

Wrong output when searching for prime numbers in given interval

I am trying to find to find prime numbers between num1 and num2, but this code is yields only 1,2,3 as prime numbers if my input is 1,10
#include <iostream>
using namespace std;
void prime(int num1, int num2)
{
bool prime = 1; // prime=1 means the number is prime
for (num1; num1 <= num2; num1++)
{
for (int i = 2; i < num1; i++)
{
if (num1 % i == 0)
{
prime = 0;
break;
}
}
if (prime == 0)
{
cout << num1 << " Is not Prime" << endl;
}
else
{
cout << num1 << " Is prime" << endl;
}
}
}
int main()
{
int num1, num2;
cout << "Enter 2 numbers to check the prime numbers between them:";
cin >> num1 >> num2;
prime(num1, num2);
}
You're setting prime=1 only once, before you start looping over candidate prime numbers. So as soon as you hit a number that isn't prime (in your case, 4) you get prime=0 and after that it never gets set to 1 again.
Also second for should go from 3 to (int)sqrt((double)num2), with a step i+=2.
You are forgetting to reset bool prime to true.
Try this....
#include <iostream>
using namespace std;
void prime(int num1, int num2)
{
for (num1; num1 <= num2; num1++)
{
bool prime = 1; // prime=1 means the number is prime
for (int i = 2; i < num1; i++)
{
if (num1 % i == 0)
{
prime = 0;
break;
}
}
if (prime == 0)
cout << num1 << " Is not Prime" << endl;
else
cout << num1 << " Is prime" << endl;
}
}
int main()
{
int num1, num2;
cout << "Enter 2 numbers to check the prime numbers between them:";
cin >> num1 >> num2;
prime(num1, num2);
}

How to print void function after cout statement?

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;

Having trouble with a simple loop when trying to print two numbers to the screen at once

I need to make ask the user for how many numbers of the Fibonacci sequence displayed to the screen. I have everything done pretty much, but since i am printing two numbers to the screen at a time, it is printing double of what the user enters. I would just divide the number the user enters by two, but the wont work for odd numbers. If anyone can think of a solution for this, thank you. http://en.wikipedia.org/wiki/Fibonacci_number <-- the fibonacci number if you dont know it
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "How many numbers do you want generated in the Fibonacci sequence.\n";
int num;
cin >> num;
int num1 = 0, num2 = 1;
int sum;
int f = 1;
while (f <= num)
{
cout << num1 << setw(5) << num2 << setw(5);
sum = num1 + num2;
num1 = sum;
num2 = sum + num2;
++f;
}
}
Loop something like:
while ( f <= num )
{
cout << num1 << setw(5);
if ( ++f <= num )
{
cout << num2 << setw(5);
/// rest of calcs
++f;
}
}
Since you're generating two numbers at a time, you need to increment your counter 2 at a time:
int f = 0;
for (; f < num; f += 2) {
// body
}
And then if num is even, print the last one
if (f == num) {
// print num1
}
In a Fibonacci series, you need a minimun of 2, anything less has no meaning. More than 2 you just have to print the sum.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "How many numbers do you want generated in the Fibonacci sequence.\n";
int num;
cin >> num;
int num1 = 0, num2 = 1;
int sum;
cout << num1 << setw(5) << num2 << setw(5);
int f = 3;
while (f <= num)
{
sum = num1 + num2;
cout << sum << setw(5);
num1 = num2;
num2 = sum;
++f;
}
cout << std::endl;
}