0% found this document useful (0 votes)
68 views

Data Structure Practical File: D.S Lab

The document contains a data structure practical file submitted by Prabjot Singh, a student of CSE with roll number CO10365. It includes 10 programs related to arrays and data structures, such as adding numbers from an array, finding the factorial of a number, addition and multiplication of matrices, finding the smallest and greatest elements of an array, calculating percentage marks using an array, traversing, inserting and deleting elements from an array, and implementing bubble sorting. For each program, the code and output are listed.

Uploaded by

Rohit Arora
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Data Structure Practical File: D.S Lab

The document contains a data structure practical file submitted by Prabjot Singh, a student of CSE with roll number CO10365. It includes 10 programs related to arrays and data structures, such as adding numbers from an array, finding the factorial of a number, addition and multiplication of matrices, finding the smallest and greatest elements of an array, calculating percentage marks using an array, traversing, inserting and deleting elements from an array, and implementing bubble sorting. For each program, the code and output are listed.

Uploaded by

Rohit Arora
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

D.

S LAB

Data Structure Practical File


Submitted To: Ms. Jaspreet Kaur Submitted By: Prabjot singh CSE, 3rd Semester CO10365

CO10365

D.S LAB

Index
Sr No. Program List Remarks

CO10365

D.S LAB

1. Write a program to add two numbers from an array.


#include<stdio.h> #include<conio.h> void main() { clrscr(); inti,x,y,b,a[10]; printf("Enter the array : "); for(i=0;i<=9;i++) { scanf("%d",&a[i]); } printf("Choose any two numbers to add : "); scanf("%d%d",&x,&y); b=a[x]+a[y]; printf("Answer is : %d",b); getch(); }

Output of the program:


Enter the array : 1 2 3 4 5 6 7 8 9 10 Choose any two numbers to add : 3 9 Answer is : 14

CO10365

D.S LAB

2. Write a program to find factorial of a number.


#include<stdio.h> #include<conio.h> Int a,i,b=1; void fact(int); void main() { clrscr(); printf("Enter the no to find factorial : "); scanf("%d",&a); fact(a); getch(); } void fact(int a) { for(i=1;i<=a;i++) { b=b*i; } printf("Answer is : %d",b); }

Output of the program:


Enter the no to find factorial : 4 Answer is : 24

CO10365

D.S LAB

3. Write a program for addition of two matrices.


#include<stdio.h> #include<conio.h> int a[3][3],b[3][3],c[3][3]; int i,j; void main() { clrscr(); printf("Enter the elements of first matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("Enter the elements of second matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; } } printf("THE SUM OF THE TWO MATRICES IS : \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",c[i][j]); printf("\t"); } printf("\n"); } getch();

CO10365

D.S LAB }

Output of the program:


Enter the elements of first matrix : 1 2 3 4 5 6 7 8 9 Enter the elements of second matrix : 9 8 7 6 5 4 3 2 1 THE SUM OF THE TWO MATRICES IS : 10 10 10 10 10 10 10 10 10

CO10365

D.S LAB

4. Write a program to find smallest and greatest element of an array.


#include<stdio.h> #include<conio.h> int a[5],i,x,y; void main() { clrscr(); printf("Enter the elements of array\n"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } x=a[0]; y=a[0]; for(i=0;i<5;i++) { if(a[i]>x) { x=a[i]; } if(a[i]<y) { y=a[i]; } } printf("The greatest no. is : %d\nThe smallest no. is : %d",x,y); getch(); }

Output of the program:


Enter the elements of array 10 20 50 80 30 The greatest no. is : 80 The smallest no.is : 10

CO10365

D.S LAB

5. Write a program to find percentage marks of student using array.


#include<stdio.h> #include<conio.h> Int i,m[5],sum=0; float per; void main() { clrscr(); printf("Enter the marks of the student\n"); for(i=0;i<5;i++) { scanf("%d",&m[i]); sum=sum+m[i]; } per=(float)sum/5; printf("Percentage marks are : %f",per); getch(); }

Output of the program:


Enter the marks of the student 70 84 90 75 87 Percentage marks are : 81.211113

CO10365

D.S LAB

6. Write a program for multiplication of two matrices.


#include<stdio.h> #include<conio.h> int m1[3][3],m2[3][3],m3[3][3]; inti,j,k; void main() { clrscr(); printf("Enter the elements of matrix A\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&m1[i][j]); } } printf("Enter the elements of matrix B\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&m2[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) { m3[i][j]+=m1[i][k]*m2[k][j]; } } } printf("The product of both the matrices is\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d\t",m3[i][j]); }

CO10365

D.S LAB printf("\n"); } getch(); }

Output of the program:


Enter the elements of first matrix 1 2 3 4 5 6 7 8 9 Enter the elements of second matrix 9 8 7 6 5 4 3 2 1 The product of both the matrices is 30 24 18 84 69 54 138 114 90

CO10365

D.S LAB

7. Write a program for traversing a linear array.


#include<stdio.h> #include<conio.h> int a[5],k=0; void main() { clrscr(); printf("Enter the elements to be traversed:\n"); while(k<=4) { scanf("%d",&a[k]); k++; } printf("\n"); k=0; while(k<=4) { printf("%d\n",a[k]); k++; } getch(); }

Output of the program:


Enter the elements to be traversed: 10 30 80 20 50 10 20 30 50 80

CO10365

D.S LAB

8. Write a program to insert in an array.


#include<stdio.h> #include<conio.h> inti,a[30],k,n; void main() { clrscr(); printf("Enter the no. of elements of the array : "); scanf("%d",&n); printf("Enter the elements of array\n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } printf("Enter the position of the element for insertion : "); scanf("%d",&k); n++; for(i=n;i>=k;i--) { a[i+1]=a[i]; } printf("Enter the item : "); scanf("%d",&a[k]); printf("The new array after insertion is :\n"); for(i=1;i<=n;i++) { printf("%d\n",a[i]); } getch(); }

CO10365

D.S LAB

Output of the program:


Enter the no. of elements of the array : 5 Enter the elements of array 11 22 44 55 66 Enter the position of the element for insertion : 3 Enter the item : 33 The new array after insertion is : 11 22 33 44 55 66

CO10365

D.S LAB

9. Write a program to show deletion from an array.


#include<stdio.h> #include<conio.h> Int i,a[30],k,n; void main() { clrscr(); printf("Enter the no. of elements of the array : "); scanf("%d",&n); printf("Enter the elements of array\n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } printf("Enter the position of the element to be deleted: "); scanf("%d",&k); for(i=k;i<=n;i++) { a[i]=a[i+1]; } n--; printf("The new array after deletion is :\n"); for(i=1;i<=n;i++) { printf("%d\n",a[i]); } getch(); }

Output of the program:


Enter the no. of elements of the array : 5 Enter the elements of array 10 20 90 30 40 Enter the position of the element to be deleted : 3 The new array after deletion is : 10 20 30

CO10365

D.S LAB 40

10. Write a program to implement bubble sorting.


#include<stdio.h> #include<conio.h> Int i,a[30],j,n,b; void main() { clrscr(); printf("Enter the no. of elements of the array : "); scanf("%d",&n); printf("Enter the elements of array\n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=1;i<=n-1;i++) { for(j=1;j<=n-i;j++) { if(a[j]>a[j+1]) { b=a[j+1]; a[j+1]=a[j]; a[j]=b; } } } printf("The sorted array is : \n"); for(i=1;i<=n;i++) { printf("%d\n",a[i]); } getch(); }

CO10365

D.S LAB

Output of the program:


Enter the no. of elements of the array : 5 Enter the elements of array 99 67 87 50 28 The sorted array is : 28 50 67 87 99

CO10365

You might also like