Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding ratio of 2 numbers in C++

Tags:

c++

math

I'm currently learning C++ using PPP by Bjarne Stroustrup. There is a Practice question:

Write a program that prompts the user to enter two integer values. Store these values in int variables named val1 and val2. Write your program to determine the smaller, larger, sum, difference, product, and ratio of these values and report them to the user.

For ratio I want it to be like The ratio of 500 and 700 is 5:7. I known the answer of this could be

double ratio = 500/700;

But I want to show this as 5:7 rather than 500/700 = 0.714.

How can I do that?

Edit: - That's how I did it.

#include <iostream>
#include <cmath>

void Ratio(double val1, double val2)
{
    double temp1 = val1;
    double temp2 = val2;
    for(int num = 0; num < 20; num++)
    {
        if(num == 0 || num == 1)
            continue;

        if(std::fmod(temp1, num) == 0 && std::fmod(temp1, num) == 0) 
        {
            temp1 /= num;
            temp1 /= num;
            num = 0;
        }
    } 
    std::cout<<"Ratio of " << val1 << " and " << val2 << " is " << val1 << ":" << val2<< std::endl;
}

int main()
{
    Ratio(500, 700);
}

I'm still a beginner in programming and this really is something for me. Anyone have a better solution for this.

like image 993
Abhay Kumar Avatar asked Mar 02 '23 13:03

Abhay Kumar


2 Answers

As @Dominique points out:

You are working with the wrong algorithm: In order to find the ratio between two numbers a and b, you are doing:

calculate a/b; 
do something with the result.

This does not work, as computers don't understand rational numbers (in mathematics, 1/3 is different from 0.333333333, but for a computer both are equal because computer cannot handle infinite decimals).

What you want is to simplified a fraction (reducing it to the simplest form). To do this, you can divide both a and b by gcd(a,b). gcd() of two numbers can be calculated with Euclidean algorithm, or by using directly the __gcd() function.

Code:

#include <iostream>
#include <cmath>
using namespace std;

int gcd(int a, int b)
{
    if (b == 0) {return a;}
    return gcd(b, a%b);
}

pair<int, int> ratioFunc(int a, int b)
{
    pair<int, int> ret;
    if (a == 0)
    {
        ret.first = 0; ret.second = b;
    }
    else
    {
        int gc = gcd(abs(a),abs(b));
        a /= gc; b /= gc;
        ret.first = a; ret.second = b;
    }
    return ret;

}

int main()
{
    int a,b; cin >> a >> b;
    if (b == 0) {cout << "No answer";}
    else {auto res = ratioFunc(a,b); cout << "The ratio of " << a << " and " << b << " is " << res.first << ":" << res.second;}
}

Output :

500 700
The ratio of 500 and 700 is 5:7

Output (with negative integers):

-33 27
The ratio of -33 and 27 is -11:9

In case you haven't worked with pair yet, you can simply print immediately:

void ratioFunc(int a, int b)
{
    if (b == 0) {cout << "No answer";}
    else
    {

        if (a == 0) {cout << "The ratio of " << a << " and " << b << " is " << a << ":" << b; return; }

        int x = a, y = b; //Storing a,b for printing
        int gc = gcd(abs(a),abs(b));
        a /= gc; b /= gc;

        cout << "The ratio of " << x << " and " << y << " is " << a << ":" << b;
    }
}
like image 129
silverfox Avatar answered Mar 11 '23 01:03

silverfox


You are working with the wrong algorithm: in order to find the ratio between two numbers a and b, you are doing:

calculate a/b;
do something with the result.

This does not work, as computers don't understand rational numbers (in mathematics, 1/3 is different from 0.333333333, but for a computer both are equal because computer cannot handle infinite decimals).

Therefore you need to adopt another algorithm, which is the following:

calculate the greatest common divisor of a and b (ggd(a,b))
Show: a / ggd(a,b)
      b / ggd(a,b)
like image 28
Dominique Avatar answered Mar 11 '23 01:03

Dominique