alilprogrammerappears
alilprogrammerappears

Reputation: 25

C++: How to see if an element in an array is over a certain value?

New to C++, so I apologize in advance for my ineptitude.

I need to write a function outside of function Main that checks the elements of an array (that were entered by the user) to see if they are over 10. If they are, I need to print that number of elements as well as a list of the elements themselves. If they aren't, I need to display a message that says there aren't any.

I've tried various things, but this is the one that I keep coming back to. Can you help out?

void print_over_ten(const int list[], int length)
    //list[] contains user entered elements
    //length contains the length of the array, list[]
{
    int index=0;   //counter
    int amount;  //number of elements over 10
    int number_over_ten;    //variable for numbers over ten

    cout << "\nThese numbers are over ten: ";

    if (index > 10 && index <= length)
    {
        number_over_ten = index-1;
        cout << number_over_ten << " ";
        index++;
    }
    else
        cout << "\nThere are no numbers over 10.\n";

    amount = index;

    cout << "There are " << amount << " numbers over 10.\n";    
}

I think most of that is probably wrong, so feel free to trash that.

Any help is greatly appreciated. Thanks!

Upvotes: 2

Views: 712

Answers (3)

Kostas
Kostas

Reputation: 4176

You can simplify this a lot by using std::copy_if from the <algorithm> STL file:

std::vector<int> get_over_10(const int list[], int length) {
  std::vector<int> over_10;
  std::copy_if(list, list+length, std::back_inserter(over_10),
      [](int i) { return i > 10; });
  return over_10;
}

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 596507

If you just need to count the matching elements, you can use a simple loop, eg:

void print_over_ten(const int list[], int length)
{
    int amount = 0;
    for (int i = 0; i < length; ++i) 
    {
        if (list[i] > 10)
            ++amount;
    }
    if (amount > 0)
        cout << "There are " << amount << " numbers over 10.";
    else
        cout << "There are no numbers over 10.";
} 

Or, the standard std::count_if() algorithm, eg:

#include <algorithm>

void print_over_ten(const int list[], int length)
{
    size_t amount = std::count_if(list, list+length, [](int i){ return i > 10; });
    if (amount > 0)
        cout << "There are " << amount << " numbers over 10.";
    else
        cout << "There are no numbers over 10.";
} 

But, if you need to actually display the individual numbers that match, you will need to compare each number individually, eg:

void print_over_ten(const int list[], int length)
{
    for(int i = 0; i < length; ++i)
    {
        if (list[i] > 10)
        {
            int amount = 1;
            cout << "These numbers are over ten: " << list[i];
            for (++i; i < length; ++i)
            {
                if (list[i] > 10)
                {
                    ++amount;
                    cout << " " << list[i];
                }
            }
            cout << "\nThere are " << amount << " numbers over 10.";
            return;
        }
    }

    cout << "There are no numbers over 10.";
} 

Or, you could gather the matching numbers into a container, like a std::vector, eg:

#include <vector>

void print_over_ten(const int list[], int length)
{
    std::vector<int> numbers;
    for (int i = 0; i < length; ++i) 
    {
        if (list[i] > 10)
            numbers.push_back(list[i]);
    }
    if (!numbers.empty())
    {
        cout << "These numbers are over ten:";
        for(int i : numbers) {
            cout << " " << i;
        }
        cout << "\nThere are " << numbers.size() << " numbers over 10.";
    }
    else
        cout << "There are no numbers over 10.";
} 

Upvotes: 3

cigien
cigien

Reputation: 60228

Your if condition is mostly right, but you need to do that check as you iterate over the array with a loop:

for (int i = 0; i < length; ++i)
{
  if (list[i] > 10)
    ++index;
}

if (index > 10)
    cout << "There are " << index << " numbers over 10.\n";    
else
    cout << "\nThere are no numbers over 10.\n";

Minor note on naming: the variable index doesn't describe what it does. You should probably call it counter as you have written in a comment, or amount, as you you have later assigned it to a variable with that name.


Even better than a loop is to use an algorithm like this:

int amount = std::count_if(list, list + length, [](int i) { return i > 10; });

Upvotes: 3

Related Questions