I am attempting to write a program that takes input for an array from a file, then counts the number of times each element is repeated than outputs a single instance of each element and how many times it appears in ascending order. Vectors cannot be used.
This is my input file:
inScores.txt:
Code:
55
80
78
92
95
55
78
53
92
65
78
95
85
92
85
95
95
And this is what the output should look like:
Code:
Element No. of Instances
53 1
55 2
65 1
78 3
80 1
85 2
92 3
95 4
Press any key to continue . . .
I have tried 2 methods of coding this which have given me 2 separate results, neither of which are correct.
My first go around looked like this:
c++ Code:
Original
- c++ Code |
|
|
//Include Header Files
#include <iostream>
#include <fstream>
//Declare Namespace
using namespace std;
void insSort2(int *list, int length);
int main()
{
//Declare Variables
ifstream inFile;
ofstream outFile;
int count = 1;
int scores[100];
int last = scores[0];
int x = 0;
//Open input/output files
inFile.open("inScores.txt");
outFile.open("outScores.dat", ios::app);
for(;!inFile.eof();x++)
{
inFile >> scores[x];
}
insSort2(scores, x);
outFile << "Element No. of Instances" << endl;
for(int z=1; z < x;z++)
{
if(scores[z] == last)
count++;
else
{
outFile << " " << scores[z] << " " << count << endl;
last = scores[z];
count = 1;
}
}
system ("PAUSE");
return 0;
}
void insSort2(int *list, int length)
{
for(int x = 1; x<length;x++)
{
for(int y = x-1; y>=0 && list[y+1]<list[y];y--)
{
register int temp = list[y+1];
list[y+1] = list[y];
list[y] = temp;
}
}
}
The output for that looked like this:
Code:
Element No. of Instances
55 1
65 2
78 1
80 3
85 1
92 2
95 3
Press any key to continue . . .
Notice that not only are all the count numbers off but the single instance of 53 isn't even listed.
At first the only thing I saw that was wrong with the output was the fact that 53 was missing and the count for 95 was 3 instead of 4.
So I rewrote my code as thus:
c++ Code:
Original
- c++ Code |
|
|
//Include Header Files
#include <iostream>
#include <fstream>
//Declare Namespace
using namespace std;
void insSort2(int *list, int length);
int main()
{
//Declare Variables
ifstream inFile;
ofstream outFile;
int count = 1;
int scores[100];
int last = scores[0];
int x = 0;
//Open input/output files
inFile.open("inScores.txt");
outFile.open("outScores.dat", ios::app);
for(;!inFile.eof();x++)
{
inFile >> scores[x];
}
insSort2(scores, x);
outFile << "Element No. of Instances" << endl;
for(int z=0; z < x;z++)
{
if(scores[z] == last)
count++;
else if (scores[z] != last)
{
if (scores[z] == scores[x - 1])
{
count++;
}
outFile << " " << scores[z] << " " << count << endl;
last = scores[z];
count = 1;
}
}
system ("PAUSE");
return 0;
}
The output became:
Code:
Element No. of Instances
53 1
55 1
65 2
78 1
80 3
85 1
92 2
95 4
As you can see, 53 is now present and the count for 95 is now correct, but everything else is still wrong. What am I doing wrong here?
I solved the problem, for those who are curious the correct code is:
c++ Code:
Original
- c++ Code |
|
|
//Include Header Files
#include <iostream>
#include <fstream>
//Declare Namespace
using namespace std;
void insSort2(int *list, int length);
int main()
{
//Declare Variables
ifstream inFile;
ofstream outFile;
int count = 1;
int scores[100];
int x = 0;
//Open input/output files
inFile.open("inScores.txt");
outFile.open("outScores.dat");
for(;!inFile.eof();x++)
{
inFile >> scores[x];
}
insSort2(scores, x);
int last = scores[0];
outFile << "Element No. of Instances" << endl;
int z;
for(z=1; z < x;z++)
{
//cout << scores[z] << endl;
if(scores[z] == last)
count++;
else
{
outFile << " " << scores[z-1] << " " << count << endl;
last = scores[z];
count = 1;
}
}
outFile << " " << scores[z-1] << " " << count << endl;
return 0;
}
void insSort2(int *list, int length)
{
for(int x = 1; x<length;x++)
{
for(int y = x-1; y>=0 && list[y+1]<list[y];y--)
{
int temp = list[y+1];
list[y+1] = list[y];
list[y] = temp;
}
}
}