Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help in 2 programs required??? (http://www.go4expert.com/forums/help-2-programs-required-t15046/)

internet_bug 8Nov2008 09:39

Help in 2 programs required???
 
Well we got an assignment of 20 questions, i am able to solve 18 of them but these 2 questions are getting off my brain, can anyone please help me to make these


15. Write an algorithm that inputs two fractions in the form a/b and c/d, and outputs their sum in the form p/q cancelled down to its simplest form.

--------------------

19. FRACTIONS TO DECIMALS: Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator, that prints out the decimal representation. If the decimal representation has a repeating sequence of digits, it should be indicated by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as .(3), and 41/333 = .123123123...is denoted as .(123).

Typical conversions are:

1/3 = .(3)
22/5 = 4.4
1/7 = .(142857)
3/8 = .375
45/56 = .803(571428)

Test your program with the fractions above and the fraction 11/59.

Sample Run

ENTER N,D : 1,7

1/7 = .(142857)

xpi0t0s 8Nov2008 14:09

Re: Help in 2 programs required???
 
Well, how would you do them on paper?

If a/b=14/20 and c/d=16/36, what's p/q (I got 103/90)? Show your working, and we can help you translate that working into an algorithm. Also show how far you got and where you're stuck, and we can help you with the next step.

Same goes for the second one. This one's a lot more interesting! Seems the key to this is to start with the long division algorithm you followed way back, you know where you write it out and solve it as follows:
Code:

    14...etc
 ----------
7)1.0000000
    7
  ---
    30
    28
    --
    2...etc

As you perform a couple of example divisions on paper, e.g. 1/7 and 3/8, can you see anything that your program should look for, in order to determine (a) if there is a loop and if so where from; (b) if the decimal expansion terminates.

internet_bug 8Nov2008 15:11

Re: Help in 2 programs required???
 
Quote:

Originally Posted by xpi0t0s
Well, how would you do them on paper?

If a/b=14/20 and c/d=16/36, what's p/q (I got 103/90)? Show your working, and we can help you translate that working into an algorithm. Also show how far you got and where you're stuck, and we can help you with the next step.

Same goes for the second one. This one's a lot more interesting! Seems the key to this is to start with the long division algorithm you followed way back, you know where you write it out and solve it as follows:
Code:

    14...etc
 ----------
7)1.0000000
    7
  ---
    30
    28
    --
    2...etc

As you perform a couple of example divisions on paper, e.g. 1/7 and 3/8, can you see anything that your program should look for, in order to determine (a) if there is a loop and if so where from; (b) if the decimal expansion terminates.

well for the 1st program i have done some and have got a/b & c/d part clear out, see the code

Code:

#include <stdio.h>
#include <conio.h>

void main()
{
int a,b,c,d,i,lcm;
char ch;
clrscr();
printf("Enter 1st fraction: ");
scanf("%d%c%d",&a,ch,&b);
printf("Enter 2nd fraction: ");
scanf("%d%c%d",&c,ch,&d);
printf("Result: %d / %d",den,lcm);
 end:;
getch();
}

now how can i take the lcm and how am i gona reduce the result to it's lowest forum??, here i am stuck

internet_bug 8Nov2008 15:14

Re: Help in 2 programs required???
 
sorry cut that printf and end; label statement in the last

internet_bug 8Nov2008 15:16

Re: Help in 2 programs required???
 
ooh i got that lcm part clear out, i just want now how to reduce the form p/q

xpi0t0s 8Nov2008 15:29

Re: Help in 2 programs required???
 
well, if p/q is 16/20, what's the lcm, and what's the final result?

internet_bug 8Nov2008 15:33

Re: Help in 2 programs required???
 
if p/q is the result, it's not final we have to reduce it like 16/20 = 4/5(final result)

xpi0t0s 8Nov2008 15:34

Re: Help in 2 programs required???
 
lcm=least common multiple according to the Wiki, which is 2, but 8/10 isn't the simplest form.

xpi0t0s 8Nov2008 15:35

Re: Help in 2 programs required???
 
Yes, that's right. So how would you do that on paper?

internet_bug 8Nov2008 15:51

Re: Help in 2 programs required???
 
suppose i enter 8/12 + 6/8, now if i do it on paper it's lcm is 24, which my code is giving and the result is 34/24(which my code is giving), this is not the simplest form, it can further be divided, and reduced to 17/12, which i wanted, so how to reduce 34/24


All times are GMT +5.5. The time now is 11:27.