0% found this document useful (0 votes)
14 views7 pages

CS Lab Assignment

The document contains C programs for various mathematical and algorithmic tasks, including calculating the determinant of a matrix, checking collinearity of points, finding the area of a triangle, computing matrix powers, performing selection sort, and executing linear search. Each program includes sample input and output to demonstrate functionality. The code snippets are structured to guide users through the implementation of these algorithms in C.

Uploaded by

kshitij0720
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)
14 views7 pages

CS Lab Assignment

The document contains C programs for various mathematical and algorithmic tasks, including calculating the determinant of a matrix, checking collinearity of points, finding the area of a triangle, computing matrix powers, performing selection sort, and executing linear search. Each program includes sample input and output to demonstrate functionality. The code snippets are structured to guide users through the implementation of these algorithms in C.

Uploaded by

kshitij0720
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/ 7

CS ASSIGNMENT - C PROGRAMS

1. Determinant of a Matrix

#include <stdio.h>
int main() {
int a[3][3], i, j;
int det;
printf("Enter 3x3 matrix elements:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

det = a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1])
- a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])
+ a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);

printf("Determinant = %d", det);


return 0;
}

Sample Output:
Enter 3x3 matrix elements:
123
456
789
Determinant = 0
2. Check Collinearity using Determinant

#include <stdio.h>
int main() {
float x1,y1,x2,y2,x3,y3,det;
printf("Enter coordinates x1 y1 x2 y2 x3 y3:\n");
scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);

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

if(det == 0)
printf("Points are collinear");
else
printf("Points are not collinear");
return 0;
}

Sample Output:
Enter coordinates x1 y1 x2 y2 x3 y3:
112233
Points are collinear

3. Area of Triangle using Determinant

#include <stdio.h>
#include <math.h>
int main() {
float x1,y1,x2,y2,x3,y3,area;
printf("Enter coordinates x1 y1 x2 y2 x3 y3:\n");
scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);

area = fabs(x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2))/2.0;

printf("Area of triangle = %.2f", area);


return 0;
}

Sample Output:
Enter coordinates x1 y1 x2 y2 x3 y3:
004003
Area of triangle = 6.00

4. Matrix Power (M^n)

#include <stdio.h>
int main() {
int M[10][10], result[10][10], temp[10][10];
int n, m, i, j, k, p;
printf("Enter order of matrix: ");
scanf("%d", &m);
printf("Enter matrix elements:\n");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&M[i][j]);
printf("Enter power n: ");
scanf("%d",&n);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
result[i][j] = (i==j)?1:0;

for(p=0;p<n;p++){
for(i=0;i<m;i++)
for(j=0;j<m;j++){
temp[i][j]=0;
for(k=0;k<m;k++)
temp[i][j]+=result[i][k]*M[k][j];
}
for(i=0;i<m;i++)
for(j=0;j<m;j++)
result[i][j]=temp[i][j];
}

printf("M^%d is:\n", n);


for(i=0;i<m;i++){
for(j=0;j<m;j++)
printf("%d ", result[i][j]);
printf("\n");
}
return 0;
}

Sample Output:
Enter order of matrix: 2
Enter matrix elements:
12
34
Enter power n: 2
M^2 is:
7 10
15 22

5. Selection Sort (Ascending & Descending)

#include <stdio.h>
int main() {
int a[100], n, i, j, min, temp;
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=0;i<n;i++) scanf("%d",&a[i]);

for(i=0;i<n-1;i++){
min=i;
for(j=i+1;j<n;j++)
if(a[j]<a[min]) min=j;
temp=a[i]; a[i]=a[min]; a[min]=temp;
}
printf("Ascending order: ");
for(i=0;i<n;i++) printf("%d ",a[i]);

for(i=0;i<n-1;i++){
min=i;
for(j=i+1;j<n;j++)
if(a[j]>a[min]) min=j;
temp=a[i]; a[i]=a[min]; a[min]=temp;
}
printf("\nDescending order: ");
for(i=0;i<n;i++) printf("%d ",a[i]);
return 0;
}

Sample Output:
Enter number of elements: 5
Enter elements:
42513
Ascending order: 1 2 3 4 5
Descending order: 5 4 3 2 1

6. Linear Search

#include <stdio.h>
int main() {
int a[100], n, key, i, found=0;
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter elements:\n");
for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("Enter element to search: ");
scanf("%d",&key);
for(i=0;i<n;i++){
if(a[i]==key){
printf("Element found at position %d", i+1);
found=1; break;
}
}
if(!found) printf("Element not found");
return 0;
}

Sample Output:
Enter number of elements: 5
Enter elements:
10 20 30 40 50
Enter element to search: 30
Element found at position 3

You might also like