MC-1208 CPNM Lab Syllabus
MC-1208 CPNM Lab Syllabus
CPNM LAB
SYLLABUS
1. Write a program to read x, y coordinates of 3 points and then calculate the area of a triangle
formed by them and print the coordinates of the three points and the area of the triangle.
What will be the output from your program if the three given points are in a straight line?
2. Write a program, which generates 100 random integers in the range of 1 to 100. Store them
in an array and then print the arrays. Write 3 versions of the program using different loop
constructs. (e.g. for, while, and do while).
3. Write a set of string manipulation functions e.g. for getting a sub-string from a given
position, Copying one string to another, Reversing a string, adding one string to another.
4. Write a program which determines the largest and the smallest number that can be stored
in different data types like short, int, long, float, and double. What happens when you add
1 to the largest possible integer number that can be stored?
5. Write a program, which generates 100 random real numbers in the range of 10.0 to 20.0,
and sort them in descending order.
6. Write a function for transposing a square matrix in place (in place means that you are not
allowed to have full temporary matrix).
7. First use an editor to create a file with some integer numbers. Now write a program, which
reads these numbers and determines their mean and standard deviation.
8. Given two points on the surface of the sphere, write a program to determine the smallest
arc length between them.
9. Implement bisection method to find the square root of a given number to a given accuracy.
10. Implement Newton Raphson method to det. a root of polynomial equation.
11. Given table of x and corresponding f(x) values, Write a program which will determine f(x)
value at an intermediate x value by using Lagrange’s interpolation/
12. Write a function which will invert a matrix.
13. Implement Simpson’s rule for numerical integration.
14. Write a program to solve a set of linear algebraic equations.
1.)Write a program to read x, y coordinates of 3 points and then calculate the area of a triangle
formed by them and print the coordinates of the three points and the area of the triangle. What
will be the output from your program if the three given points are in a straight line?
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
float area;
int x1,x2,x3,y1,y2,y3;
clrscr();
scanf("%d %d",&x1,&y1);
scanf("%d %d",&x2,&y2);
scanf("%d %d",&x3,&y3);
area=(0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));
if(area==0)
else if(area<0)
else
getch();
Expected Output:
73
83
95
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<time.h>
void main()
int i=1,choice,k[100];
clrscr();
scanf("%d",&choice);
srand(time(0));
rand();
switch(choice)
case 01:
for(i=0;i<=100;i++)
k[i]=rand()%100;
printf("%d\t",k[i]);
break;
case 02:
while(i<=100)
k[i]=rand()%100;
printf("%d\t",k[i]);
i++;
break;
case 03:
do
k[i]=rand()%100;
printf("%d\t",k[i]);
i++;
while(i<=100);
break;
default:
break;
getch();
Expected Output:
1.for
2.while
3.do while
15 3 47 27 2 86 23 37 73 42
61 76 81 98 19 50 26 32 28 16
74 57 2 87 59 90 25 15 15 78
66 80 30 55 43 86 62 89 94 41
7 90 56 60 88 92 64 12 90 47
63 36 6 62 86 56 64 35 13 12
86 58 62 86 23 11 79 22 77 42
84 89 9 36 32 63 77 95 90 41
94 58 50 55 11 50 9 25 13 59
12 92 63 35 16 80 65 35 96 68
17
1.for
2.while
3.do while
82 82 40 35 48 10 48 95 55 92
17 0 65 37 27 98 55 56 5 65
25 52 56 79 30 30 99 70 34 47
81 65 5 17 28 19 65 35 14 21
63 21 54 25 66 58 9 71 57 13
32 8 89 5 29 84 57 89 1 39
92 38 9 59 16 50 41 33 38 38
12 75 6 95 45 77 40 21 61 22
10 84 78 48 56 84 10 45 89 59
40 40 32 32 71 9 58 38 76 5
1.for
2.while
3.do while
50 98 0 24 92 12 36 75 48 15
55 44 89 6 78 85 50 60 23 88
11 74 26 32 26 92 8 75 27 37
12 96 24 28 39 33 77 10 92 79
10 61 7 88 9 70 64 38 99 50
43 94 99 93 91 67 62 4 93 83
69 20 95 86 2 92 47 78 4 83
44 82 62 85 66 75 3 23 86 91
61 24 18 79 9 15 41 32 49 26
82 46 88 45 96 9 45 39 18 74
3.) Write a set of string manipulation functions e.g. for getting a sub-string from a given
position, Copying one string to another, Reversing a string, adding one string to another.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
int i,j,k=0,l=0,l1,l2;
char a[30],b[30],c[30],d[30],e[30];
clrscr();
scanf("%s %s",&a,&c);
scanf("%d %d",&l1,&l2);
for(i=0;a[i]!='\0';i++)
l++;
for(j=0;a[j]!='\0';j++)
b[j]=a[j];
b[j]='\0';
for(j=l-1;j>=0;j--)
d[k]=a[j];
k++;
d[k]='\0';
a[l+i]=c[i];
if(l1<=l&&l2<=l)
for(i=l1-1,j=0;i<l2;i++)
e[j]=a[i];
j++;
getch();
Expected Output:
enter string:
cpnmlab classes
17
length of string 7
#include<stdio.h>
#include<limits.h>
#include<values.h>
#include<math.h>
#include<float.h>
#include<conio.h>
void main()
clrscr();
getch();
Expected output:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int i,j;
float a[150],temp;
clrscr();
for(i=0;i<100;i++)
{
a[i]=10.0+random(1000)/100.0;
}
printf("the generated 100 random real numbers : \n");
for(i=0;i<100;i++)
{
printf("%.2f \t",a[i]);
}
/*sorting*/
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("the generated 100 random real numbers in descending order: \n");
for(i=0;i<100;i++)
{
printf("%.2f \t",a[i]);
}
getch();
return 0;
}
Expected output:
#include<stdio.h>
#include<conio.h>
void main()
int i,j,r,c,a[10][10];
clrscr();
scanf("%d",&r);
scanf("%d",&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d",a[i][j]);
printf("\t");
printf("\n");
for(j=0;j<r;j++)
printf("%d\t",a[j][i]);
printf("\n");
getch();
Expected Output:
345
798
Elements are:
1 3 6
3 4 5
7 9 8
transpose matrix is :
1 3 7
3 4 9
6 5 8
7.)First use an editor to create a file with some integer numbers. Now write a program, which
reads these numbers and determines their mean and standard deviation
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
FILE *fp;
int n,count=0,sum=0,sumsq=0;
float mean=0.0,sd=0.0;
clrscr();
fp=fopen("mean.dat","w");
if(fp==NULL)
exit(0);
while(scanf("%d",&n)!=EOF)
fprintf(fp,"%d\t",n);
fclose(fp);
fp=fopen("mean.dat","r");
while(fscanf(fp,"%d",&n)!=EOF)
count++;
sum=sum+n;
printf("%d\t",n);
sumsq+=n*n;
}
fclose(fp);
mean=sum/count;
sd= sqrt((sumsq/count)-(mean*mean));
getch();
return 0;
Expected output:
1234567
1 2 3 4 5 6 7
Mean = 4.00
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int x,y,z,x1,y1,z1,x2,y2,z2;
double r,l,d,A;
clrscr();
printf("Enter x,y,z,co-ordinates of the centre of the sphere\n");
scanf("%d %d %d",&x,&y,&z);
printf("Enter x1,y1,z1,co-ordinates\n");
scanf("%d %d %d",&x1,&y1,&z1);
printf("Enter x2,y2,z2,co-ordinates\n");
scanf("%d %d %d",&x2,&y2,&z2);
r=sqrt(pow((x-x1),2)+pow((y-y1),2)+pow((z-z1),2));
d=sqrt(pow((x1-x2),2)+pow((y1-y2),2)+pow((z1-z2),2));
A=2*asin(d/(2*r));
l=(r*A);
printf("The smallest arc length between the given points on the sphere is %f limits",l);
getch();
return 0;
}
Expected output:
123
Enter x1,y1,z1,co-ordinates
323
Enter x2,y2,z2,co-ordinates
103
The smallest arc length between the given points on the sphere is 3.141593 limits
9.)Implement bisection method to find the square root of a given number to a given accuracy
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float n;
float f(float x)
return(x*x-n);
main()
{float a,b,epsilon,xmid;
clrscr();
scanf("%f",&n);
scanf("%f,%f,%f",&a,&b,&epsilon);
if(f(a)*f(b)>0)
exit(0);
xmid=(a+b)/2;
if(f(xmid)==0)
break;
if(f(a)*f(xmid)<0)
b=xmid;
else
a=xmid;
xmid=(a+b)/2;
getch();
return 0;
Expected output:
25
4,6,0.0001
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i=1;
float x0,x1,f0,f1;
clrscr();
printf("enter a,b,c,x0: \n");
scanf("%d %d %d %f",&a,&b,&c,&x0);
printf("iteration x0 \t\t x1\t\t f0(x0)\t\t f1(x0)\t\t");
while(i<10)
{
f0=(a*x0*x0)+(b*x0)+c;
f1=(2*a*x0)+b;
x1=x0-(f0/f1);
printf("\n %d\t %f\t %f\t %f\t %f\t \n",i,x0,x1,f0,f1);
if(x0==x1)
break;
else
x0=x1;
i++;
}
printf("appropriate value of root is %f",x0);
getch();
}
Expected output:
enter a,b,c,x0:
2463
iteration x0 x1 f0(x0) f1(x0)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j;
float a[20][10],s=0,t=1,x;
clrscr();
printf("Enter number of values:");
scanf("%d",&n);
printf("Enter values of x and f(x) : \n");
for(i=0;i<n;i++)
{
scanf("%f %f",&a[0][i],&a[1][i]);
}
printf("Enter intermediate x value:");
scanf("%f",&x);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
t=t*((x-a[0][j])/(a[0][i]-a[0][j]));
}
s=s+a[1][i]*t;
t=1;
}
printf("f(%f)=%f\n",x,s);
getch();
}
Expected output:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
void inverse(float a[10][10],int n)
{
int i,j,k;
float b[10][10],r;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
b[i][j]=0.0;
b[i][i]=1.0;
}
for(k=0;k<n;k++)
for(i=0;i<n;i++)
{
if(i==k)
continue;
r=a[i][k]/a[k][k];
for(j=0;j<n;j++)
{
a[i][j] -=r*a[k][j];
b[i][j] -=r*b[k][j];
}
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
b[i][j]=b[i][j]/a[i][i];
printf("the inverse matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3f \t",b[i][j]);
printf("\n");
}
}
main()
{
int i,j,k,n;
float a[10][10];
clrscr();
printf("enter order of the matrix:\n");
scanf("%d",&n);
printf("enter the elements of matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
inverse(a,n);
getch();
return 0;
}
Expected output:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float f(float x)
{
return(1/(1+x));
}
main()
{
int i,n;
float s1=0,s2=0,a,b,h,x,integral;
clrscr();
printf("Enter a and b limits \n");
scanf("%f %f",&a,&b);
printf("Enter no of steps: \n");
scanf("%d",&n);
h=(b-a)/n;
x=a;
for(i=1;i<n;i++)
{
x+=h;
if(i%2==0)
s1+=2*f(x);
else
s2+=4*f(x);
}
integral=(h/3)*(f(a)+s1+s2+f(b));
printf("approximate value is %f",integral);
getch();
return 0;
}
Expected output:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define esp 0.0001
#define x1(x2,x3)((17-20*(x2)+2*(x3))/20)
#define x2(x1,x3)((18-3*(x1)+(x3))/20)
#define x3(x1,x2)((25-2*(x1)+(x2))/20)
main()
{
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=1;
clrscr();
printf("iteration x1\t\t x2\t\t x3\t \n");
while(i>=0)
{
y1=x1(x2,x3);
y2=x2(x1,x3);
y3=x3(x1,x2);
if((fabs(y1-x1)<esp)&&(fabs(y2-x2)<esp)&&(fabs(y3-x3)<esp))
{
y1=x1(x2,x3);
y2=x2(x1,x3);
y3=x3(x1,x2);
printf("x1=%f\n",y1);
printf("x2=%f\n",y2);
printf("x3=%f\n",y3);
break;
}
else
{
x1=y1;
x2=y2;
x3=y3;
printf("\n %d\t %f\t %f\t %f\t \n",i,x1,x2,x3);
i++;
}
}
getch();
return 0;
}
Expected output:
iteration x1 x2 x3
Actual output:
iteration x1 x2 x3