Access over 35 million academic & study documents

4 Write the function to reduce the numerator and denominator in the

Content type
User Generated
Rating
Showing Page:
1/2
4. Write the function to reduce the numerator and
denominator in the Rat class to lowest terms.
Solution
#include <iostream>
using namespace std;
void reduce(int &n, int &d){
int min = (n<d)?n:d;
for(int i=min; i>0; i--){
if(n%i==0 && d%i==0){
n /= i;
d /= i;
break;
}
}
}
int main(){
int n = 24, d = 1024;
reduce(n, d);
cout << n << endl << d;
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/2

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
4. Write the function to reduce the numerator and denominator in the Rat class to lowest terms. Solution #include using namespace std; void reduce(int &n, int &d){ int min = (n0; i--){ if(n%i==0 && d%i==0){ n /= i; d /= i; break; } } } int main(){ int n = 24, d = 1024; reduce(n, d); cout ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
This is great! Exactly what I wanted.

Studypool
4.7
Indeed
4.5
Sitejabber
4.4