Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define N 30
  5. typedef int Tab[N];
  6. void wypelnij(Tab t)
  7. {
  8. int i;
  9. srand(time(0));
  10. for(i=0;i<N;++i)
  11. {t[i]=rand()%101;}
  12. }
  13. void sort(Tab t)
  14. {
  15. int i,j,tmp;
  16. for(i=0;i<N-1;++i)
  17. for(j=0;j<N;++j)
  18. {
  19. if(t[j]>t[j+1])
  20. {
  21. tmp=t[j];
  22. t[j]=t[j+1];
  23. t[j+1]=tmp;
  24. }
  25. }
  26. }
  27. void wyswietl(Tab t)
  28. {
  29. int i;
  30. for(i=0;i<N;++i)
  31. printf("Indeks:%d Wartosc:%d\n",i,t[i]);
  32. }
  33. int szukaj(Tab t, int poczatek,int koniec, int szukana)
  34. {
  35. if(poczatek>koniec)
  36. {return -1;}
  37. int srodek=(poczatek+koniec)/2;
  38. if(szukana==t[srodek])
  39. {return srodek;}
  40. if(szukana<t[srodek])
  41. return szukaj(t,poczatek,srodek-1,szukana);
  42. else
  43. return szukaj(t,srodek+1,koniec,szukana);
  44. }
  45. int main()
  46. {
  47. Tab t;
  48. int poczatek=0,koniec=N-1,szukana,miejsce,czy;
  49. wypelnij(t);
  50. sort(t);
  51. printf("Jakiej liczby szukasz: ");
  52. scanf("%d",&szukana);
  53. miejsce=szukaj(t,poczatek,koniec,szukana);
  54. if(miejsce==-1)
  55. printf("Liczba: %d nie wystepuje w tablicy\n",szukana);
  56. else
  57. printf("Liczba: %d występuje w tablicy o indeksie %d\n",szukana,miejsce);
  58. puts("Wyswietlic tablice dla sprawdzenia\n 1.Tak\n 2.Nie");
  59. scanf("%d",&czy);
  60. if(czy==1)
  61. wyswietl(t);
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement