Consider the following somewhat strange algorithm to find th

Consider the following (somewhat strange) algorithm to find the smallest element of a list L:

Solution

// Done the program below in two steps, the elements from 0 to k and from k+1 to L
#include<iostream.h>
#include<conio.h>
int minimum(int array[],int k)
{
int min1;
if(k==1)
return array[0];
else
{
min1=minimum(array,k-1);
if(min1<array[k-1]){ return min1;}
else
return array[k-1];
}
}

int min(int a[],int m)
{
int min2;
if(m==1)
return a[0];
else
{
min1=minimum(a,m-1);
if(min2<array[m-1]){ return min2;}
else
return array[m-1];
}
}
void main()

{
int i,j,n,array[20],L,k;
clrscr();
cout<<\"enter total no.of elements L:\";
cin>>L;
cout<<\"enter limit k:\";
cin>>k;
cout<<\"enter values till position k : \";
for(i=0;i<k;i++)
{
cin>>array[i];
}
int m=L-k;
cout<<\"enter values from k+1 to L:\";
for(j=0;j<m;j++)
{
cin>>a[j];
}
int Min1= minimum(array,k);
int Min2= min(a,m);
cout<<\"Min1=\"<<Min1;
cout<<\"Min2=\"<<Min2;
if(Min1<Min2){cout<<\"Min1 is lesser than Min2\";}
else{cout<<\"Min2 is lesser than Min1\";}
getch();
}
__________________________________________________
Mathematical induction
__________________________________________________
Prove : 2 sections:
1 + 2 + 3 +.....+k+(k+1)+ ... + L = L (L + 1) / 2
according to above problem.
step1: check for p(1)=1(1+1)/2=1
step2: assume p(k) is true and check for p(K+1)
1 + 2 + 3 + ... + k = k (k + 1) / 2
1 + 2 + 3 + ... + k + (k + 1) = k (k + 1) / 2 + (k + 1)
                               = (k + 1)(k + 2) / 2
___________________________________________________________________________


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site