Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- int cautare(int v[], int n, int element)
- {
- for (int i = 0;i < n;i++)
- {
- if (v[i] == element)
- return i;
- else
- return -1;
- //throw invalid_argument("value not available");
- }
- }
- int cautare2(int v[], int elm ,int p,int u)
- {
- if (p > u)
- return (-1);
- int mijloc = v[(p + u)/2];
- if (v[mijloc] == elm)
- return mijloc;
- if (v[mijloc] < elm)
- return cautare2(v, elm, mijloc+1, u);
- return cautare2(v, elm, p, mijloc-1);
- }
- void main()
- {
- int v[] = {1,3,5,7,9,11};
- //cautare 1
- cout << "Elementul cautat a fost gasit pe pozitia: " << cautare(v, 6, 99) << endl;
- cout << sizeof(v) / sizeof(int) - 1;
- //cautare 2
- cout << cautare2(v, 2, 0, sizeof(v)/sizeof(int)-1);
- //pause
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement