Counting items in a vector

I'm a beginner, so this is complicated to me.

For a C++ class project I'm writing a program that reads a data file of all the Powerball numbers drawn from 02/2010 up until now (I have that part done). I am basically trying to determine the 5 most frequently called numbers in the main 5 numbers cagetory, and the 1 most frequently called powerball number (and the least of each).

I created a struct to hold my powerball numbers (one, two, three, four, five, powerball) and loaded them into a vector.

Now I am supposed to use an array (or two?), to count how many times each number in the one, two, three, four, and five categories appears in my vector (numbers 1-69), and also to count how many times each number in the powerball category appears in the vector (numbers 1-26).

Can anyone tell me how to create the counters so that the struct elements can be separated in this way? I hope that made sense.
Last edited on
Simple. Create an array of 70 ints. Lets call it count. Initialize the array to 0s. Iterate through your vector, incrementing count[n] for each number in your vector.

Thanks. What about the powerball number? The powerball ranges from 1-26 only, so do I need two arrays? How do I tell the first array to search through only the the first 5 numbers of every line, and the second array to search only the last number of every line?
You weren't clear as to whether you wanted to count occurrences of powerball numbers separately or include them in the overall count. It makes sense to count them separately since including them in the count[] array would skew the results.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <vector>
using namespace std;

struct drawing
{   int number[5]; // An array is easier here
    int pb;
};
  
int main()
{   int count[70] = {0};    //count[0] not used
    int pb_cnt[27] = {0};
    vector<drawing> drawings;
    int num;

    //  Read the file into the vector
    for (unsigned i=0; i<drawings.size(); i++)
    {  for (int j=0; i<5; j++)
        {   num = drawings[i].number[j];
            count[num]++;
        }
        num = drawings[i].pb;
        pb_cnt[num]++;
    }
    return 0;
}




This is starting to make more sense. However, now I have another problem. I don't know how to incorporate the struct the way you have it because there are characters in the data file that I have to get rid of in order to pull in only the numbers. This is how I designed my struct. I read the file into an object, and then pushed them into the vector like this (and I have to use functions).

the data file looks like this (with 647 lines of numbers):
Draw Date,Winning Numbers,Multiplier
4/13/2016,30 33 35 38 64 22,3
4/9/2016,14 22 23 41 61 09,3
4/6/2016,04 28 49 60 65 25,2
4/2/2016,09 28 30 40 61 03,2
3/30/2016,24 44 53 55 63 19,2 ETC....


Struct:

1
2
3
4
5
6
7
8
9
struct Powerball
{
	string one;
	string two;
	string three;
	string four;
	string five;
	string powerball;
}


Here's my function that reads it in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
bool ReadPowerballNumbers(vector<Powerball> vNumbers)

{
	string trash;
	ifstream openFile;						        
	openFile.open(FILE_IN);
	if (openFile.is_open())
	{
		getline(openFile, trash, ',');
		getline(openFile, trash, ',');
		getline(openFile, trash);	
		for (int i = 0; i < 650; i++)			   
		{
			Powerball numbers;						
			
			getline(openFile, trash, ',');			
			getline(openFile, numbers.one, ' ');
			getline(openFile, numbers.two, ' ');
			getline(openFile, numbers.three, ' ');
			getline(openFile, numbers.four, ' ');
			getline(openFile, numbers.five, ' ');
			getline(openFile, numbers.powerball, ',');

			vNumbers.push_back(numbers);			
			
		}
		openFile.close();							
		return true;
	}
	else
	{
		cout << "\n\t\tError! Could not open file." << endl;
		return false;
	}

}
You're going to have a problem defining one,two,three,four,five as strings.
You want those as ints.

Another problem, line 1, you're passing vNumbers by value. Any changes made to vNumbers are made to a copy of the vector. The changes are lost when the function exits. Your want to pass vNumbers by reference.
Last edited on
I tried making the stuct ints instead of strings, but then my getlines starting on line 17 all have the red squiggly lines underneath and say "no instance of overloaded function "getline" matches the argument list argument types are:(std::ifstream, int,char)." I don't know what that means.
no instance of overloaded function "getline" matches the argument list

That means getline only works with strings, not ints. You need to use the >> operator instead.

Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Powerball
{   int number[5];
    int powerball;
};

bool ReadPowerballNumbers (vector<Powerball> & vNumbers)  // note the pass by reference
{   string      trash;
	ifstream    openFile;	
    Powerball   numbers;		
    char        comma;
    int         multiplier;
    						        
	openFile.open ("powerball.txt");
	if (! openFile.is_open())
	{   cout << "\n\t\tError! Could not open file." << endl;
		return false;
	}
	while (getline(openFile, trash, ','))   //  Eat the date
	{	openFile >> numbers.number[0];
		openFile >> numbers.number[1]; 
		openFile >> numbers.number[2]; 
		openFile >> numbers.number[3]; 
		openFile >> numbers.number[4]; 
		openFile >> numbers.powerball;
		openFile >> comma;  // discard
		openFile >> multiplier;  // discard
		vNumbers.push_back(numbers);			
	}
	return true;			
}
 
int main()
{   int count[70] = {0};    //count[0] not used
    int pb_cnt[27] = {0};
    vector<Powerball> vNumbers;
    int num;

    if (! ReadPowerballNumbers(vNumbers))
        return 1;
    for (unsigned i=0; i<vNumbers.size(); i++)
    {  for (int j=0; i<5; j++)
        {   num = vNumbers[i].number[j];
            count[num]++;
        }
        num = vNumbers[i].powerball;
        pb_cnt[num]++;
    }
    //  Display the frequencies from the count and pb_cnt arrays
    return 0;
}

I've not tested this, but it should give you a good start.

Last edited on
I change it up and try it. Thank you SO much for your help!
I tried it and it works! Sorry to be dumb, but how do I "display the frequencies?" I know cout, but cout what? pb_count, or [i], or something else?
1
2
3
4
5
6
7
    cout << "Number  Count" << endl;
    for (int i=1; i<=69; i++)
        cout << setw(8) << i << count[i] << endl;
    cout << endl;        
    cout << "Pball   Count" << endl;        
    for (int i=1; i<=26; i++)
        cout << setw(8) << i << pb_cnt[i] << endl;

Last edited on
I believe I have it working, but it's basically displaying (I think) the number of times the numbers 1-69 and 1-26 occur in the vector (in their corresponding categories). Now I need for it to figure out the 5 most frequently drawn numbers (in the 1-5 category) and the 1 (or 5) most frequently drawn powerball number (and the least of each). That is actually what I will need to display in the end. Do I have to do the "bubble sorter" type thing to put the numbers in order? And if so (or if not), I have no idea how to tell it to display the target numbers.
Topic archived. No new replies allowed.