Assignment 3 : Solution
Exercise 1
#include <stdio.h>
#include <math.h>
double distance ( double x1 , double y1 , double x2 , double y2 )
{
double dsqr;
dsqr = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
return(sqrt(dsqr));
}
double radius ( double a , double b , double c )
{
double rsqr;
rsqr = (a*a+b*b)/4 - c;
if (rsqr < 0) return (-1);
return (sqrt(rsqr));
}
int main ()
{
double a1, b1, c1, a2, b2, c2;
double r1, r2, d, r, R;
int i;
printf("a1 = "); scanf("%lf",&a1);
printf("b1 = "); scanf("%lf",&b1);
printf("c1 = "); scanf("%lf",&c1);
printf("a2 = "); scanf("%lf",&a2);
printf("b2 = "); scanf("%lf",&b2);
printf("c2 = "); scanf("%lf",&c2);
printf("\n");
r1 = radius(a1,b1,c1);
printf("Radius of the first circle = %f\n", r1);
if (r1 < 0) printf("(a1,b1,c1) does not define a circle.\n");
r2 = radius(a2,b2,c2);
printf("Radius of the second circle = %f\n", r2);
if (r2 < 0) printf("(a2,b2,c2) does not define a circle.\n");
d = distance(-a1/2,-b1/2,-a2/2,-b2/2);
if ((r1>=0)&&(r2>=0)) {
printf("Distance between the centers = %f\n", d);
R = r1 + r2;
if (r1 > r2) r = r1 - r2; else r = r2 - r1;
if (d == R)
printf("Case 1(a) : The two circles touch from outside.\n");
else if (d == r)
printf("Case 1(b) : The two circles touch from inside.\n");
else if ((d > r) && (d < R))
printf("Case 2 : The two circles intersect.\n");
else if (d > R)
printf("Case 3(a) : The two circles do not intersect (centers are too distant).\n");
else /* if (d < r) */
printf("Case 3(b) : The two circles do not intersect (centers are too close).\n");
}
printf("\n");
}
Output
a1 = 1
b1 = 1
c1 = -1
a2 = 1
b2 = 1
c2 = 1
Radius of the first circle = 1.224745
Radius of the second circle = -1.000000
(a2,b2,c2) does not define a circle.
a1 = 0
b1 = 2
c1 = -3
a2 = -2
b2 = 2
c2 = 1
Radius of the first circle = 2.000000
Radius of the second circle = 1.000000
Distance between the centers = 1.000000
Case 1(b) : The two circles touch from inside.
a1 = 2
b1 = -2
c1 = 1
a2 = 0
b2 = 0
c2 = -9
Radius of the first circle = 1.000000
Radius of the second circle = 3.000000
Distance between the centers = 1.414214
Case 3(b) : The two circles do not intersect (centers are too close).
a1 = -4
b1 = -4
c1 = -2
a2 = -4
b2 = 6
c2 = 11
Radius of the first circle = 3.162278
Radius of the second circle = 1.414214
Distance between the centers = 5.000000
Case 3(a) : The two circles do not intersect (centers are too distant).
a1 = -4
b1 = -6
c1 = 13
a2 = 0
b2 = -6
c2 = 5
Radius of the first circle = 0.000000
Radius of the second circle = 2.000000
Distance between the centers = 2.000000
Case 1(a) : The two circles touch from outside.
a1 = -4
b1 = -4
c1 = 4
a2 = -10
b2 = -4
c2 = 24
Radius of the first circle = 2.000000
Radius of the second circle = 2.236068
Distance between the centers = 3.000000
Case 2 : The two circles intersect.
a1 = 1
b1 = -1
c1 = 0.25
a2 = 0
b2 = -1
c2 = -0.75
Radius of the first circle = 0.500000
Radius of the second circle = 1.000000
Distance between the centers = 0.500000
Case 1(b) : The two circles touch from inside.
a1 = 0.5
b1 = -0.5
c1 = 0.125
a2 = 0
b2 = 0
c2 = 0
Radius of the first circle = 0.000000
Radius of the second circle = 0.000000
Distance between the centers = 0.353553
Case 3(a) : The two circles do not intersect (centers are too distant).
a1 = 0
b1 = 0
c1 = -625
a2 = -36
b2 = -48
c2 = 875
Radius of the first circle = 25.000000
Radius of the second circle = 5.000000
Distance between the centers = 30.000000
Case 1(a) : The two circles touch from outside.
a1 = 0
b1 = 0
c1 = -25
a2 = -7.2
b2 = -9.6
c2 = 35
Radius of the first circle = 5.000000
Radius of the second circle = 1.000000
Distance between the centers = 6.000000
Case 3(a) : The two circles do not intersect (centers are too distant).
Exercise 2
#include <stdio.h>
void baseconv ( int n , int b )
{
/* n is too small. Simply print it and return. */
if (n < b) { printf("%d",n); return; }
/* Recursively print the more significant digits */
baseconv(n/b,b);
/* Finally print the least significant digit */
printf(",%d",n%b);
}
int main ()
{
int n, b;
printf("n = "); scanf("%d", &n);
printf("b = "); scanf("%d", &b);
if ((n < 0)||(b < 2)) {
fprintf(stderr, "Error: Invalid input...\n");
exit(1);
}
printf("(%d)_10 = (",n);
baseconv(n,b);
printf(")_%d\n",b);
exit(0);
}
Output
n = 0
b = 12
(0)_10 = (0)_12
n = 34
b = 56
(34)_10 = (34)_56
n = 78
b = 78
(78)_10 = (1,0)_78
n = 9801
b = 99
(9801)_10 = (1,0,0)_99
n = 15626
b = 25
(15626)_10 = (1,0,0,1)_25
n = 65535
b = 8
(65535)_10 = (1,7,7,7,7,7)_8
n = 536870911
b = 2
(536870911)_10 = (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)_2
n = 536870912
b = 16
(536870912)_10 = (2,0,0,0,0,0,0,0)_16
n = 1111111111
b = 11
(1111111111)_10 = (5,2,0,2,1,4,2,5,0)_11
n = 1273412531
b = 47
(1273412531)_10 = (5,25,45,10,28,30)_47