Code: #include <iostream> #include <ctime> using namespace std; class Fraction { private: int num; // Numerator int den; // Denominator public: void setFraction (int); //sets fraction int Reduce(); //will reduce the fraction to lowest terms void print(); //will print the fraction in the form: numerator / denominator int rand(); //will create random fractions void multiply(); //will multiply the fractions Fraction(); // constructor Fraction(int,int); // 2 Argument Constructor }; Fraction::Fraction() { num=0; den=1; } Fraction::Fraction(int n,int d) { num=n; den=(d==0)? 1 :d; } int rand () { int Num[3]; int Den[3]; int answer; // user input //int Numerator; //int Denominator; srand (time(0)); //Initiate checking for correct answer while (answer != -1) { Num[0] = rand() % 9 + 1; // load the first random number into Num[0] Num[1] = rand() % 9 + 1; // load the second into Num[1] Num[2] = Num[0] * Num[1]; // load the answer into Num[2] Den[0] = rand() % 9 + 1; // load the first random number into Den[0] Den[1] = rand() % 9 + 1; // load the second into Den[1] Den[2] = Den[0] * Den[1]; // load the answer into Den[2] cout << "\n\n""What is " << Num[0] << "/" << Den[0]<< "*" << Num[1] << "/" << Den[1] <<"?" << endl; cout << "Enter Numerator / Denominator" << endl; cin >>answer ; if ((answer == Num[0] * Num[1]) && (Den[0] * Den[1])); // check against the numbers array { cout << "\n""Correct!"<< endl; //Outcome for correct answer } //else //statement for while loop // GIVE THE CORRECT ANSWER AN MOVE ON //cout << "\nWrong! The correct answer was " << numbers[2] << endl; //Outcome for incorrect answer } return 0; } void multiply(int &num, int &den, int &num2, int &den2) { int calcnum; int calcden; calcnum = num * num2; calcden = den * den2; } void Reduce(int &num, int &den, int &num2, int &den2) { int a, b, c, d, i, j = 0; a = den; b = num; c = den2; d = num2; for (i = a * b; i > 1; i--) { if ((a % i == 0) && (b % i == 0)) { a /= i; b /= i; } } for (j = 50; j > 1; j--) { if ((c % j == 0) && (d % j == 0)) { c /= j; d /= j; } } den = a; num = b; den2 = c; num2 = d; } int Reduce(int a, int b) { int calcden = a; int calcnum = b; if(b == 0) { return a; } else { return Reduce(b, a % b); } } void print(int &num, int &den) { cout << "The reduced and added fraction is " << num << "/" << den << endl; } int main() { int num, den, num2, den2; int calcnum = 0; int calcden = 0; int a = 0; int b = 0; int m = 0; int n = 0; //int i; //int rand (); cout << "What is 3/4 * 3/5?" << endl; //cout << "What is" << num[i]<< "/" << den[i]<< "*" << num2[i]<<"/"<<den2[i]<<"?"<<endl; cout << "enter numerator / denominator" << " " << "Your answer must be in reduced form" << endl; cin >> num >> den; multiply(num, den, num2, den2); Reduce(a, b); print(num, den); cout << endl; return(0); }
What exactly is the problem? We can't read your mind. What input did you give, what output did you get, did it match what you expected, and if not what did you expect? What line of code does it start going wrong? (If you find this, then you stand a good chance of fixing the problem. To find it, you can add printf/cout statements to the code to display values of variables; this will show you if the code is really doing what you expect)
Thanks for the response. I was in a total re-write when my computer went down. I am trying to multiply random fractions then reduce them. I have added my new code. I can not seem to pass the random numerators and denominators to cout. Code: #include <iostream> #include <ctime> using namespace std; class Fraction { private: int num; // Numerator int den; // Denominator public: int num2; int den2; double setNumerator() {return num;} //sets numerator double setDenominator() {return den;} //sets Denominator double setNumerator2() {return num2;} //sets numerator 2 double setDenominator2() {return den2;} //sets Denominator 2 int Reduce(); //will reduce the fraction to lowest terms void print(); //will print the fraction in the form: numerator / denominator /int rand(); //will create random fractions void Result(); //will multiply the fractions Fraction(); // constructor Fraction(int,int); // 2 Argument Constructor //int Fract1(); }; Fraction::Fraction() { num=0; den=1; } Fraction::Fraction(int n,int d) { num=n; den=(d==0)? 1 :d; } double setNumerator (int num) { srand ( time(NULL) ); num = rand() % 9 + 1; return num; } double setDenominator (int den) { srand ( time(NULL) ); den = rand() % 9 + 1; return den; } double setNumerator2 (int num2) { srand ( time(NULL) ); num2 = rand() % 9 + 1; return num2; } double setDenominator2 (int den2) { srand ( time(NULL) ); den2 = rand() % 9 + 1; return den2; } //Fract2.setNumerator(rand() % 9 + 1); //Fract2.setDenominator(rand() % 9 + 1); //Result.setDenominator(Fract1.getDenominator() * Fract2.getDenominator()); void Result(int &num, int &den, int &num2, int &den2) { int calcnum; int calcden; calcnum = num * num2; calcden = den * den2; } int Reduce(int a, int b) { int calcden = a; int calcnum = b; if(b == 0) { return a; } else { return Reduce(b, a % b); } } void print(int &num, int &den) { cout << "The reduced and added fraction is " << num << "/" << den << endl; } int main() { int num = 0, den = 0, num2 = 0, den2 = 0; int calcnum = 0; int calcden = 0; int a = 0; int b = 0; int m = 0; int n = 0; //int i; //int rand (); //cout << "What is << num << "/" << den << "*" <<num2 << "/" << den2<< "?" << endl; cout << "What is" << num<< "/" << den<< "*" << num2<<"/"<<den2<<"?"<<endl; cout << "enter numerator / denominator" << " " << "Your answer must be in reduced form" << endl; cin >> num >> den; //Result(num, den, num2, den2); Reduce(a, b); print(num, den); cout << endl; return(0); }
Could you answer the questions please. That's if you want some help, of course. I went to the effort of thinking through them and typing them in, so your appropriate response is to think through the answers and respond.
By the looks of it the output from Code: int num = 0, den = 0, num2 = 0, den2 = 0; cout << "What is" << num<< "/" << den<< "*" << num2<<"/"<<den2<<"?"<<endl; will be: What is0/0*0/0? Is that what you expected it to display? If not, what did you expect it to display? Did you expect it to display values contained in num, num2, den and den2 some time AFTER this statement is executed (mathematicians who are new to programming often make that mistake)?
the best i can think of right now because you don't say exactly what you want to do is this code Code: #include <iostream> #include <ctime> using namespace std; class Fraction{ private: int num; // Numerator int den; // Denominator public: void setRandomFraction() { num = rand() % 9 + 1; den = rand() % 9 + 1; } //sets random Fraction int getNumerator(){ return num; } int getDenominator(){ return den; } void print(){ //will print the fraction in the form: numerator / denominator cout << "The fraction is " << num << "/" << den << endl; } void Reduce(){//will reduce the fraction to lowest terms int d=maxCommonDivider(num,den); if (d<2) return; num/=d; den/=d; } int maxCommonDivider(int a,int b){ int i,j,gcd=-1; i=abs(a); j=abs(b); while (i>0 && j>0){ if (i>=j) i=i%j; else j=j%i; } gcd=i+j; return gcd; } Fraction(){ // constructor num=0; den=1; } //~Fraction();//destructor Fraction(int n,int d){ // 2 Argument Constructor num=n; den=(d==0)? 1 :d; }; }; int main(){ srand (time(NULL)); int num,den,num1 = 0, den1 = 0, num2 = 0, den2 = 0; Fraction fraction1,fraction2,result; //create fraction 1 fraction1=Fraction::Fraction(); fraction1.setRandomFraction(); //create fraction 2 fraction2=Fraction::Fraction(); fraction2.setRandomFraction(); //gets numerators,denominators of fraction1,fraction2 num1=fraction1.getNumerator(); den1=fraction1.getDenominator(); num2=fraction2.getNumerator(); den2=fraction2.getDenominator(); //create the resulting fraction of fraction1*fraction2 result=Fraction::Fraction(num1*num2,den1*den2); //reduces the fraction result result.Reduce(); cout << "What is" << num1<< "/" << den1<< "*" << num2<<"/"<<den2<<"?"<<endl; cout << "enter numerator / denominator" << " " << "Your answer must be in reduced form" << endl; cin >> num >> den; result.print(); cin>>num; return 0; }