Question:
Need help for writing C++ code?
Marco
2013-03-11 20:31:52 UTC
Having trouble with writing this any help would be appreciated thanks.

Write a program that inputs two fractions (a numerator and a denominator for each). The program should work properly for all fractions, except those that have a zero in either denominator. If the user enters 0 in a denominator, give an error message and exit the program. You will then use these two fractions to do the following:

Add the two fractions together and give the resulting fraction and the decimal equivalent. Remember that you can only add fractions that have the same denominator and for this lab you may just multiple the two denominator together (make sure you appropriately multiply the numerators), to give a common denominator, without worrying about the least common denominator. If the two fractions that were entered were 9/13 and 7/12 the output would be shown as both 199/156 and 1.2756.
Multiply the two fractions together and give the resulting fraction and the decimal equivalent. If the two fractions that were entered were 2/9 and 6/3 the output would be 12/27 and 0.4444
Three answers:
?
2013-03-11 20:35:14 UTC
So what issues are you having in terms of completing this assignment?



You wrote down the homework description, but didnt ask a real question



What part of this assignment dont you understand?



We arent here to do your homework, but if you tell us what you are having trouble with, then we can help
?
2016-12-17 20:08:27 UTC
i'm going to assum which you have a terminal window, or you're sitting in front of a linux computing gadget. when you have logged in you will the two be dealing with a terminal window or a private computing gadget. in case you spot a private computing gadget discover the thank you to start a terminal. interior the terminal you would be able to desire to apply a textual content cloth editor. for some years human beings have argued it extremely is greater constructive vi or emacs, yet now there are others. interior the terminal on the command line type "vi helloworld.cpp" or "emacs helloworld.cpp" or "xemacs helloworld.cpp" or "vim helloworld.cpp" or "gedit hellowrold.cpp" or "nano helloworld.cpp". All of those are occasion of textual content cloth editor in unix. Gedit is like homestead windows notepad, and there are menus to maintain documents. In vi and vim it extremely is not any longer as obtrusive, it extremely is the single that i exploit the main in view which you do no longer would desire to waste time touching your mouse. Whe vi starts you will see a sparkling demonstrate press i to be in interactive mode. type your code, and hist esc : w q this escapes you from interactive mode, and then writes and extremely vi or vim. In xemacs and emacs you should use the mouse like notepad. In nano the instructions are on the bottome of the demonstrate. i desire this helps.
Peter
2013-03-13 09:24:50 UTC
1. In your examples given, THE SECOND ONE, i.e. the last example you gave, IS WRONG.



Here 's the program you need. Remember, separate numerator and denominator by a space when you give input.



#include

using namespace std;



int main()

{

        int n1,d1, n2,d2;

        cout<<"Enter the first fraction num and den, separated by spaces\n";

        cin>>n1>>d1;

        if(d1 == 0){

                cout<<"Invalid fraction, div by zero\n";

                return 0;

        }



        cout<<"Enter the second fraction\n";

        cin>>n2>>d2;

        if(d2 == 0){

                cout<<"Invalid fraction, div by zero\n";

                return 0;

        }

        

        cout<<"Adding two fractions...\n";



        int num,den;

        if(d1 == d2){

                den = d2;

                num = n1 + n2;

        } else{

                den = d1 * d2;

                num = n1*d2 + n2*d1;

        }



        cout<<"Result = "<
        cout<<"Result = "<<(float)num/den<<" (decimal)\n";



        return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...