Ada Final Submission (ada Programs)

  • Uploaded by: TB
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ada Final Submission (ada Programs) as PDF for free.

More details

  • Words: 1,417
  • Pages: 28
Name: Tanmay Baid Roll Number: 0829cs071112

Bubble Sort # include<stdio.h> # include void main() { int a[5], i, j, temp; clrscr(); printf("Enter 5 elements of array: "); for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { for(j=0;j<4;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("Sorted array: "); for(i=0;i<5;i++) { printf("\n%d",a[i]); } getch(); }

1

Name: Tanmay Baid Roll Number: 0829cs071112

Output Enter 5 elements of array: 1 8 5 2 7 Sorted array: 1 2 5 7 8

2

Name: Tanmay Baid Roll Number: 0829cs071112

Merge Sort #include<stdio.h> #include void mergesort(int x[],int n) { int aux[10],i,j,k,l1,u1,l2,u2,size; size=1; while(size
3

Name: Tanmay Baid Roll Number: 0829cs071112 for(i=l1;k
4

Name: Tanmay Baid Roll Number: 0829cs071112

Output How many elements you want to enter in array:-5 Enter the elements of array:5 3 4 2 1 The sorted array is:1 2 3 4 5

5

Name: Tanmay Baid Roll Number: 0829cs071112

Recursive Binary Search #include<stdio.h> #include void main() { int Binsrch(int [],int,int,int); int a[100],i,n,item,res; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } printf("Enter the element to be searched:"); scanf("%d",&item); res=Binsrch(a,0,n-1,item); if(res==0) printf("The element is not present in the array"); else printf("The element is found at position=%d",res+1); getch(); } int Binsrch(int a[],int i,int l, int x) { int mid; if(l==i) { if(x==a[i])

6

Name: Tanmay Baid Roll Number: 0829cs071112 return i; else return 0; } else { mid=(i+l)/2; if(x==a[mid]) return mid; else if(x
7

Name: Tanmay Baid Roll Number: 0829cs071112

Iterative Binary Search #include<stdio.h rel="nofollow"> #include void main() { int Binsrch(int [],int,int); int a[100],i,n,item,res; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } printf("Enter the element to be searched:"); scanf("%d",&item); res=Binsrch(a,n-1,item); if(res==0) printf("The element is not present in the array");

8

Name: Tanmay Baid Roll Number: 0829cs071112 else printf("The element is found at position=%d",res+1); getch(); } int Binsrch(int a[],int n,int x) { int mid; int low=0,high=n; while(low<=high) { mid=(low+high)/2; if(xa[mid]) low=mid+1; else return mid; } return 0; } Output How many elements:5 Enter the element of the array: 11 21 31 41 51 Enter the element to be searched: 31 The element is found at position=3

9

Name: Tanmay Baid Roll Number: 0829cs071112

Heap Sort #include<stdio.h> #include int a[100],i,j; void Heapify(int [],int); void HeapSort(int [],int); void Adjust(int [],int,int); void main() { int n; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) {

10

Name: Tanmay Baid Roll Number: 0829cs071112 scanf("%d",&a[i]); } HeapSort(a,n); printf("THE SORTED ARRAY IS:\n"); for(i=0;i<=n-1;i++) { printf("%d\t",a[i]); } getch(); } void HeapSort(int a[],int n) { int t; Heapify(a,n); for(i=n-1;i>=1;i--) { t=a[i]; a[i]=a[0]; a[0]=t; Adjust(a,0,i-1); } }

void Heapify(int a[],int n) { for(i=(n-1)/2;i>=0;i--) Adjust(a,i,n); } void Adjust(int a[],int i,int n) { int item; j=2*i; item=a[i]; while(j<=n-1) { if((j<=n-1)&&(a[j]=a[j]) break; a[j/2]=a[j];

11

Name: Tanmay Baid Roll Number: 0829cs071112 j=2*j; } a[j/2]=item; }

Output How many elements:6 Enter the element of the array: 85 56 69 21 10 25 THE SORTED ARRAY IS: 10 21 25 56 69 85

12

Name: Tanmay Baid Roll Number: 0829cs071112

Quick Sort #include<stdio.h> #include void quicksort(int a[],int l,int h) { int temp,low,high,key; low=l; high=h; key=a[(low+high)/2];

13

Name: Tanmay Baid Roll Number: 0829cs071112 while(low<=high) { while(a[low]key) high--; if(low<=high) { temp=a[low]; a[low]=a[high]; a[high]=temp; low++; high--; } } if(l
14

Name: Tanmay Baid Roll Number: 0829cs071112

Output How many elements you want to enter in array:-5 Enter the elements of array:5 3 4 2 1

15

Name: Tanmay Baid Roll Number: 0829cs071112 The sorted array is:1 2 3 4 5

Max-Min #include<stdio.h> #include int max,min; void main()

16

Name: Tanmay Baid Roll Number: 0829cs071112 { void StraightMaxMin(int [],int,int,int); int a[100],i,n; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the element of the array:\n"); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } max=min=a[0]; StraightMaxMin(a,n,max,min); getch(); } void StraightMaxMin(int a[],int n,int max,int min) { int i; for(i=1;i<=n-1;i++) { if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; } printf("THE MAXIMUM ELEMENT IN THE ARRAY IS:%d",max); printf("\nTHE MINIMUM ELEMENT IN THE ARRAY IS:%d",min); }

Output How many elements:7 Enter the elements of the array: 54

17

Name: Tanmay Baid Roll Number: 0829cs071112 25 38 95 16 24 31 THE MAXIMUM ELEMENT IN THE ARRAY: 95 THE MINIMUM ELEMENT IN THE ARRAY: 16

Knapsack

18

Name: Tanmay Baid Roll Number: 0829cs071112 #include <stdio.h> #define MAXWEIGHT 100 int n = 3; /* The number of objects */ int c[10] = {8, 6, 4}; /* c[i] is the *COST* of the ith object*/ int v[10] = {16, 10, 7}; /* v[i] is the *VALUE* of the ith object*/ int W = 10; /* The maximum weight you can take */ void fill_sack() { int a[MAXWEIGHT]; /* a[i] holds the maximum value that can be obtained using at most i weight */ int last_added[MAXWEIGHT]; /* I use this to calculate which object were added */ int i, j; int aux; for (i = 0; i <= W; ++i) { a[i] = 0; last_added[i] = -1; } a[0] = 0; for (i = 1; i <= W; ++i) for (j = 0; j < n; ++j) if ((c[j] <= i) && (a[i] < a[i - c[j]] + v[j])) { a[i] = a[i - c[j]] + v[j]; last_added[i] = j; } for (i = 0; i <= W; ++i) if (last_added[i] != -1) printf("\nWeight %d; Benefit: %d; \nTo reach this weight I added object %d (%d$ %dKg) to weight %d.\n", i, a[i], last_added[i] + 1, v[last_added[i]], c[last_added[i]], i c[last_added[i]]); else printf("Weight %d; Benefit: 0; Can't reach this exact weight.\n", i); printf("---\n"); aux = W; while ((aux > 0) && (last_added[aux] != -1)) { printf("Added object %d (%d$ %dKg). Space left: %d\n", last_added[aux] + 1, v[last_added[aux]], c[last_added[aux]], aux - c[last_added[aux]]); aux -= c[last_added[aux]];

19

Name: Tanmay Baid Roll Number: 0829cs071112 } printf("Total value added: %d$\n", a[W]); } void main(int argc, char *argv[]) { fill_sack(); }

20

Name: Tanmay Baid Roll Number: 0829cs071112

Output

21

Name: Tanmay Baid Roll Number: 0829cs071112

Multiplication of Matrix #include<stdio.h> #include int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; void arr_prod(int [10][10],int [10][10]); void main() { printf("ENTER THE NO OF ROWS OF MATRIX A:"); scanf("%d",&m); printf("ENTER THE NO OF COLUMNS OF MATRIX A:"); scanf("%d",&n); printf("ENTER THE NO OF ROWS OF MATRIX B:"); scanf("%d",&p); printf("ENTER THE NO OF COLUMNS OF MATRIX B:"); scanf("%d",&q); if(n!=p) printf("\nMARTIX MULTIPLICATION NOT POSSIBLE"); else { printf("\nENTER ELEMENTS FOR FIRST MATRIX:\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=n-1;j++) { scanf("%d",&a[i][j]); } } printf("\nENTER ELEMENTS FOR SECOND MATRIX:\n"); for(i=0;i<=p-1;i++) { for(j=0;j<=q-1;j++) {

22

Name: Tanmay Baid Roll Number: 0829cs071112 scanf("%d",&b[i][j]); } } arr_prod(a,b); printf("PRODUCT OF MARIX A AND B IN MATRIX FORM IS:\n\n"); for(i=0;i<=m-1;i++) { for(j=0;j<=q-1;j++) { printf("%d\t",c[i][j]); } printf("\n"); } } } void arr_prod(int a[10][10],int b[10][10]) { int k; for(i=0;i<=m-1;i++) { for(j=0;j<=q-1;j++) { c[i][j]=0; for(k=0;k<=p-1;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } }

23

Name: Tanmay Baid Roll Number: 0829cs071112

Output

24

Name: Tanmay Baid Roll Number: 0829cs071112

Factorial #include<stdio.h> #include int fact(int); void main() { int n,result; clrscr(); printf("Enter the number: "); scanf("%d",&n); result=fact(n); printf("\nThe factorial of %d is: %d",n,result); getch(); } int fact(int x) { int f; if(x==1) { return(x); } else { return(x*fact(x-1)); } }

25

Name: Tanmay Baid Roll Number: 0829cs071112

Output Enter the number: 5 The factorial of 5 is:120

26

Name: Tanmay Baid Roll Number: 0829cs071112

Fibonacci #include<stdio.h> #include int fibnum(int); void main() { int p,num; clrscr(); printf("Enter the position of element you want from Fibonacci series: "); scanf("%d",&p); num=fibnum(p); printf("The required Fibonacci number is: %d",num); getch(); } int fibnum(int p) { if(p==1||p==0) { return(p); } else { return(fibnum(p-1)+fibnum(p-2)); } }

27

Name: Tanmay Baid Roll Number: 0829cs071112

Output Enter the position of element you want from Fibonacci series: 6 The required Fibonacci number is: 8

28

Related Documents

Ada
November 2019 53
Ada
July 2020 33
Ada
October 2019 34
Ada Augusta Ada Byron
April 2020 23

More Documents from "TB"