Answer to Question #286458 in C++ for Aleeza

Question #286458

Search all occurrences of an element in an array of 10 elements using ptr notation.

1
Expert's answer
2022-01-10T17:31:22-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment