Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. // C1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8.  
  9. int cautare(int v[], int n, int element)
  10. {
  11. for (int i = 0;i < n;i++)
  12. {
  13. if (v[i] == element)
  14. return i;
  15. else
  16. return -1;
  17. //throw invalid_argument("value not available");
  18. }
  19. }
  20.  
  21. int cautare2(int v[], int elm ,int p,int u)
  22. {
  23. if (p > u)
  24. return (-1);
  25.  
  26. int mijloc = v[(p + u)/2];
  27.  
  28. if (v[mijloc] == elm)
  29. return mijloc;
  30.  
  31. if (v[mijloc] < elm)
  32. return cautare2(v, elm, mijloc+1, u);
  33.  
  34. return cautare2(v, elm, p, mijloc-1);
  35. }
  36.  
  37.  
  38.  
  39. void main()
  40. {
  41. int v[] = {1,3,5,7,9,11};
  42.  
  43. //cautare 1
  44. cout << "Elementul cautat a fost gasit pe pozitia: " << cautare(v, 6, 99) << endl;
  45.  
  46. cout << sizeof(v) / sizeof(int) - 1;
  47.  
  48. //cautare 2
  49. cout << cautare2(v, 2, 0, sizeof(v)/sizeof(int)-1);
  50.  
  51. //pause
  52. system("pause");
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement