1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help in 2 programs required???

Discussion in 'C' started by internet_bug, Nov 8, 2008.

  1. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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)
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  3. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    sorry cut that printf and end; label statement in the last
     
  5. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    ooh i got that lcm part clear out, i just want now how to reduce the form p/q
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    well, if p/q is 16/20, what's the lcm, and what's the final result?
     
  7. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    if p/q is the result, it's not final we have to reduce it like 16/20 = 4/5(final result)
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    lcm=least common multiple according to the Wiki, which is 2, but 8/10 isn't the simplest form.
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes, that's right. So how would you do that on paper?
     
  10. internet_bug

    internet_bug New Member

    Joined:
    Sep 16, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Can you spot a number that divides both 34 and 24? Assuming the answer is yes, what would you then do? Would you do it again, potentially? Would you want to do that a fixed number of times regardless of the numbers, or would you repeat the operation until a defined end condition was reached?
     
  12. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Does posting a duplicate thread mean that you can't be arsed working this through and just want someone to give you the answer?
     
  13. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Here.....I have the solution of your first assignment....

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int gcd(int a, int b)
    {
    int c,d,m,g=1;
    for(m=1;m<=(a<b?a:b);m++)
    {
    c=a%m;
    d=b%m;
    if((c==0)&&(d==0)&&(m>g))
    {
    g=m;
    }
    }
    return(g);
    }
    
    void main()
    {
    int a,b,c,d,p,q,r,s;
    printf("Enter a,b,c,d");
    scanf("%d%d%d%d", &a, &b, &c, &d);
    printf("\nFirst fraction %d/%d\nSecond fraction %d/%d\n", a,b,c,d);
    q=(b*d)/gcd(b,d);
    p=a*(q/b) + c*(q/d);
    r=p/gcd(p,q);
    s=q/gcd(p,q);
    printf("\nThe fraction output is=%d/%d\n", r,s);
    getch();
    }
    
    ---------------
    @ r k @
     
  14. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    sorry.....I thought the first one was not posted properly....I am extremely sorry....
     
  15. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Oh well, what the heck. Here's what I got for the second assignment. It was fun to solve:
    Code:
    void go4e_15046b()
    {
    /* ~~~
    1/3 = .(3)             > 0.(3)
    22/5 = 4.4             > 4.4
    1/7 = .(142857)        > 0.(142857)
    3/8 = .375             > 0.375
    45/56 = .803(571428)   > 0.803(571428)
    
    Test your program with the fractions above and the fraction 11/59.
    0.(1864406779661016949152542372881355932203389830508474576271)
    _0.18644067796610169491525423728814 <- Windows Calc
    ~~~
    */
    	int N=11,D=59;
    	printf("Enter N: %d\n",N);
    	//char buf[32];
    	//fgets(buf,30,stdin);
    	//N=atoi(buf);
    	printf("Enter D: %d\n",D);
    	//fgets(buf,30,stdin);
    	//D=atoi(buf);
    
    	printf("\nN=%d, D=%d\n\n",N,D);
    	int A=0;
    	if (N>D)
    	{
    		A=N/D;
    		N-=A*D;
    	}
    	const int COUNT=100;
    	int loop[COUNT+5];
    	char result[COUNT+5]; // +5 to avoid any buffer overflows if we hit COUNT
    	int resi=0;
    	int next=0,loopf=-1;
    	// Long division algorithm
    	for (int max=COUNT; max && loopf<0; max--)
    	{
    		N*=10;
    		for (int i=0; i<next; i++)
    		{
    			if (N==loop[i])
    			{
    				loopf=i;
    				break;
    			}
    		}
    		// we don't want to concatenate N if we've found a loop
    		if (loopf<0)
    		{
    			loop[next++]=N;
    			int H=N/D; // how many times does D go into N?
    			result[resi++]=H+'0';
    			result[resi]=0;
    			N=N-H*D;
    			if (!N) break;
    		}
    	}
    	printf("result before the brackets: '%s'\n",result);
    	if (loopf>=0)
    	{
    		int i;
    		strcat(result,")");
    		// find the end of the result
    		for (i=0; result[i]; i++) ;
    		for (; i>=loopf; i--)
    		{
    			result[i+1]=result[i];
    		}
    		result[i+1]='(';
    	}
    	printf("%d.%s\n",A,result);
    }
    
     
  16. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Brilliant job....xpi0t0s!!! Congrats....It was really fun solving 'em...
     

Share This Page