0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Does posting a duplicate thread mean that you can't be arsed working this through and just want someone to give you the answer?
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
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 @
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
sorry.....I thought the first one was not posted properly....I am extremely sorry....
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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);
}
0
back from retirement's Avatar, Join Date: Nov 2008
Contributor
Brilliant job....xpi0t0s!!! Congrats....It was really fun solving 'em...