i need help on my coursework
hope someone can help me
My question :
This program performs operations on fraction.
1 : To add fraction
2 : To Subtract fraction
3 : To multiply fraction
4 : To divide fraction
9 : To exit the program
Please Enter Your Choice : 1
For fraction 1
Enter the numerator :
Enter the denominator :
For fraction 2
Enter the numerator :
Enter the denominator :
Result : (fraction1) +(depends on my choice) (fraction2) = (answer)
You can start with the code you know, at least output the questions as shown on the assignment, get the user input and store it in appropriatly named variables and so on. From there we can help you with the acctual operations since this does "feel" differant from doing math operations for fractions on paper.
int main()
void frac()
{
int num1,num2,deno1,deno2,result;
cout << "For fraction 1" <<endl ;
cout << " Enter the numerator: " ;
cin >> num1;
cout << " Enter the denominator: ";
cin >> deno1;
cout << "For fraction 2" <<endl;
cout << " Enter the numerator: " ;
cin >> num2;
cout << " Enter the denominator: " ;
cin >> deno2;
{
void add()
{
//formula
}
void subtract()
{
//formula
}
void multiple()
{
//formula
}
void divide()
{
//formula
}
int menu()
{
int option;
printf(" 1. To add fraction\n");
printf(" 2. To Subtract fraction\n");
printf(" 3. To Multiple fraction\n");
printf(" 4. To Divide fraction\n");
printf(" 9. Thank you for using the FRACTION CALCULATOR program\n");
cout << "\n Please Enter your choice : ";
cin >> option;
-None of your variables are surviving outside of the void frac() function. This would be a good time to practice pointers if you have learned them otherwise you'll need to do the decision making and the output within the frac() function, this isn't wrong per say but rather a little awkward.
-If you are allowed for simplicity sake you may want to try using global variables in this one, in which case you will not have to get into pointers, pass your variables into each function or have your functions return anything.
Ok so I said I would help with the math and here I go:
ADD: You will need to find a common denominator, the easiest way to do this is simply multiply the two denominators together then multiply the numerators by the same amount as their respective denominators. Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//... Code code code
//Assuming variables are global
void Add() {
N1 = N1 * D2;
D1 = D1 * D2;
N2 = N2 * D1;
D2 = D2 * D1;
// Now Add
N1 = N1 + N2;
} //End of Add(...) function
EDIT: Do you need to return the answer as the lowest common denominator? This would be an interesting twist to the assignment.
REEDIT: As for your choice menu, under option 9 replace exit(0); with return 0; and that should be fine. You will also want to tag on a return 0; after system("pause"); I'll skip the obligatory rant as to why you shouldn't ever use the system function for later.
i try redo another one and it complete
but not as done as what i want
1 > when i press the choice as '5' or other than 1,2,3,4,9
it ask me to continue type in the num and den
but what i want is it show out 'Invalid Selection'
2 > when i press the choice as '9'
i want it show me 'Thank you for using the FRACTION CALCULATOR program' only
but there got 'invalid selection'