C++ check if array contains all array values from another array

Oct 7, 2016 at 4:21pm
I want to make a program that will check if an array contains all the array values from another array. So, if that's true, the program will return True using a value for it. If it isn't true, it will return False. I've got found the code that does this but But I do not understand the algorithm . Could you please write the code by using pointers. And also do this with other algorithms using pointers ? I do not know please guide me. Thanks .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool searchArray(int arrayToSearch[] , int arrayOne[] , int arrayToSearchLength, int arrayOneLength);

int main(int argc, char** argv) {
  cout << "enter length of first array :";
  int arrayLengthOne;
  cin >> arrayLengthOne;
  int ai = 0;
  int arrayOne[arrayLengthOne];
  while (ai < arrayLengthOne){
    cout << "enter number for index " << ai << " :";
    cin >> arrayOne[ai];
    ai++;
  }

  cout << "enter length of second array :";
  int arrayLengthTwo;
  cin >> arrayLengthTwo;

  ai = 0;
  int arrayToSearch[arrayLengthTwo];
  while (ai < arrayLengthTwo){
    cout << "enter number for index " << ai << " :";
    cin >> arrayToSearch[ai];
    ai++;
  }

  bool result = searchArray(arrayToSearch, arrayOne, arrayLengthTwo, arrayLengthOne);

  if(result){
    cout << "True \n";
  }else{
    cout << "False \n";
  }


  system("pause");
  return 0;
}

bool searchArray(int arrayToSearch[] , int arrayOne[], int arrayToSearchLength, int arrayOneLength){
  int lastIndex = 0;
  for(int i = 0; i < arrayToSearchLength ; i++){
      for(int j = lastIndex; j < arrayOneLength + 1 ; j++){
          if (j == arrayOneLength){
              return false;
          }
          if(arrayToSearch[i] == arrayOne[j]){
            if(j >= lastIndex){
              lastIndex = j;
              break;
        }else{
          return false;
        }
          } 
      }
  }
  return true;
}
Oct 7, 2016 at 4:26pm
I quickly ran the program and it does what it was meant to do. Why do you need to use pointers? If the program provides the necessary functionality you shouldn't need any particular implementation.
Oct 7, 2016 at 4:31pm
I want to learn how to use a pointer to do this
Oct 7, 2016 at 4:33pm
Oct 7, 2016 at 4:41pm
I already tried ( http://www.cplusplus.com/doc/tutorial/pointers/ )
But I could not do it
I am not good at programming :(
Please write the program with pointers .I need it so much . please
Oct 7, 2016 at 6:33pm
please . someone help me :((
Oct 8, 2016 at 6:01pm
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main()
{
    int p = 5;
    int* pointer = &p;//pointer points to the memory location of p
    cout << "p: " << p << endl;//p is 5
    cout << "*pointer: " << *pointer << endl;//the value at pointer is 5
    *pointer = 7;//change the variable that pointer points to: 7
    cout << "p now: " << p << endl;//p is 7
    return 0;
}


Do you understand what this program does?
Last edited on Oct 8, 2016 at 6:02pm
Topic archived. No new replies allowed.