Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define N 30
- typedef int Tab[N];
- void wypelnij(Tab t)
- {
- int i;
- srand(time(0));
- for(i=0;i<N;++i)
- {t[i]=rand()%101;}
- }
- void sort(Tab t)
- {
- int i,j,tmp;
- for(i=0;i<N-1;++i)
- for(j=0;j<N;++j)
- {
- if(t[j]>t[j+1])
- {
- tmp=t[j];
- t[j]=t[j+1];
- t[j+1]=tmp;
- }
- }
- }
- void wyswietl(Tab t)
- {
- int i;
- for(i=0;i<N;++i)
- printf("Indeks:%d Wartosc:%d\n",i,t[i]);
- }
- int szukaj(Tab t, int poczatek,int koniec, int szukana)
- {
- if(poczatek>koniec)
- {return -1;}
- int srodek=(poczatek+koniec)/2;
- if(szukana==t[srodek])
- {return srodek;}
- if(szukana<t[srodek])
- return szukaj(t,poczatek,srodek-1,szukana);
- else
- return szukaj(t,srodek+1,koniec,szukana);
- }
- int main()
- {
- Tab t;
- int poczatek=0,koniec=N-1,szukana,miejsce,czy;
- wypelnij(t);
- sort(t);
- printf("Jakiej liczby szukasz: ");
- scanf("%d",&szukana);
- miejsce=szukaj(t,poczatek,koniec,szukana);
- if(miejsce==-1)
- printf("Liczba: %d nie wystepuje w tablicy\n",szukana);
- else
- printf("Liczba: %d występuje w tablicy o indeksie %d\n",szukana,miejsce);
- puts("Wyswietlic tablice dla sprawdzenia\n 1.Tak\n 2.Nie");
- scanf("%d",&czy);
- if(czy==1)
- wyswietl(t);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement