Sei sulla pagina 1di 45

Assignment No.

:- 1 Algorithm and Programming for LINEAR SEARCH USING C PROGRAMMING

Algorithm : LINEAR(DATA,N,ITEM,LOC) 1. Set DATA [N+1] = ITEM 2. Set LOC = 1 3. while DATA[LOC] != ITEM Set LOC = LOC + 1 4. If LOC = N + 1 Set LOC = 0 5. Exit

Program : #include<stdio.h> #include<conio.h> void main() { int a[10],i,n,m,c=0; printf("Enter the size of an array"); scanf("%d",&n); printf("\nEnter the elements of the array"); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("\nThe elements of an array are"); for(i=0;i<=n-1;i++){ printf(" %d",a[i]); } printf("\nEnter the number to be search"); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("\nThe number is not in the list"); else printf("\nThe number is found"); getch(); }

Output: Enter the size of an array 8 Enter the elements of the array 12345678 Enter the number to be search : 6 Result: The number is found at place 6. Enter the number to be search : 9 Result: The number is not in the list

Assignment No. :- 2 Algorithm and Programming for BINARY SEARCH USING C PROGRAMMING Algorithm: BINARY(DATA,LB,UB,ITEM,LOC)

1. Set BEG = LB,END = UB and MID = INT(BEG+END/2) 2. Repeat Steps 3 and 4,while BEG<=ND and DATA[MID] != ITEM 3. If ITEM < DATA[MID]
Then Set END = MID 1 Else Set BEG = MID + 1

4. Set MID = INT (BEG + END)/2 5. If DATA[MID] = ITEM


Then Set LOC = MID Else Set LOC = NULL

6. Exit
PROGRAM : #include<stdio.h> #include<conio.h> Void main() { int a[10],i,n,m,c=0,l,u,mid; clrscr(); printf("Enter the size of an array->"); scanf("%d",&n); printf("\nEnter the elements of the array->"); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("\nThe elements of an array are->"); for(i=0;i<n;i++){ printf(" %d",a[i]); } printf("\nEnter the number to be search->"); scanf("%d",&m); l=0,u=n-1; while(l<=u) { mid=(l+u)/2;

if(m==a[mid]){ c=1; break; } else if(m<a[mid]) { u=mid-1; } else l=mid+1; } if(c==0) printf("\nThe number is not in the list"); else printf("\nThe number is found"); getch(); }

OUTPUT: Enter the size of an array 7 Enter the elements of the array41 52 63 84 95 46 67 The elements of an array are 41 52 63 84 95 46 67 Enter the number to be search 53 Result : The number is not in the list Enter the number to be search 84 Result : The number is found

Assignment No. :- 3 Algorithm and Programming for BUBBLE SORT USING C PROGRAMMING

ALGORITHM : BUBBLE(A,N) 1. Repeat 2 & 3 for K=1 to N-1 2. Set PTR = 1 3. While PTR <= N-K (a)If A[PTR]>a[PTR+1] Then INTERCHANGE (b)PTR = PTR + 1 EXIT

PROGRAM: #include<stdio.h> #include<conio.h> void main() { int s,temp,i,j,a[20]; clrscr(); printf("\nEnter size of the array: "); scanf("%d",&s); printf("\nEnter %d elements in to the array:",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=0;i<s-1;i++) { for(j=0;j<s-1-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("\nThe array after sorting is: "); for(i=0;i<s;i++); printf(" %d",a[i]); getch();}

OUTPUT: Enter size of the array: 6 Enter elements in to the array: 56 24 63 12 8 69 The array after sorting is:8 12 24 56 63 69

Assignment No. :- 4 Algorithm and Programming for SELECTION SORT USING C PROGRAMMING ALGORITHM : for i = 1:n, k=i for j = i+1:n, if a[j] < a[k], k = j

invariant: a[k] smallest of a[i..n]


swap a[i,k]

invariant: a[1..i] in final position


end

PROGRAM : #include<stdio.h> #include<conio.h> void main() { int s,i,j,temp,a[20]; clrscr(); printf("\nEnter size of the array :"); scanf("%d",&s); printf("\nEnter %d elements in to the array:"); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=0;i<s;i++) { for(j=i+1;j<s;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("\nThe array after sorting is: "); for(i=0;i<s;i++) printf(" %d",a[i]); getch();}

OUTPUT : Enter size of the array : 7 Enter elements in to the array : 14 25 7 65 43 21 89 The array after sorting is: 7 14 21 25 43 65 89

Assignment No. :- 5(a) Algorithm and Programming for MATRIX ADDITION USING C PROGRAMMING ALGORITHM : int A[n][n], B[n][n], C[n][n]; //--> declaring 3 matrices in a common format. int i=j=1; //---> declaring variables.i and j values changes from 1 to 3 in the pgm. for(i=1; i<= 3; i++) { for(j=1; j<= 3; j++) { C[i][j]=A[i][j]+B[i][j]; } }

PROGRAM : #include<stdio.h> #include<conio.h> void main() { int a[2][2],b[2][2],c[2][2],i,j; printf("Enter the First matrix->"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); printf("\nEnter the Second matrix->"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&b[i][j]); printf("\nThe First matrix is\n"); for(i=0;i<2;i++){ printf("\n"); for(j=0;j<2;j++) printf("%d\t",a[i][j]); } printf("\nThe Second matrix is\n"); for(i=0;i<2;i++){

printf("\n"); for(j=0;j<2;j++) printf("%d\t",b[i][j]); } for(i=0;i<2;i++) for(j=0;j<2;j++) c[i][j]=a[i][j]+b[i][j]; printf("\nThe Addition of two matrix is\n"); for(i=0;i<2;i++){ printf("\n"); for(j=0;j<2;j++) printf("%d\t",c[i][j]); } getch(); }

10

OUTPUT : Enter the First matrix 2 3 57 Enter the Second matrix 4 6 54 The Addition of two matrix is 6 9 10 11

11

Assignment No. :- 05(b) Algorithm and Programming for MATRIX SUBTRACTION USING C PROGRAMMING ALGORITHM : int A[n][n], B[n][n], C[n][n]; //--> declaring 3 matrices in a common format. int i=j=1; //---> declaring variables.i and j values changes from 1 to 3 in the program. for(i=1; i<= 3; i++) { for(j=1; j<= 3; j++) { C[i][j]=A[i][j]-B[i][j]; } }

PROGRAM : #include<stdio.h> #include<conio.h> void main() { int a[2][2],b[2][2],c[2][2],i,j; printf("Enter the First matrix->"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); printf("\nEnter the Second matrix->"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&b[i][j]); printf("\nThe First matrix is\n"); for(i=0;i<2;i++){ printf("\n"); for(j=0;j<2;j++) printf("%d\t",a[i][j]); } printf("\nThe Second matrix is\n"); for(i=0;i<2;i++){

12

printf("\n"); for(j=0;j<2;j++) printf("%d\t",b[i][j]); } for(i=0;i<2;i++) for(j=0;j<2;j++) c[i][j]=a[i][j]-b[i][j]; printf("\nThe Addition of two matrix is\n"); for(i=0;i<2;i++){ printf("\n"); for(j=0;j<2;j++) printf("%d\t",c[i][j]); } getch(); }

13

OUTPUT : Enter the First matrix 8 9 57 Enter the Second matrix 4 6 54 The Subtraction of two matrix is 4 3 03

14

Assignment No. :- 05(c) Algorithm and Programming for TRANSPOSE OF A MATRIX USING C PROGRAMMING ALGORITHM : 1. for (int i = 0; i < 4; i++) { 2. for (int j = i + 1; j < 4; j++) { 3. int save = matrix[i][j]; 4. matrix[i][j] = matrix[j][i]; 5. matrix[j][i] = save; 6. } 7. } PROGRAM : #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],i,j,k=0,m,n; printf("\nEnter the row and column of matrix"); scanf("%d %d",&m,&n); printf("\nEnter the First matrix->"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe matrix is\n"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<m;j++) { printf("%d\t",a[i][j]); } } for(i=0;i<m;i++) for(j=0;j<n;j++) b[i][j]=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { b[i][j]=a[j][i]; printf("\n%d",b[i][j]); }

15

} printf("\n\nTraspose of a matrix is -> "); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<m;j++) { printf("%d\t",b[i][j]); } } getch(); }

16

OUTPUT : Enter the row and column of matrix 2 2 Enter the First matrix 5 7 9 8 Traspose of a matrix is 5 9 7 8

17

Assignment No. :- 05(d) Algorithm and Programming for MATRIX MULTIPLICATION USING C PROGRAMMING ALGORITHM : void mm_mul (int A[N][N], int B[N][N], int C[N][N]) { int i, j, k; int sum; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { sum = 0; for (k = 0; k < N; k++) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; } } } PROGRAM : # include<stdio.h> # include<conio.h> void main() { int a[3][3],i,j,k,b[3][3],c[3][3]; printf("Enter matrix A:"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("First Matrix A is :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t",a[i][j]); printf("\n"); } printf(Enter matrix B:) for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); printf("Second Matrix B is:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t",b[i][j]); printf("\n");

18

} printf("Multiplication of the Matrices:\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; printf("%d\t",c[i][j]); } printf("\n"); } } getch(); }

19

OUTPUT: Enter matrix A : 111 111 111 Enter matrix B : 222 222 222 Multiplication of the Matrices : 666 666 666

20

Assignment No. :- 06 Algorithm and Programming for

Stack operations (Inserting ,deleting traversing)


Algorithm :int main() { int j = 10; int l; /* allocating a stack */ stackinit(j); /* fill the stack with 10 items */ for(l=0;l<=10;l++){ push(l); /* we have 10 items in the stack, so quit */ if(l >= 10) { break; exit(1); } } /* pop out all items of the stack */ int k; for(k = 10;k>=0;k--) { pop(k); if(k <= 0) { break; exit(1); } } return 0; } Program:#include<stdio.h> #include<conio.h> #define maxsize 10 void push(); int pop(); void traverse(); int top=-1; int stack[maxsize]; void main() {

21

int choice; char ch; do { clrscr(); printf("1. Push\n2. Pop\n3. Traverse\n Enter your choice...."); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: printf("the deleted element is::: ",pop()); break; case 3: traverse(); break; default:printf("wrong choice entered"); } printf("Do you want to continue(y/n)...."); scanf("%c", &ch); } while((ch=='Y')||(ch=='y')); } void push() { int item; if(top==maxsize-1) printf("overflow"); else { printf("Enter the item to be inserted...."); scanf("%d", &item); top=top+1; stack[top]=item; } } int pop() { int item; if (top==-1) printf("underflow"); else { item=stack[top]; top=top-1; } return item; } void traverse()

22

{ int i; for(i=top;i>=0;i--) printf("\n %d",stack[i]); }

23

Output :1. Push 2. Pop 3. Traverse Enter your choice. 1 Enter the item to be inserted. 5 Do you want to continue(Y/N). N

24

Assignment No. :- 07 Algorithm and Programming for Stack as Linked List Algorithm :PUSH( ) 1. t = newnode( ) 2. Enter info to be inserted 3. Read n 4. tinfo = n 5. tnext = top 6. top = t 7. Return

POP( ) 1. If (top = NULL) Print underflow Return 2. x = top 3. top = top next 4. delnode(x) 5. Return Program:#include<stdio.h> #include<conio.h> struct stack { int no; struct stack *next; } *start=NULL; typedef struct stack st; void push(); int pop(); void display(); void main() { char ch; int choice,item; do

25

{ clrscr(); printf("\n 1: push"); printf("\n 2: pop"); printf("\n 3: display"); printf("\n Enter your choice"); scanf("%d",&choice); switch (choice) { case 1: push(); break; case 2: item=pop(); printf("The delete element in %d",item); break; case 3: display(); break; default : printf("\n Wrong choice"); }; printf("\n do you want to continue(Y/N)"); fflush(stdin); scanf("%c",&ch); } while (ch=='Y'||ch=='y'); } void push() { st *node; node=(st *)malloc(sizeof(st)); printf("\n Enter the number to be insert"); scanf("%d",&node->no); node->next=start; start=node; } int pop() { st *temp; temp=start; if(start==NULL) { printf("stack is already empty"); getch(); exit();

26

} else { start=start->next; free(temp); } return(temp->no); } void display() { st *temp; temp=start; while(temp->next!=NULL) { printf("\nno=%d",temp->no); temp=temp->next; } printf("\nno=%d",temp->no);}

27

Assignment No. :- 08 Algorithm and Programming for Implementation of Queue as Link List Algorithm:CREATE 1. t = new node 2. Enter info to be inserted 3. Read n 4. t info = n 5. t next = front 6. front = t INSERTION 1. r next = t 2. t next = NULL 3. Return DELETION 1. x = front 2. front = front next 3. delnode(x) 4. Return DISPLAY 1. If (front = NULL) Print empty queue Return Else P = start Repeat until (p< > NULL) Print p info P = p next Return Program:#include<stdio.h> #include<conio.h> struct queue {int no; struct queue *next;} *start=NULL;

28

void add(); int del(); void traverse(); void main() { int ch; char choice; do { clrscr(); printf("----1. add\n"); printf("----2. delete\n"); printf("----3. traverse\n"); printf("----4. exit\n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: add(); break; case 2: printf("the deleted element is\n%d",del()); break; case 3: traverse(); break; case 4: return; default : printf("wrong choice\n"); }; fflush(stdin); scanf("%c",&choice); } while(choice!=4); } void add() { struct queue *p,*temp; temp=start; p=(struct queue*)malloc(sizeof(struct queue)); printf("Enter the data"); scanf("%d",&p->no); p->next=NULL; if(start==NULL) { start=p; } else { while(temp->next!=NULL) { temp=temp->next; } temp->next=p; } } int del() { struct queue *temp;

29

int value; if(start==NULL) { printf("queue is empty"); getch(); return(0); } else { temp=start; value=temp->no; start=start->next; free(temp); } return(value); } void traverse() { struct queue *temp; temp=start; while(temp->next!=NULL) { printf("no=%d",temp->no); temp=temp->next;} printf("no=%d",temp->no); getch(); }

30

Output :1. 2. 3. 4. Add Delete Traverse Exit

Enter your choice. 2 Enter the item to be deleted. 5 The item is deleted i.e. . 5 Do you want to continue(Y/N). N

31

Assignment No. :- 09 Algorithm and Programming for Finding Factorial of any number Algorithm:1. Take a number as a input from the user. 2. initialize a variable fact=1 3. fact=fact*number 4. number=number-1 5. repeat step 3 to 5 if number>0 6. print the value of fact variable

Program:#include<stdio.h> #include<conio.h> void main() { int c, n, fact = 1; printf("Enter a number to calculate it's factorial\n"); scanf("%d",&n); for( c = 1 ; c <= n ; c++ ) fact = fact*c;

printf("Factorial of %d = %d\n",n,fact); getch() return 0; }

32

Output :Enter a number to calculate it's factorial 5 Factorial of 5 = 120

33

Assignment No. :- 10 Algorithm and Programming for Finding Fibonacci Series in C Algorithm:Fib(n) = fib(n-1) + fib(n-2) Stop conditions: n = 2 || n = 1

Program:#include <iostream.h> int fib(int n); int main() { int n, answer; printf("Enter number upto where fibonacci is to find: "); scanf(%d,&n); printf("\n\n"); answer = fib(n); Printf(Answer is the " , n ); return 0; } int fib (int n) { cout << "Processing fib(" << n << ")... "; if (n < 3 ) { cout << "Return 1!\n"; return (1); } else { cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n"; return( fib(n-2) + fib(n-1)); }}

34

Output :Enter number upto where fibonacci is to find: 3 Answer is the 0 1 2

35

Assignment No. :- 11 Algorithm and Programming for Swapping two nos. by Call by Value Method Algorithm :swap(x1,y1) { int z1; z1=x1; x1=y1; y1=z1; printf(x=%d y=%d,x1,y1); getch(); }

Program :#include<stdio.h> #include<conio.h> void main() { int x,y,swap(intx,inty); printf("enter value of x & y:"); scanf("%d%d",&x,&y); swap(&x,&y); printf(x=%d y=%d,x,y); } swap(x1,y1) { int z1; z1=x1; x1=y1; y1=z1; printf(x=%d y=%d,x1,y1); getch(); }

36

Output :Enter value of x and y: 10 20 Values after swapping are x1=20 y1=10

37

Assignment No. :- 12 Algorithm and Programming for Swapping two nos. by Call by Reference Method Algorithm :swap(x1,y1) { int z1; z1=*x1; *x1=*y1; *y1=z1; printf(Values after swapping are\n*x=%d *y=%d,x1,y1); getch(); } Program :#include<stdio.h> #include<conio.h> void main() { int x,y,swap(intx,inty); printf("Enter value of x & y:"); scanf("%d%d",&x,&y); swap(&x,&y); printf(x=%d y=%d,x,y); } swap(x1,y1) { int z1; z1=*x1; *x1=*y1; *y1=z1; printf(Values after swapping are\n*x=%d *y=%d,x1,y1); getch(); }

38

Output :Enter value of x and y: 10 20 Values after swapping are x1=20 y1=10

39

Assignment No. :- 13 Algorithm and Programming for Algorithm and Programming for Quick Sort Algorithm :1. 2. 3. 4. 5. 6. 7. low =l, high = h, key a[(l+h)/2] Repeat through step 7 while (low <= high) Repeat step 4 while (a[low] < key) low = low +1 Repeat step 6 while (a[high] > key) high = high 1 If (low <= high) a) temp = a[low] b) a[low] = a[high] c) a[high] = temp d) low = low + 1 e) high = high + 1 8. If (l < high) quicksort (a,l,high) 9. If (h>low) quicksort (a,low,h) 10. Exit Program :#include<stdio.h> #include<conio.h> #define max 100 int a[max],n,i,l,h; void main() { void input(void); input(); getch(); } void input(void) { void output(int a[],int n); void quick_sort(int a[],int l,int h); printf("How many elements in the array : "); scanf("%d",&n); printf("\n"); printf("Enter the elemennts : \n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } l=0; h=n-1;

40

quick_sort(a,l,h); printf("Sorted Array :\n "); output(a,n); } void quick_sort(int a[],int l, int h) { int temp,key,low,high; low=l; high=h; key=a[(low+high)/2]; do { while(key>a[low]) { low++; } while(key<a[high]) { high--; } if(low<=high) { temp=a[low]; a[low++]=a[high]; a[high--]=temp; } } while(low<=high); if(l<high) quick_sort(a,l,high); if(low<h) quick_sort(a,low,h); } void output(int a[],int n) { for(i=0;i<=n-1;i++) { printf("%d\n",a[i]); } }

41

Output :How many elements in the array: 5 Enter the elements : 55 28 82 298 477 Sorted Array: 28 55 82 298 477

42

Assignment No. :- 14 Algorithm and Programming for INSERTION SORT.

#include<stdio.h> #include<conio.h> void main() { int a[100],n,k,i,j,temp; clrscr(); printf("how many elements: "); scanf("%d",&n); printf("enter the element of array\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } for(k=1;k<=n-1;k++) { temp=a[k]; j=k-1; while(temp<a[j]&&(j>=0)) {a[j+1]=a[j]; j=j-1; } a[j+1]=temp;

43

} printf("element of array after sorting are : \n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); }

44

OUTPUT: How many elements: 6 Enter the elements of array: 6 4 9 2 8 1 Elements of array after sorting are: 1 2 4 6 8 9

45

Potrebbero piacerti anche