BCSL-045

7
Page | 1 PIXELES CLASSES BCA & MCA (IGNOU) Course Code : BCSL-045 Course Title : Introduction to Algorithm Design Lab Assignment Number : BCA(IV)/L-045/Assignment/2015 1. Write a program that finds the two smallest numbers in an array of n numbers. (5 marks) Ans: #include <stdio.h> #include <limits.h> /* For INT_MAX */ void smallest(int arr[], int arr_size) { int i, first, second; if (arr_size < 2) { printf(" Invalid Input "); return; } first = second = INT_MAX; for (i = 0; i < arr_size ; i ++) { if (arr[i] < first) { second = first; first = arr[i]; } else if (arr[i] < second && arr[i] != first) second = arr[i]; } if (second == INT_MAX) printf("There is no second smallest element\n"); else printf("Two smallest element are %d and %d\n",first, second); } main() { int arr[] = {19, 45, 3, 25, 34, 5}; www.pixelesindia.com

description

ty

Transcript of BCSL-045

Page 1: BCSL-045

Page | 1

PIXELES CLASSES BCA & MCA (IGNOU)Course Code : BCSL-045

Course Title : Introduction to Algorithm Design Lab

Assignment Number : BCA(IV)/L-045/Assignment/2015

1. Write a program that finds the two smallest numbers in an array of n numbers. (5 marks)

Ans:

#include <stdio.h>#include <limits.h> /* For INT_MAX */void smallest(int arr[], int arr_size){int i, first, second;

if (arr_size < 2) {printf(" Invalid Input ");return; }

first = second = INT_MAX;for (i = 0; i < arr_size ; i ++){if (arr[i] < first) {second = first;first = arr[i];}

else if (arr[i] < second && arr[i] != first)second = arr[i];}if (second == INT_MAX)printf("There is no second smallest element\n");elseprintf("Two smallest element are %d and %d\n",first, second);}main(){ int arr[] = {19, 45, 3, 25, 34, 5}; smallest(arr, 6); getch();}

2. Write a program to generate Fibonacci series of 10 numbers. (5 marks)

Ans:

#include<stdio.h>

www.pixelesindia.com

Page 2: BCSL-045

Page | 2

Branches & Contacts Details

Ph: 9213327975, 97163395808750321695, web: www.pixelesindia.com

Nangloi:-Plot. No-19, Ext- 2A, oppBanke-Bihari, Talabwali Road, Nangloi, Delhi-41Uttam Nagar:-WZ-B7, Old Pankha Road (Opp. Primary School), Near East Metro Station, Uttam Nagar, New Delhi-59

PIXELES CLASSES BCA & MCA (IGNOU)main()

{

int a, b, c, i;

a = 0;

b = 1;

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

{

c=a+b;

a=b;

b=c;

printf(“%d”, c);

}

getch();

}

3. Write a program to reverse a string. (4 marks)

Ans:

void main()

{

char *s,*v,*p;

clrscr();

printf(“enter the string”)

gets(s);

v=p;

do

{

*p=*s;

*p++;

*s++;

} while(*s!=NULL);

printf("\n the copied string is ::- %s",v);

getch();

}

www.pixelesindia.com

Page 3: BCSL-045

Page | 3

PIXELES CLASSES BCA & MCA (IGNOU)

4. Write a program to do linear search for an integer number in an array of 20 distinct numbers. The program should return the location of an element if the number found in the array.(6 marks)

Ans:

A Program for linear search.

#include<stdio.h>#define max 10int lsearch(int arr[],int n){int i;for(i=0;i<=max-1;i++){if(arr[i]==n)return i;}return -1;}

void main(){int arr[max]={5,6,7,3,2,4,5,16,17,9};int i, n,find;clrscr();printf("enter the number for searching");scanf("%d",&n);find=lsearch(arr,n);if (find==-1)printf("not found");elseprintf("found at the position=%d",find);

getch();}

5. Implement merge sort algorithm. (10 marks)

Ans:

#include<stdio.h>void sort(int a[]) // used for sorting array{int i,j,t; for(i=0;i<=4;i++) { for(j=i;j<=4;j++) {

if(a[i]>=a[j]){t=a[i];a[i]=a[j];a[j]=t;

www.pixelesindia.com

Page 4: BCSL-045

Page | 4

PIXELES CLASSES BCA & MCA (IGNOU) } } }}void merge(int a[],int b[],int c[]) //this function passed three arguments{ //as array first and second array willint i=0,j=0,k=0; //will be merged into third.while(i<5 && j<5) //loop executes up to i or j becomes false{if(a[i]<b[j]) // if element of first array is less than second array,{ // it will store into third arrayc[k]=a[i];i++;}else{c[k]=b[j]; //other wise element of second arrayj++;}k++;}while(j<5) //j is less than 5 then all elements of second array go to third array{c[k]=b[j];k++;j++;

}while(i<5) //i is less than 5 then all elements of first array go to third array{c[k]=a[i];k++;i++;}}void disp(int c[],int size) // to display element of array{int i;for(i=0;i<size;i++)printf(" %d",c[i]);}

main(){int a[]={5,8,2,7,10},b[]={1,4,12,9,11};int c[10],j,k;clrscr();sort(a);sort(b);printf("\nMy first Array:");disp(a,5);printf("\n\nMy first Array:");disp(b,5);merge(a,b,c);printf("\n\nMerged Array:");

www.pixelesindia.com

Page 5: BCSL-045

Page | 5

PIXELES CLASSES BCA & MCA (IGNOU)disp(c,10);getch();}

6. Write a program to sort data using a selection sort algorithm. (10 marks)

Ans:

#include<stdio.h>void selection(int arr[], int size){int small,pos, temp,i,j,k;for(i=0;i<size;i++){small=arr[i];for(j=i;j<size;j++){if(arr[j]<=small){small=arr[j];pos=j;}}temp=arr[i];arr[i]=arr[pos];arr[pos]=temp;}}void disp(int arr[], int size){int i;clrscr();for(i=0;i<size;i++)printf("%d\t",arr[i]);}

void main(){int arr[]={5,13,18,19,2,7,4,12,1,6};clrscr();selection(arr,10);disp(arr,10);getch();}

www.pixelesindia.com

We are teaching IGNOU’s BCA & MCA Students

Why join us?

Regular Classes

BCA & MCA IGNOU Special Institute

Free Trial Classes

Subjective Knowledge

Free PIXELES Guide Books (Prepared by our

teachers)

Free Solved Assignments

Experienced Faculties

100% Results

Home Test Series

Class Test Series

We teach you until you pass

Final Year Synopsis & Project

Proper Guidance