Sunteți pe pagina 1din 5

Page | 1

PIXELES CLASSES
MCA (IGNOU)
Course Code
Course Title

BCA &

: BCSL-045

: 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};
smallest(arr, 6);
getch();
}
2. Write a program to generate Fibonacci series of 10 numbers.
marks)
Ans:
#include<stdio.h>
main()

www.pixelesindia.com

(5

Page | 2

PIXELES CLASSES
MCA (IGNOU)

BCA &

{
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.


Ans:
void main()

(4 marks)

Branches & Contacts Details

gar:-WZ-B7, Old {Pankha Road (Opp.Nangloi:-Plot.


Primary School),
Near ExtEast Metro
Station, Uttam Nagar,
New Delhi-59
No-19,
2A, oppBanke-Bihari,
Talabwali
Road, N

char *s,*v,*p;
clrscr();
printf(enter the string)
gets(s);

Ph: 9213327975, 9716339580

v=p;

8750321695

do

, web: www.pixelesindia.com

{
*p=*s;
*p++;
*s++;
} while(*s!=NULL);
printf("\n the copied string is ::- %s",v);
getch();
}

www.pixelesindia.com

Page | 3

PIXELES CLASSES
MCA (IGNOU)

BCA &

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 10
int 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");
else
printf("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];

www.pixelesindia.com

Page | 4

PIXELES CLASSES
MCA (IGNOU)
a[i]=a[j];
a[j]=t;
}
}
}
}
void merge(int a[],int b[],int c[]) //this function passed three arguments
{
//as array first and second array will
int 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 array
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
//other wise element of second array
j++;
}
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);

www.pixelesindia.com

BCA &

Page | 5

PIXELES CLASSES
MCA (IGNOU)

BCA &

printf("\n\nMy first Array:");


disp(b,5);
merge(a,b,c);
printf("\n\nMerged Array:");
disp(c,10);
getch();
}
6. Write a program to sort data using a selection sort algorithm.
marks)

(10

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]);
}

We are teaching IGNOUs 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

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

S-ar putea să vă placă și