0

Hi guys I'm rather new to programming and working my way through Stroustrup's "Programming, Principles and Practice Using C++" and I've come to a complete standstill at the end of Chapter 3 with an exercise asking you to write a piece of code that does a number of calculations involving 2 numbers which includes finding the ratio of the numbers. Unfortunately this hasn't been covered at all in the book and I'm tearing my hair out trying to figure it out by myself, only able to find examples of code way to advanced for my small little brain.

The code I have at the moment is:

 double ratio;
    if (val2 > val1)
        ratio = (val2 / val1);
    if (val2 < val1)
        ratio = (val1 / val2);
    cout << "The ratio of " << val1 << " and " << val2 << " is 1:" << ratio << '\n';

which works fine for numbers that equate to a whole ratio (e.g. 100 and 25) however despite me setting the variable "ratio" as a double it removes any decimals from the answer in cases of non whole number ratios. Can anyone tell me where I'm going wrong?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Leonjwolf
  • 13
  • 1
  • 1
  • 2
  • This isn't the problem, but `ratio = (val2 / val1)` doesn't need parentheses. – Pete Becker Jul 12 '16 at 15:45
  • `((double) val2) / val1`; *integer* division returns *integer*, e.g. `7/2 == 3` and `7 % 2 == 1` (remainder) when `7.0 / 2 = 3.5` – Dmitry Bychenko Jul 12 '16 at 15:45
  • What are the types of `val1` and `val2`? (Yes, I'm pretty sure I know the answer, but this is the first thing you should look at when you get surprising behavior) – Pete Becker Jul 12 '16 at 15:46
  • @DmitryBychenko thank you sorted it completely, and thanks to the rest of you sorry for such a simple question – Leonjwolf Jul 12 '16 at 15:52
  • @Leonjwolf: the question is quite OK, it often appear to be a surprising fact that `7/2` and `7.0/2.0` in C, C++, C#, Java return different answers when math ensures that the formulae are equal – Dmitry Bychenko Jul 12 '16 at 15:58

6 Answers6

5

When dividing integers the result is integer (integer arithmetics is used):

11 / 2 == 5
11 % 2 == 1 /* remainder */

and when dividing floating point values the result is floating point as well:

11.0 / 2 == 5.5
11 / 2.0 == 5.5
((double) 11) / 2 == 5.5

In your case

 double ratio = (val2 / val1);

you have an integer division and only after the disvison performed the outcome of it is cast to double. You can either declare val2 and val1 as double:

double val1; 
double val2;

or cast at least one argument of the ratio to double:

double ratio = ((double)val2) / val1; 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

The fact that result type is double doesn't matter if the original division is performed on integral types (truncating the decimal part).

So to solve your problem, either:

  • Use a floating point type for the input numbers as well
  • Cast one of the numbers to a floating point type before division
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
0

I did the whole problem from Stroustrup's "Programming, Principles and Practice Using C++. Here is the codes although no comments.

int main()
{
/** --------Numbers-----*/
int val1;
int val2;
double largest; //I'll store here the largest value 
double smallest; //I'll store here the smallest value


cout<< " Enter two Numbers to play with\n";
while(cin>> val1>>val2){
    if(val1<val2){
            cout<< "smallest: "<<val1<<endl;
            cout<< "largest: "<<val2<<endl;
           //If the above argument succeeds, largest and smallest will get their values
            largest=val2;  
            smallest=val1;}
    if(val1>val2){
            cout<< "smallest: "<<val2<<endl;
            cout<< "largest: "<<val1<<endl;
          //If the above argument succeeds, largest and smallest will get their values
            largest=val1;
            smallest=val2;}

    int their_sum=val1+val2;
    int their_product=val1*val2;
    int their_diff=val1-val2;
    double ratio1; 
    ratio1=largest/smallest;
    cout<<"Sum: "<<their_sum<<endl;
    cout<<"Difference: "<<their_diff<<endl;
    cout<<"Product: "<<their_product<<endl;
    cout<<"Ratio: "<<ratio1;
                     }
 return 0;
}

There is nothing new in this code, everything was covered in the previous chapters.

Sadiki
  • 91
  • 1
  • 4
0

If at all you need ratio of two numbers say a,b in the form of n:m (where n>=1) then simply find the GCD(a,b) and divide a,b with this result.

eg:

a=4,b=6; 
GCD(a,b)=2; 
n=4/2=>2 
m=6/2=>3 
so ratio of 4 and 6 is 2:3
Kishan Mehta
  • 2,598
  • 5
  • 39
  • 61
0
 #include<iostream>
 using namespace std;

 class Test
 {
     public:
     void check()
     {
          int x,y;
          cout<<"Enter 1st number";
          cin>>x;
          cout<<"Enter 2nd number";
          cin>>y;
          int a;
          int d=  gcd(x,y);
          cout<< x/d << " : " << y / d << endl;      
     }

      int gcd(int x, int y) // 14, 21
      {
          int d;
          if(y>x)
          {
              y=x+y; 
              x=y-x; 
              y=y-x; 
          }
          for(int i=1; i<=y; i++)
          {
              if(x%i==0 && y%i==0 )
              {
                  d=i;
              }
          }
          return d;
       }
   };


 int main()
 {
     Test t;
     t.check();
     return 0;
 }
-2
#include <iostream>

using namespace std;

int main()
{
int val1,val2;

cout << " Enter two integer values followed by enter" << endl << endl;
cin >> val1;
cin >> val2;
    if(val1 < val2) // To determine which value is larger and which one is smaller
    {
        cout << val1 << " is smaller than" << val2 << endl << endl << "And"                  << val2 << " is larger than " << val1 << endl<<endl;
            }   

    enter code here
    else if( val2 < val1)
    {
        cout<<val2 <<" is smaller than"<< val1<<endl<<endl<<"And"<< val1 << " is larger than "<< val2<< endl << endl;
    }

cout << "The sum of "<< val1<<" and "<<val2<<" is "<< val1+val2<<endl<<endl;
// diplaying the sum of the two numbers

    enter code here

cout << " The difference between "<<val1<< " and "<<val2<< " is " << val1-val2<<endl;
// displays the difference of val2 from val1
cout << " The difference between "<<val2<< " and "<<val1<< " is " << val2-val1<<endl;
// displays thr difference of val1 fromval2

    enter code here
    enter code here

cout << " The product of " <<val1<< " and " << val2<< " is " << val1*val2<< endl<<endl;
// displaying the product of val1 and val2

    enter code here
    enter code here
    enter code here

// now to diplay the ratio of the two numbers
double ratio1;
cout << " The ratio of "<<val1<<" and "<<val2<<" is ";
       if(val1 < val2)
       {
         ratio1= ((double)val2) /val1;
           cout <<  ratio1;
       }
       else if(val1 > val2)
        {
          ratio1= ((double)val1) /val2;
           cout <<  ratio1;
       }
}
madlymad
  • 6,367
  • 6
  • 37
  • 68
  • 1
    Welcome to SO. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) to improve the quality of your answer.. – thewaywewere Apr 22 '17 at 13:39