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 .
#include <iostream>
usingnamespace 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){
returnfalse;
}
if(arrayToSearch[i] == arrayOne[j]){
if(j >= lastIndex){
lastIndex = j;
break;
}else{
returnfalse;
}
}
}
}
returntrue;
}
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.
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
#include <iostream>
usingnamespace 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;
}