Home > database >  Is there a way to count the number of ocurrences of each element of a string array?
Is there a way to count the number of ocurrences of each element of a string array?

Time:11-05

I have the following code that does exactly what I want. The problem is that I need the sample array to compare the strings and keep the count. Is there a way to count the number of occurrences of each string on any array without a sample?

For a little bit more context, the initial problem was to read data from a .txt file including vehicles information, like:

Volkswagen Jetta 
Ford Focus 
Volkswagen Jetta

And count the number of vehicles of each brand. Keep in mind that this is from an introductory course for programming, and we don't know how to use vectors or maps.

#include <iostream>
#include <string>
using namespace std;
using std::string;
#define MAX 20

int main(){
    int counter[MAX];
    string arr[MAX]={"ABC","AOE","ADC","ABC","ADC","ADC"};
    string sample[MAX]={"ABC", "AOE", "ADC"};
    for(int i=0; i<=MAX; i  ){
        counter[i]=0;
    }
    for(int i=0; i<MAX;i  ){
        for(int j=0; j<MAX; j  ){
            if (sample[i]==arr[j]){
                counter[i]  ;
            }
        }
    }

    for(int i=0; i<3;i  ){
        cout<< sample[i] << "=" << counter[i]<<endl;
    }
    return 0;
}

CodePudding user response:

All you are expected to do is keep a list (an array will do) of brand names, and an array of counts for each name:

std::string brand_names[100];
int  counts[100]; // number of times each element of brand_names[] was read from file
int  num_items = 0;

Each time you read a brand name from file, try to find it in the array of strings. If found, just add one to the count at the same index. If not found, add it to the end of the brand_names[] array, add 1 to the end of the counts[] array, and increment num_items.

You do not need anything more than a simple loop for this:

  • an outer loop to read the next brand name from file
  • an inner loop to try to find the brand name in the list

CodePudding user response:

If you want to solve this problem without knowing the initial values of the sample array:

Create an empty sample array. When you see new elements add them to this array.

Use a variable sample_size to keep track how many samples have been seen. Below is a simple example which doesn't use std::vector or dynamic allocation.

int main() 
{
    std::string arr[MAX] = { "ABC","AOE","ADC","ABC","ADC","ADC" };
    std::string sample[MAX];
    int sample_size = 0;
    int counter[MAX] = { 0 };
    for (int i = 0; i < MAX; i  )
    {
        if (arr[i].empty()) break;

        bool sample_found = false;
        for (int j = 0; j < sample_size; j  )
            if (arr[i] == sample[j])
            {
                sample_found = true;
                counter[j]  ;
                break;
            }

        if (!sample_found)
        {
            sample[sample_size] = arr[i];
            counter[sample_size]  ;
            sample_size  ;
        }
    }
    for (int i = 0; i < sample_size; i  )
        cout << sample[i] << "=" << counter[i] << std::endl;
    return 0;
}
  • Related