Sorting Function not Correct

Hi, For my final project I'm writing a program that reads all the powerball numbers from 02/03/2010 up until now and then sorts them to determine the 5 most frequently drawn numbers and the 5 most frequently drawn powerball numbers (and the least frequent of each). I am having the steps cout to the console for now so that I can see they're working. I will eventually write it to a file when finished.

My problem is that the sorting function is working, but it's not correct. I don't know if I'm not passing the right variables/functions or if I'm just doing it wrong. The prototypes, definitions and calls are all separate. Let me know if you need more info, I'm just attaching the read/count/sort functions. Can anyone give me some pointers as to what is wrong/why it's not working?
Thank you!

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//FUNCTIONS.CPP
//I have these included for now just in case I need them before I'm done.
#include <iostream>
#include <string> 
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include "Functions.h"

using namespace std;

#define FILE_IN  "PowerballNow.csv"
//#define FILE_OUT "will be determined by the user"

bool ReadPowerballNumbers(vector<Powerball> &vNumbers)

{
	//OPEN DATA FILE "Powerball.csv"
	string trash;
	ifstream openFile;
	Powerball numbers;
	char comma;
	int multiplier;

	openFile.open(FILE_IN);
	if (openFile.is_open())
	{
		//THROWING OUT THE FIRST LINE OF TEXT
		getline(openFile, trash, ',');
		getline(openFile, trash, ',');
		getline(openFile, trash);

		//LOAD DATA INTO OBJECT "numbers"
		for (int i = 0; i < 648; i++)
		{
			getline(openFile, trash, ',');//ELIMINATING 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;  // ELIMINATING THE COMMA
			openFile >> multiplier;  // ELIMINATING THE MULTIPLIER

		        //PUSHING "NUMBERS" INTO VECTOR NAMED vNumbers
			vNumbers.push_back(numbers);

			//COUT ALL NUMBERS TO PROVE FILE WAS READ
			cout << "\t" << setw(2) << numbers.number[0] << "  " << setw(2)
			<< numbers.number[1] << "  " << setw(2) << numbers.number[2] << "  "
			<< setw(2) << numbers.number[3] << "  " << setw(2) << numbers.number[4]
			<< "   " << setw(2) << numbers.powerball << endl;
		}
		//CLOSE FILE AFTER READING
		openFile.close();
		return true;
	}
	else
	{
		cout << "\n\t\tError! Could not open file." << endl;
		return false;
	}

}

void CountNumbers(vector<Powerball> &vNumbers, int numCount[], int pbCount[], int &num1, int &pbnum)
{
  for (unsigned int i = 0; i < vNumbers.size(); i++)
  {
		for (int j = 0; j < 5; j++)
		{
		num1 = vNumbers[i].number[j];
		numCount[num1]++;
		}
		pbnum = vNumbers[i].powerball;
		pbCount[pbnum]++;
	}

	cout << "\n\n\tNumber Count" << endl;
	for (int i = 1; i <= 69; i++)
	{
		cout << "\t" << setw(3) << i << "\t" << numCount[i] << endl;
	}
	cout << "\n\n\tPowerball Count" << endl;
	for (int i = 1; i <= 26; i++)
	{
		cout << "\t" << setw(3) << i << "\t" << pbCount[i] << endl;
	}

}

void SortNumberCounts(int numCount[], int pbCount[], int &num1, int &pbnum)
{
	int i = 0, j = 0, temp;
	for (i = 1; i < num1 - 1; ++i)
	{
		for (j = 1; j < num1; ++j)
		{
			if (numCount[j - 1] > numCount[j])
			{
				temp = numCount[j];
				numCount[j] = numCount[j - 1];
				numCount[j - 1] = temp;
			}
		}
	}
	cout << "\n\n\t The Five Most Frequently Drawn Numbers Are: \n\n";
	for (int i = 1; i < 6; ++i)
	{
		cout << "\tNumber[" << i << "]" << numCount[i] << endl;
	}
	cout << "\n\n\t The Five Most Infrequently Drawn Numbers Are: \n\n";
	for (int i = num1 - 6; i < num1; ++i)
	{
		cout << "\tNumber[" << i << "]" << numCount[i] << endl;
	}
}

/*   THIS PART WON'T WORK AT ALL: WHEN I RUN IT WITH THIS IT SAYS BUILD ERRORS, BUT THERE ARE NO ERRORS LISTED.
	
	for (i = 1; i < pbnum - 1; ++i)
	{
		for (j = 1; j < pbnum; ++j)
		{
			if (pbCount[j - 1] > pbCount[j])
			{
				temp = pbCount[j];
				pbCount[j] = pbCount[j - 1];
				pbCount[j - 1] = temp;
			}
		}
	}
	cout << "\n\n\t The Five Most Frequently Drawn Powerball Numbers Are: \n";
	for (int i = 1; i < 6; ++i)
	{
		cout << "Number[" << i << "]" << pbCount[i] << endl;
	}

	cout << "\n\n\t The Five Most Infrequently Drawn Powerball Numbers Are: \n";
	for (int i = pbnum - 6; i < pbnum; ++i)
	{
		cout << "Number[" << i << "]" << pbCount[i] << endl;
	}
	
} */
Last edited on
How are the numbers stored in the file? Can you give an example?

Also, you don't need to sort a container to find its biggest elements. You can just iterate through and find the index of the most frequent number.


The data file starts like this (Is that what you're asking?):
Draw Date Winning Numbers Multiplier
2/3/2010 17 22 36 37 52 24 2
2/6/2010 14 22 52 54 59 04 3
2/10/2010 05 08 29 37 38 34 5
2/13/2010 10 14 30 40 51 01 4
2/17/2010 07 08 19 26 36 15 3
2/20/2010 13 27 37 41 54 32 2 (etc...)

I read the data file into an object and pushed it into a vector of structs:
1
2
3
4
5
struct Powerball
{
	int number[5];
	int powerball;
};



If there is an easier way to do it, please tell me how. I am a total beginner.
The multiplayer specifies the number of each powerball number found? If yes, you can do it in the following way:

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
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<string>

using namespace std;

struct powerball{
int powerball_code[6];
int multiplier;
};

bool compare (const powerball a, const powerball b){  //sort by multiplier
if (a.multiplier>b.multiplier){
    return true;
}
else{
    return false;
}
}

int main(){
//Read input
powerball powerballs[10000];

//Sort
sort (powerballs, powerballs+place+1, compare);
for (int i=0; i<6; i++){
    cout<<powerballs[place].powerball_code[i]<<" ";
}
return 0;
}
Btw, I think your program crushes because you'd need a class with a constructor in order to use a powerball vector.
The multiplier, in the data, represents another number in the powerball game (that you can pay an extra $1 for if you choose, and if you win, your winnings are multiplied by the multiplier drawn for that drawing (unless of course you win the jackpot). For my program purposes, I am not using the multiplier. I am only using the five numbers drawn, and the powerball number. my code, where it reads in the file, throws out the headings, the date and the multiplier. Does that change anything you wrote above?
Yes, it does change it. My code above sorts everything by the multiplayer. Maybe you don't need a struct at all. Just use strings for each input in a map. Then sort the map by the mapped value. Have you used maps before?
I have not learned about maps yet. The code I have right now displays to the console screen, all the numbers in the data file (that I didn't trash), then it shows the counts of all the first 5 numbers, and the counts of the powerball numbers. The counts are right, except for the first 11 numbers of the "first five numbers" category are wrong. All the rest of the counts for the rest of the first 5 numbers and all the powerball counts are correct. I can't figure out why that is. Then, I have it showing the five most frequently drawn and five most infrequently drawn numbers in each category, but they're not all correct either. Plus many of them are repeats, so I don't know what's wrong with my code.
Topic archived. No new replies allowed.