Explain what needs to be done. The task:

The function checks whether array B is a subset of array A and returns a pointer to the beginning of the found fragment.

How can I return a pointer? If possible, with an example. That's what happened, but it certainly does not work, please find the error.

#include<iostream> using namespace std; int* proverca(int* A,int* B, int size1, int size2){ int* ptr ; int i = 0; for (i = 0; i < size1; i++){ int j = i, k = 0; while(j < size1 && k < size2 && B[k] == A[j]){ j++; k++; }if(k == size2) return ptr = A + i; else return 0; } } int main(){ setlocale(LC_ALL, "rus"); int size1, size2; cout << "Введите размер массива A: "; cin >> size1; int* A = new int[size1 + 1]; cout << "Заполните массив: "; for(int i = 0; i<size1; i++) cin >> A[i]; cout << "Введите размер массива B: "; cin >> size2; int* B = new int[size2]; cout << "Заполните массив: "; for(int i = 0; i<size2; i++) cin >> B[i]; cout << "Элемент должен стоять на позиции " << *proverca(A, B, size1, size2) << endl; delete []A; delete []B; return 0; } 
  • show your solution first? - ampawd
  • And what is the problem to return the pointer? What exactly is coming out? return (тут ваш указатель) and everything in general. - VladD pm
  • Subset or subsequence? - Cerbo
  • @Cerbo subset - Asya Filatova
  • @ AsyaFilatova You are trying to find just a subsequence, well, return the index And where B. begins. If we consider strictly the sets, then by definition there cannot be any fragments and indexes. So tell your teacher to give a clearer assignment. - Cerbo

2 answers 2

The problems are as follows:

 int* proverca(int* A,int* B, int size1, int size2) { int* ptr ; int i = 0; for (i = 0; i < size1; i++) { int j = i, k = 0; while(j < size1 && k < size2 && B[k] == A[j]) { j++; k++; } if(k == size2) return ptr = A + i; //else // return 0; // эта ветка здесь лишняя, вы возвращаете 0 после // первой же неудачной попытки } return 0; // она должна быть здесь } 
  • Yes, it worked, thanks) - Asya Filatova

Your task describes exactly the behavior of the std :: search algorithm. Its use looks like this:

 #include <iostream> #include <algorithm> int main(){ int first[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int second[4] = {4, 5, 6, 7}; int *match = std::search(&first[0], &first[10], &second[0], &second[4]); } 

As far as I understand, the one who gave you this task will not be satisfied with such a solution:

 int* search(int *aBegin, int *aEnd, int *bBegin, int *bEnd){ return std::search(aBegin, aEnd, bBegin, bEnd); } 

Although what the hell is not joking, you can try to show off the knowledge of stl :)

If suddenly this option is not suitable, you can peep the implementation of this algorithm for the link that I gave you. If you throw out templates from there, you get something like this:

 int* search(int *aBegin, int *aEnd, int *bBegin, int *bEnd){ while (aBegin != aEnd){ int *it1 = aBegin; int *it2 = bBegin; while (*it1 == *it2){ ++it1; ++it2; if(it2 == bEnd){ return aBegin; } if(it1==aEnd){ return aEnd; } } ++aBegin; } return aEnd; }