Mam takie zadanie napisać co wyświetli program, ale nie wychodzi mi ta sama liczba co pokazuje kompilator.
#include <stdio.h>
int funk(int tab[], int x, int start, int koniec)
{
int srodek;
srodek = (start + koniec) / 2;
if(x == tab[srodek] || start > koniec)
{
return srodek;
}
if(x < tab[srodek])
{
tab[srodek] += 1;
return funk(tab,x,srodek + 1,koniec);
}
else
{
tab[srodek] -= 2;
return (funk(tab,x,srodek + 1,koniec));
}
}
int main()
{
int tab[20], k,m;
for(k = 0;k<20;k=k+1)
{
tab[k] = 1;
}
m = -1;
printf("%d\n",funk(tab, m, 0, 19));
for(k=1;k<20;k=k+2)
{
printf("%d ",tab[k]);
}
return 0;
}
W funkcji funk wykonuję się ten drugi warunek if, dla mnie wychodzi coś takiego:
Na początku jest:
x = -1
start = 0
koniec = 19
srodek = 9
Po wywołaniu rekurencyjnym:
tab[9] = 1
s = 1
k = 9
sr = 5
tab[5] = 1
s = 2
k = 5
sr = 5
I dalej już nie ciągnę tego bo to nie ma sensu. Coś źle robię ?
dodanie znaczników <code class="c">
- @furious programming