Reputation: 187
So I am supposed to take 1000000 numbers from an input.txt file and store them into one array of terms of size 10000 and another array of the frequency of each term of size 10000. For right now, I am just trying to sort a series of 7 numbers:
0
1
2
0
3
2
0
And I am supposed to get the output:
0 3
2 2
1 1
3 1
But instead. I am getting
0 3
1 2
1 1
3 1
For some reason it isn't placing the two in the spot it needs to. I assume a problem in the Insert()
function. But I can't find it.
Also, as a side note. I tried to run the program with the 1,000,000 numbers but it just said SearchAndSort.exe has failed and needs to close.
Please help me out! I bet there's just some small syntax error somewhere. Here is my main.cpp file
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "TermTable.h"
using namespace std;
int main()
{
TermTable termTableObj;
ifstream fin("input.txt");
if (fin.is_open())
{
cout << "Open" << endl;
int num;
while (fin >> num)
{
termTableObj.Insert(num);
}
termTableObj.Sort();
termTableObj.Display();
}
return 0;
}
And here is my TermTable.cpp implementation file.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "TermTable.h"
using namespace std;
int TermTable::BinarySearch(int searchValue)
{
int first, last, middle, position;
bool found;
first = 0;
last = currentAmount - 1;
found = false;
position = -1;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (termArray[middle] == searchValue)
{
found = true;
position = middle;
}
else if (termArray[middle] > searchValue)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
void TermTable::Sort()
{
int temp;
bool swapOccurred;
do {
swapOccurred = false;
for (int count = 0; count < (currentAmount - 1); count++)
{
if (frequencyArray[count] < frequencyArray[count + 1])
{
temp = frequencyArray[count];
frequencyArray[count] = frequencyArray[count + 1];
frequencyArray[count + 1] = temp;
temp = termArray[count];
termArray[count] = frequencyArray[count + 1];
termArray[count + 1] = temp;
swapOccurred = true;
}
}
} while (swapOccurred);
}
void TermTable::Insert(int value)
{
int position = BinarySearch(value);
if (position != -1)
{
frequencyArray[position] += 1;
}
else
{
int temp;
termArray[currentAmount] = value;
frequencyArray[currentAmount] = 1;
currentAmount++;
for (int i = currentAmount + 1; i >= 0; i--)
{
if (termArray[i] < termArray[i - 1])
{
temp = termArray[i];
termArray[i] = termArray[i - 1];
termArray[i - 1] = temp;
temp = frequencyArray[i];
frequencyArray[i] = frequencyArray[i - 1];
frequencyArray[i - 1] = temp;
}
else
{
break;
}
}
}
}
void TermTable::Display()
{
ofstream fout("output.txt");
for (int j = 0; j < 10000; j++)
{
fout << termArray[j] << " " << frequencyArray[j] << endl;
}
}
And here is my class definition:
#include <cstdlib>
#include <iostream>
using namespace std;
// class specification
class TermTable
{
public:
// constructor
TermTable()
{
currentAmount = 0;
for (int i = 0; i < 10000; i++)
{
termArray[i] = 0;
}
for (int i = 0; i < 10000; i++)
{
frequencyArray[i] = 0;
}
};
// member functions
int BinarySearch(int searchValue);
void Insert(int value);
void Sort();
void Display();
// destructor
~TermTable()
{
return;
}
private:
// data
int currentAmount;
int termArray[10000];
int frequencyArray[10000];
};
Upvotes: 0
Views: 159
Reputation: 392833
It looks like you just want a standard histogram of input values, and list them in order of decreasing frequency.
So let's do the usual insertion into a map (by the value as key) and sort the pairs by frequency decending, afterwards.
#include <iostream> // cin/cout
#include <algorithm> // for_each
#include <iterator> // istream_iterator
#include <map>
#include <vector>
#include <functional> // mem_fn
using namespace std;
using Histo = map<int, size_t>;
int main() {
Histo histo;
std::for_each(std::istream_iterator<int>(std::cin), {}, [&](int i) { histo[i]++; });
vector<pair<int, size_t>> v(begin(histo), end(histo));
sort(begin(v), end(v), [](Histo::value_type a, Histo::value_type b) { return a.second > b.second; });
for(auto& p : v)
std::cout << p.first << " " << p.second << "\n";
}
Prints
0 3
2 2
1 1
3 1
Upvotes: 2