0% found this document useful (0 votes)
81 views28 pages

MC-1208 CPNM Lab Syllabus

The document provides a syllabus for a CPNM LAB course that includes 14 programming problems covering topics like calculating the area of a triangle from point coordinates, generating and printing random integers in arrays using different loops, writing string manipulation functions, determining the maximum and minimum values of different data types, generating and sorting random real numbers, and implementing numerical methods like matrix operations and interpolation. Students would need to write code to solve each problem and test their programs.

Uploaded by

Sharmila Jajula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views28 pages

MC-1208 CPNM Lab Syllabus

The document provides a syllabus for a CPNM LAB course that includes 14 programming problems covering topics like calculating the area of a triangle from point coordinates, generating and printing random integers in arrays using different loops, writing string manipulation functions, determining the maximum and minimum values of different data types, generating and sorting random real numbers, and implementing numerical methods like matrix operations and interpolation. Students would need to write code to solve each problem and test their programs.

Uploaded by

Sharmila Jajula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

MC-1208

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();

printf("To find the area of triangle\n");

printf("enter the coordinates of point a\n");

scanf("%d %d",&x1,&y1);

printf("enter the coordinates of point b\n");

scanf("%d %d",&x2,&y2);

printf("enter the coordinates of point c\n");

scanf("%d %d",&x3,&y3);

printf("the coordinates of a,b,c area(%d,%d),b(%d,%d),c(%d,%d)\n",x1,y1,x2,y2,x3,y3);

area=(0.5)*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));

if(area==0)

printf("the point are in straight line and hence area is 0 \n");

else if(area<0)

printf("area of triangle = %f",area*(-1));

else

printf("\n area of triangle = %f", area);


}

getch();

Expected Output:

To find the area of triangle

enter the coordinates of point a

73

enter the coordinates of point b

83

enter the coordinates of point c

95

the coordinates of a,b,c area(7,3),b(8,3),c(9,5)

area of triangle = 1.000000


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).

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<time.h>

void main()

int i=1,choice,k[100];

clrscr();

printf("enter choice for \n 1.for\n 2.while\n 3.do while\n");

printf("enter any choice:");

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:

printf("wrong choice \n");

break;

getch();

Expected Output:

enter choice for

1.for

2.while

3.do while

enter any choice:1

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

enter choice for

1.for

2.while

3.do while

enter any choice:2

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

enter choice for

1.for

2.while

3.do while

enter any choice:3

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();

printf("enter string: \n");

scanf("%s %s",&a,&c);

printf("enter the upper and lower limits of the substring \n");

scanf("%d %d",&l1,&l2);

for(i=0;a[i]!='\0';i++)

l++;

printf("length of string %d\n",l);

for(j=0;a[j]!='\0';j++)

b[j]=a[j];

b[j]='\0';

printf("the copied string is %s \n",b);

for(j=l-1;j>=0;j--)

d[k]=a[j];

k++;

d[k]='\0';

printf("the reversed string is %s \n",d);


for(i=0;a[i]!='\0';i++)

a[l+i]=c[i];

printf("the concatenated string is %s \n",a);

if(l1<=l&&l2<=l)

for(i=l1-1,j=0;i<l2;i++)

e[j]=a[i];

j++;

printf("substring from %d to %d is %s \n",l1,l2,e);

getch();

Expected Output:

enter string:

cpnmlab classes

enter the upper and lower limits of the substring

17

length of string 7

the copied string is cpnmlab

the reversed string is balmnpc

the concatenated string is cpnmlabclasses

substring from 1 to 7 is cpnmlab


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?

#include<stdio.h>

#include<limits.h>

#include<values.h>

#include<math.h>

#include<float.h>

#include<conio.h>

void main()

clrscr();

printf("\n the minimum range of short integers is %d", SHRT_MIN);

printf("\n the maximum range of short integers is %d", SHRT_MAX);

printf("\n the minimum range of integers is %d", INT_MIN);

printf("\n the maximum range of integers is %d", INT_MAX);

printf("\n the minimum range of character is %d", CHAR_MIN);

printf("\n the maximum range of character is %d", CHAR_MAX);

printf("\n the minimum range of float is %e", FLT_MIN);

printf("\n the maximum range of float is %e", FLT_MAX);

printf("\n the minimum range of double is %e", DBL_MIN);

printf("\n the maximum range of double is %e", DBL_MAX);

printf("\n the minimum range of long integers is %lld", LONG_MIN);

printf("\n the maximum range of long integers is %lld", LONG_MAX);

printf("\n when 1 is added to maximum range of short int is %d",SHRT_MAX+1);

getch();

Expected output:

the minimum range of short integers is -32768

the maximum range of short integers is 32767


the minimum range of integers is -32768

the maximum range of integers is 32767

the minimum range of character is -128

the maximum range of character is 127

the minimum range of float is 1.175494e-38

the maximum range of float is 3.402823e+38

the minimum range of double is 2.225074e-308

the maximum range of double is 1.797693e+308

the minimum range of long integers is -2147483648

the maximum range of long integers is 2147483647

when 1 is added to maximum range of short int is -32768


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.

#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:

the generated 100 random real numbers :


10.10 10.03 13.35 10.33 13.55 12.17 15.36 11.95 17.00 19.49
12.74 14.44 11.08 16.98 15.64 10.41 11.65 18.15 16.85 17.64
18.27 19.59 12.19 14.26 19.52 18.39 19.23 18.10 14.51 16.04
16.61 15.99 15.49 17.20 11.13 14.06 11.21 16.71 14.74 14.91
15.64 13.44 18.68 12.64 11.79 14.23 16.94 11.63 15.38 16.45
16.23 10.03 17.87 12.68 14.61 13.86 13.76 15.81 16.03 12.79
11.70 18.05 12.94 13.33 14.08 12.40 14.13 10.54 14.94 19.83
10.01 14.09 10.69 10.73 12.54 19.74 13.55 14.04 11.97 11.97
12.11 12.49 17.58 18.89 19.05 17.35 14.61 15.31 10.35 11.30
14.58 14.83 15.96 12.53 11.14 17.01 16.49 18.86 18.75 10.42
the generated 100 random real numbers in descending order:
19.83 19.74 19.59 19.52 19.49 19.23 19.05 18.89 18.86 18.75
18.68 18.39 18.27 18.15 18.10 18.05 17.87 17.64 17.58 17.35
17.20 17.01 17.00 16.98 16.94 16.85 16.71 16.61 16.49 16.45
16.23 16.04 16.03 15.99 15.96 15.81 15.64 15.64 15.49 15.38
15.36 15.31 14.94 14.91 14.83 14.74 14.61 14.61 14.58 14.51
14.44 14.26 14.23 14.13 14.09 14.08 14.06 14.04 13.86 13.76
13.55 13.55 13.44 13.35 13.33 12.94 12.79 12.74 12.68 12.64
12.54 12.53 12.49 12.40 12.19 12.17 12.11 11.97 11.97 11.95
11.79 11.70 11.65 11.63 11.30 11.21 11.14 11.13 11.08 10.73
10.69 10.54 10.42 10.41 10.35 10.33 10.10 10.03 10.03 10.01
6.)Write a function for transposing a square matrix in place (in place means that you are not
allowed to have full temporary matrix).

#include<stdio.h>

#include<conio.h>

void main()

int i,j,r,c,a[10][10];

clrscr();

printf("\n Enter number of rows:");

scanf("%d",&r);

printf("\n Enter number of columns:");

scanf("%d",&c);

printf("\n Enter %d * %d matrix elements",r,c);

for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf("%d",&a[i][j]);

printf("\n Elements are:\n");

for(i=0;i<r;i++)

for(j=0;j<c;j++)

printf("%d",a[i][j]);

printf("\t");

printf("\n");

printf("transpose matrix is : \n");


for(i=0;i<c;i++)

for(j=0;j<r;j++)

printf("%d\t",a[j][i]);

printf("\n");

getch();

Expected Output:

Enter number of rows:3

Enter number of columns:3

Enter 3 * 3 matrix elements1 3 6

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)

puts("\n can't open the file ");

exit(0);

printf("\n Enter some numbers .To stop use ctrl + z: \n");

while(scanf("%d",&n)!=EOF)

fprintf(fp,"%d\t",n);

fclose(fp);

fp=fopen("mean.dat","r");

printf("\n The numbers stored in file : \n");

while(fscanf(fp,"%d",&n)!=EOF)

count++;

sum=sum+n;

printf("%d\t",n);

sumsq+=n*n;
}

fclose(fp);

mean=sum/count;

printf("\n Mean = %0.2f",mean);

sd= sqrt((sumsq/count)-(mean*mean));

printf("\n Standard deviation = %0.2f\n",sd);

getch();

return 0;

Expected output:

Enter some numbers .To stop use ctrl + z:

1234567

The numbers stored in file :

1 2 3 4 5 6 7

Mean = 4.00

Standard deviation = 2.00


8.)Given two points on the surface of the sphere, write a program to determine the smallest arc
length between them.

#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:

Enter x,y,z,co-ordinates of the centre of the sphere

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();

printf("\n Enter the number of which square root has to be found:\n");

scanf("%f",&n);

printf("\n Enter the initial guess values and accuracy: \n");

scanf("%f,%f,%f",&a,&b,&epsilon);

if(f(a)*f(b)>0)

printf("\n Unsuitable initial values\n");

exit(0);

xmid=(a+b)/2;

while(fabs(xmid-b)/xmid >= epsilon)

if(f(xmid)==0)

break;

if(f(a)*f(xmid)<0)

b=xmid;

else
a=xmid;

xmid=(a+b)/2;

printf("\n The square root = %2f \n",xmid);

getch();

return 0;

Expected output:

Enter the number of which square root has to be found:

25

Enter the initial guess values and accuracy:

4,6,0.0001

The square root = 5.000000


10.)Implement Newton Raphson method to det. a root of polynomial equation.

#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)

1 3.000000 0.750000 36.000000 16.000000

2 0.750000 -0.696429 10.125000 7.000000

3 -0.696429 -4.142333 4.184311 1.214286

4 -4.142333 -2.252931 23.748508 -12.569330

5 -2.252931 -0.828337 7.139674 -5.011725

6 -0.828337 -6.739546 4.058936 0.686651

7 -6.739546 -3.695544 69.884781 -22.958185

8 -3.695544 -1.976789 18.531910 -10.782174

9 -1.976789 -0.464632 5.908234 -3.907156


appropriate value of root is -0.464632
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

#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:

Enter number of values:2


Enter values of x and f(x) :
3
3
2
1
Enter intermediate x value:9
f(9.000000)=15.000000
12.) Write a function which will invert a matrix.

#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:

enter order of the matrix:


2
enter the elements of matrix:
1234
the inverse matrix is
-2.000000 1.000000
1.500000 -0.500000
13.)Implement Simpson’s rule for numerical integration.

#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:

Enter a and b limits


24
Enter no of steps:
2
approximate value is 0.511111
14.)Write a program to solve a set of linear algebraic equations.

#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

1 0.850000 0.900000 1.250000

2 0.075000 0.835000 1.210000

3 0.136000 0.949250 1.284250

4 0.029175 0.943812 1.283862

5 0.034574 0.959817 1.294273

6 0.019610 0.959528 1.294533

7 0.019926 0.961785 1.296015

8 0.017816 0.961812 1.296097

9 0.017798 0.962132 1.296309

10 0.017499 0.962146 1.296327


x1=0.017487
x2=0.962192
x3=1.296357

Actual output:
iteration x1 x2 x3

1 0.850000 0.900000 1.250000

2 0.075000 0.835000 1.210000

3 0.136000 0.949250 1.284250

4 0.029175 0.943812 1.283862

5 0.034574 0.959817 1.294273

6 0.019610 0.959528 1.294533

7 0.019926 0.961785 1.296015

8 0.017816 0.961812 1.296097

9 0.017798 0.962132 1.296309

10 0.017499 0.962146 1.296327


x1=0.017487
x2=0.962192
x3=1.296357

You might also like