Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*sa se scrie un program ce foloseste un subprog. recursiv cu metoda DEI ce afiseaza pozitia pe care se gaseste un element x in matrice */
- #include <iostream>
- #define NMAX 100
- using namespace std;
- void readVar(char varname, int &var) {
- cout << varname << '=';
- cin >> var;
- }
- void readMat(int &n, int &m, int a[][NMAX]) {
- readVar('n', n);
- readVar('m', m);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> a[i][j];
- }
- int findInV(int v[], int p, int q, int x) {
- if (p > q)
- return -1;
- int mid = (p + q) / 2;
- if (v[mid] == x)
- return mid;
- int pos = findInV(v, p, mid - 1, x);
- return pos == -1 ? findInV(v, mid + 1, q, x) : pos;
- }
- void findInM(int a[][NMAX], int p, int q, int x, int m) {
- if (p > q)
- return;
- int mid = (p + q) / 2;
- int pos = findInV(a[mid], 0, m - 1, x);
- if (pos != -1) {
- cout << mid << ' ' << pos << endl;
- return;
- }
- findInM(a, p, mid - 1, x, m);
- findInM(a, mid + 1, q, x, m);
- }
- int n, m, a[NMAX][NMAX], x;
- int main() {
- readMat(n, m, a);
- readVar('x', x);
- findInM(a, 0, n - 1, x, m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement