Commit 60e9c083 by glkink

Add new file

parent a711c2bf
Showing with 47 additions and 0 deletions
#include <stdio.h>
int kahend(int massiiv[], int esimene, int viimane, int otsi)
{
int m;
if (esimene > viimane)
return -1;
m = (esimene + viimane)/2;
if (massiiv[m] == otsi)
return m;
else if (otsi > massiiv[m])
return kahend(massiiv, m+1, viimane, otsi);
else
return kahend(massiiv, esimene, m-1, otsi);
}
int main()
{
int esimeneElement, viimaneElement, elemendid, otsiElement, massiivOG[100], loend, n;
printf("Sisesta elementide arv:\n");
scanf("%d", &n);
printf("Sisesta %d taisarvu:\n", n);
for (elemendid = 0; elemendid < n; elemendid++)
scanf("%d", &massiivOG[elemendid]);
printf("Sisesta vaartus mida otsid:\n");
scanf("%d", &otsiElement);
esimeneElement = 0;
viimaneElement = n - 1;
loend = kahend(massiivOG, esimeneElement, viimaneElement, otsiElement);
if (loend == -1)
printf("%d pole massiivis!\n", otsiElement);
else
printf("Arv %d on massiivis kohas %d.\n", otsiElement, loend + 1);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment