#include<conio.h>
#include<stdio.h>
int jum, mxb, mnb;
int tab[10];
//menginput banyaknya inputan yang akan dibandingkan
void input(){
int i;
printf("masukan jumlah :");
scanf("%d",&jum);
for(i=1; i<=jum;i++)
{
printf("angka ke-%d: ",i);
scanf("%d", &tab[i]);
}
}
//mengecek nilai maksimal dari inputan
int cekmx(int tb[10], int a, int b)
{
int k, max1, max2;
if(a==b)
{
mxb = tb[a];
}
else
{
if(a==b-1)
{
if(tb[a]<tb[b])
mxb=tb[b];
else
mxb=tb[a];
}
else
{
k=(a+b)/2;
max1=cekmx(tb, a, k);
max2=cekmx(tb, k+1, b);
if(max1 < max2)
mxb=max2;
else
mxb=max1;
}
}
return mxb;
}
//mengecek nilai mininum dari inputan
int cekmn(int tb[10], int a, int b)
{
int q, mn1, mn2;
if(a==b)
{
mnb=tb[a];
}
else
{
if(a==b-1)
{
if(tb[a] < tb[b])
mnb=tb[a];
else
mnb=tb[b];
}
else
{
q=(a+b)/2;
mn1=cekmn(tb, a, q);
mn2=cekmn(tb, q+1, b);
if(mn1 < mn2)
mnb=mn1;
else
mnb=mn2;
}
}
return mnb;
}
/*program utama
menampilkan nilai mak dan min */
main()
{
clrscr();
input();
printf("\n");
printf("max=%d\n",cekmx (tab, 1, jum));
printf("min=%d\n",cekmn (tab, 1, jum));
getch();
return 0;
}
Tuesday, November 30, 2010
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment