Thursday, October 29, 2009

Converting to roman equivalent

Write a general-purpose function to convert any given year into its roman equivalent. The following table shows the roman equivalents of decimal numbers: Example: Roman equivalent of 1988 is mdcccclxxxviii Roman equivalent of 1525 is mdxxv



void main()
{
int a;
printf("\nInput the year to get it in Roman numerals");
scanf("%d",&a);
roman(a);
}

roman(int year)
{
if(year>=1000)
{
printf("m");
roman(year-1000);
}

else if(year>=500)
{
printf("d");
roman(year-500);
}

else if(year>=100)
{
printf("c");
roman(year-100);
}

else if(year>=50)
{
printf("l");
roman(year-50);
}

else if(year>=10)
{
printf("x");
roman(year-10);
}

else if(year>=5)
{
printf("v");
roman(year-5);
}

else if(year>=1)
{
printf("i");
roman(year-1);
}


}

Checking of point in triangle(Chap5[J](j))

Write a function to compute the distance between two points and use it to develop another function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use these functions to develop a function which returns a value 1 if the point (x, y) lines inside the triangle ABC, otherwise a value 0.


To check if point p inside triangle:













Calculate length AB,BC,AC
Then calculate area of triangle APB,APC,BPC
Point P is inside triangle if Area( APB + APC + BPC)=Area triangle ABC









#include maths.h (include the the two arrows)
float geodis(int,int,int,int);
float areat(float,float,float);
int pcheck(int,int,int,int,int,int,int,int,float);
void main()
{

float dis1,dis2,dis3,area;
int a,b,c,d,e,f,g,h,i=0;

printf("\nInput the 3 coordinates x1,y1,x2,y2,x3,y3");
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
dis1=geodis(a,b,c,d);
dis2=geodis(a,b,e,f);
dis3=geodis(c,d,e,f);
area=areat(dis1,dis2,dis3);
printf("\nThe area of the triangle is %f",area);
printf("\nEnter a point(x,y) to check if it lies in the triangle");
scanf("%d%d",&g,&h);
i=pcheck(a,b,c,d,e,f,g,h,area);
printf("\nThe value of i now is %d",i);
if(i==1)
printf("\nThe point lies inside the triangle");
else
printf("\nThe point lies outside the triangle");


}




float geodis(int x1,int y1,int x2,int y2)
{
float distance,x3,y3;
x3=x2-x1;
y3=y2-y1;
distance=sqrt(pow(x3,2)+pow(y3,2));
return(distance);
}

float areat(float a,float b,float c)
{
float area,s;
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return(area);
}

pcheck(int a,int b,int c,int d,int e,int f,int g,int h,float area)
{

float area1,area2,area3;
float dis1,dis2,dis3,dis4,dis5,dis6;
int total;
dis1=geodis(a,b,e,f);
dis2=geodis(c,d,e,f);
dis3=geodis(a,b,c,d);
dis4=geodis(a,b,g,h);
dis5=geodis(e,f,g,h);
dis6=geodis(c,d,g,h);
area1=areat(dis4,dis3,dis6);
area2=areat(dis2,dis5,dis6);
area3=areat(dis1,dis5,dis4);
printf("\nArea in pcheck is %f",area);
printf("\nArea 1,2,3 is %f",area1+area2+area3);
total=area1+area2+area3-area;
printf("\nTotal is %d",total);
if(total==0)
return(1);

else
return(0);
}

Area of Triangle

If the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by
area = squareroot(S*(s-a)*(s-b)*(s-c))
where, S = ( a + b + c ) / 2


#include math.h (include 2 arrows)
void main()
{
float areat(float,float,float);
float x,y,z,t;
printf("\nInput the three sides of the triangle");
scanf("%f%f%f",&x,&y,&z);
t=areat(x,y,z);
printf("\nThe area of the triangle is %f",t);
}

float areat(float a,float b,float c)
{

float s,area;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return(area);
}

Circular shift

Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10 after circular shift y = 5, z = 8 and x = 10. Call the function with variables a, b, c to circularly shift values.

void main()
{
int d,e,f;
printf("\nInput 3 digits to circular shift it(x,y,z)");
scanf("%d%d%d",&d,&e,&f);
circs(&d,&e,&f);
printf("\The shifted values are x=%d, y=%d, z=%d",d,e,f);
}

circs(int *x, int *y, int *z)
{
int a,b,c;
a=*z;
b=*x;
c=*y;
*x=a;
*y=b;
*z=c;
}

Converting decimal to binary

Write a function to find the binary equivalent of a given decimal and display it.

Reference:
http://en.wikipedia.org/wiki/Binary_numeral_system

From the above article, the pattern is that for every power of 2(i.e 2²,2³), the number of digits in the binary increases by 1. Therefore the pattern is 2¹(decimal)= 10¹(binary)
2²(decimal)=10² (binary)
2³(decimal)=10³(binary) and so on.




#include math.h (include arrows)
void main()
{
int a,b;
printf("\nInput a number to find its binary equivalent");
scanf("%d",&a);
b=bina(a);
printf("\nThe binary equivalent of %d is %d",a,b);
}
/*Let our x be 5*/
bina(int x)
{
int a,b,c,d;

for(d=0;x>0;x-=c)
{

for(a=2,b=0,c=0;x>=c;b++)
{
if(x==0)
return(0);


/*|First loop|For x= 5, the value of c will reach 8 which is 2power3 and thus b=3*/
/*|Second loop|For x=1, the value of c will reach 2 which is 2power1 and thus b=1*/

c=pow(a,b);
}
/*|First loop|When we exit the loop b++ is executed so b=4*/
/*|Second loop|When we exit loop b++ is executed so b=2*/

b=b-2;
/*|First loop|b=2*/
/*|Second loop|b=0*/
c=pow(a,b);
/*|1st|c=4*/
/*|2nd|c=1*/
d=d+pow(10,b);
/*|1st|d=100*/
/*|2nd|d=100+1=101*/
}
/*|1st|On exiting the loop x=x-c: 5-4=1 so x =1*/
/*|2nd|On exiting loop x=x-c=1-1=0 so the for loop is exited since x must be >0*/

return(d);
}


(Without explanation)


#include math.h (include arrows)
void main()
{
int a,b;
printf("\nInput a number to find its binary equivalent");
scanf("%d",&a);
b=bina(a);
printf("\nThe binary equivalent of %d is %d",a,b);
}

bina(int x)
{
int a,b,c,d;

for(d=0;x>0;x-=c)
{

for(a=2,b=0,c=0;x>=c;b++)
{
if(x==0)
return(0);



c=pow(a,b);
}

b=b-2;
c=pow(a,b);
d=d+pow(10,b);
}

return(d);
}