Search all occurrences of an element in an array of 10 elements using ptr notation.
#include <iostream>
using namespace std;
int main() {
int arr[10] = {2, 3, 0, 5, 1, 0, 7, 0, 0, 4};
int el_search = 0;
int* ptr = arr;
int count = 0;
for (int i=0; i<10; i++) {
if (*ptr == el_search) {
count++;
}
ptr++;
}
cout << "Element " << el_search << " has found " << count << " times" << endl;
return 0;
}
Comments
Leave a comment