Question

Write a C++ function that takes a pointer to an array of integers as a parameter...

Write a C++ function that takes a pointer to an array of integers as a parameter and return the number of prime integers in the array. Write main() program to test the function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstdlib>

using namespace std;

int count_primes(int *arr, int size) {
    int count = 0, n;
    bool is_prime;
    for (int i = 0; i < size; ++i) {
        n = arr[i];
        is_prime = n > 1;
        for (int j = 2; j < n; ++j) {
            if (n % j == 0) {
                is_prime = false;
            }
        }
        if (is_prime)
            ++count;
    }
    return count;
}

int main() {
    int size;
    cout << "Enter size of array: ";
    cin >> size;
    cout << "Enter " << size << " integers: ";
    int *arr = new int[size];
    for (int i = 0; i < size; ++i) {
        cin >> arr[i];
    }
    int count = count_primes(arr, size);
    cout << "Number of primes in array is " << count << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ function that takes a pointer to an array of integers as a parameter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT