search table for a specific number and count how many times it appears

advertisements

i have an array of ages from 0-100. you keep entering the ages until sentinel data(-99) is entered.

i need to then count how many times each age appears.

example output:
The number of people 5 years old is 3
The number of people 8 years old is 1
The number of people 9 years old is 1
The number of people 10 years old is 1
The number of people 17 years old is 1
The number of people 20 years old is 2
The number of people 100 years old is 1

However, when i do it, my output it this:

The number of people age 5 is 3
The number of people age 10 is 1
The number of people age 100 is 1
The number of people age 20 is 2
The number of people age 5 is 3
The number of people age 8 is 1
The number of people age 20 is 2
The number of people age 5 is 3
The number of people age 9 is 1
The number of people age 17 is 1

i need to fix it so that it would only display the 5's once, and etc.

here is my code:

    #include <iostream>
    using namespace std;

    const int MAXSIZE = 100;
    int ages[MAXSIZE];

    int findNumOfAges(int ages[], int pos, int i);

    int main()
    {
        int pos = 0;
        int age;

    for(int count = 0; count < MAXSIZE; count++)
{
    ages[count] = 0;
}

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ages[pos] = age;

    }
    pos++;

}while(age != -99);

    for(int i = 0; i < pos; i++)
    {
        int numOfAges;

        numOfAges = findNumOfAges(ages, pos, i);

        cout << "The number of people age " << ages[i] << " is " << numOfAges << endl;
    }

}

int findNumOfAges(int ages[], int pos, int i)
{
int numOfAges = 0;

for(int j = 0; j < pos; j++)
{
    if(ages[i] == ages[j])
    {
        numOfAges += 1;
    }
}
return numOfAges;
}


First of all this loop

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ages[pos] = age;

    }
    pos++;

}while(age != -99);

is invalid. You increase pos even if the entered number does not belong to the acceptable range.

It should look the following way

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ages[pos++] = age;

    }

} while( age != -99 && pos < MAXSIZE );

Also in the function you should check whether the element was already counted. I would write it the following way

int findNumOfAges( const int ages[], int pos, int i)
{
   int numOfAges = 0;

   int j = 0;
   while ( j < i && ages[i] != ages[j] ) j++;

   if ( j == i )
   {
       for( ; j < pos; j++ )
       {
          numOfAges += ages[i] == ages[j];
       }
   }

   return numOfAges;
}

and in main you have to check the returned value. If it is equal to 0 then you have to skip this element because it was already counted.

Also you could write the function based on standard algorithms. For example

#include <algorithm>
//,,,

int findNumOfAges( const int ages[], int pos, int i)
{
   const int *first = std::find( ages, ages + i, ages[i] );

   if ( first != ages + i ) return 0;

   return ( std::count( first, ages + pos, ages[i] ) );
}

The other way is to use std::map

In this case you build simply the map from the array

#include <map>

//...

std::map<int, int> m;

for ( int i = 0; i < pos; i++ ) ++m[ages[i]];

for ( const auto &p : m )
{
   std::cout << p.first << '\t' << p.second << std::endl;
}

Also you could use std::map from the very beginning instead of the array.:)

For example

#include <map>

//...

std::map<int, int> m;

do
{
    cout << "Please input an age from one to 100, put -99 to stop." << endl;
    cin >> age;

    if (age >=0 && age <=100)
    {
        ++m[age];

    }
} while( age != -99 );