Okay, I was given this problem, its another of those file I/O streams and I was wondering how I can change this code to be more "efficient" and change a lot of things. I'm having trouble on what I should change and what I should keep. Please help me out on this one. Thank you! Oh, this code does work by the way. Please nothing too fancy! I would just like to change a lot of things to make it better.
Write a program that reads in a set of positive integers, representing test
scores for a class, and outputs how many times a particular number appears in the list.
You may assume that the data set has, at most, 100 numbers and that -999 marks the end
of the input data. The numbers must be output in increasing order. For example, for the
data:
55 80 78 92 95 55 78 53 92 65 78 95 85 92 85 95 95
the output is:
Test Score Count
53 1
55 2
65 1
78 3
80 1
85 2
92 3
95 4
input2.txt has this numbers: 55 80 78 92 95 55 78 53 92 65 78 95 85 92 85 95 95
#include <iostream>//include statement(s)
#include <iomanip>
#include <fstream>
using namespace std;//using namespace statement(s)
const int MAX_SIZE = 999;//constant variable declaration(s)
void getScoresAndSize(int arry[], int& arrySize);//void function header(s)
void displayScoreArray(int arry[], int arrySize);
void orderArray(int arry[], int arry2[], int arrySize);
void outputData(int arry[], int arrySize);
int main()
{
int scoreArray[MAX_SIZE];//variable declaration(s)
int newScoreArray[MAX_SIZE];
int ArraySize;
getScoresAndSize(scoreArray, ArraySize);//void function call(s)
displayScoreArray(scoreArray, ArraySize);
orderArray(scoreArray, newScoreArray, ArraySize);
cout << "After reordering the array, ";
displayScoreArray(newScoreArray, ArraySize);
outputData(newScoreArray, ArraySize);
cout << "Program is exiting..." << endl;
system ("PAUSE");//Black box to appear and stay
return 0;//return statement(s)
}
void getScoresAndSize(int arry[], int& arrySize)
{
ifstream scorFil;
scorFil.open("input2.txt");
if (scorFil.fail())//if score file fails, program outputs "cannot open file"
{
cout << "The input2.txt file was not opened by the program. Ending program..." << endl;
exit(1);
}
else if (!scorFil.fail())//if score file opens, program outputs "file was opened successfully"
cout << "The input2.txt file was succesfully opened by the program." << endl;
int score;
arrySize = 1;
for(int i = 0; !scorFil.eof(); i++ && arrySize++)//for loop for the readable file, collect the scores
{ //the array size is incremented as well
scorFil >> score;
arry[i] = score;
}
cout << "The scores from the file were succesfully transferred into an array."<<endl;
cout << "The array contains " << arrySize << " scores." << endl << endl;
scorFil.close();
return;
}
void displayScoreArray(int arry[], int arrySize)
{
cout << "The Scores are: " << endl;
for (int i = 0; i <= (arrySize - 1); i++)//for loop to print out each score
{
cout << arry[i] << " ";//spaces between each score
if ((i + 1) % 21 == 0)
cout << endl;
}
cout << endl;
cout << endl;
return;
}
void orderArray(int arry[], int arry2[], int arrySize)
{
int index = 0;//new array for index
for (int score = 0; score <= 100; score++)//for loop to check the scores from 0 to 100
{
for (int i = 0; i <= (arrySize - 1); i++)
{
if(arry[i] == score)
{
arry2[index] = score;
index++;//index increments
}
}
}
}
void outputData(int arry[], int arrySize)
{
int oldNum = arry[0];
int count = 1;
cout << "Score" << setw(11) << "Count" << endl;//output header
for(int i = 1; i <= arrySize; i++)
{
if(arry[i] == oldNum)
{
count++;//counter increments
}
else
{
cout << " " << oldNum << setw(11) << count << endl;
count = 1;
}
oldNum = arry[i];
}
cout << endl;
}
Thanks for the help!