Sei sulla pagina 1di 5

BCA & MCA (IGNOU)

http://www.ignouassignmentguru.com
Course Code : BCSL-045

Course Title : Introduction to Algorithm Design Lab

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

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) 1
IGNOU ASSIGNMENT GURU Page-
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. (5 marks)

Ans:

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
#include<stdio.h>

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();
2
IGNOU ASSIGNMENT GURU Page-
printf(“enter the string”)
gets(s);
v=p;
do
{
*p=*s;
*p++;
*s++;
} while(*s!=NULL);

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
printf("\n the copied string is ::- %s",v);
getch();
}

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

voidmain()
{
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);
3
IGNOU ASSIGNMENT GURU Page-

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++)

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
{
if(a[i]>=a[j])
{
t=a[i];
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++; 4
IGNOU ASSIGNMENT GURU Page-
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();

/ignouassignmentfree
BCA & MCA (IGNOU)
http://www.ignouassignmentguru.com
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:");
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)
{
5
IGNOU ASSIGNMENT GURU Page-
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();
}

/ignouassignmentfree

Potrebbero piacerti anche