int ReadScores (ifstream& fin, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES], int judgeNumber[MAXMEMBERS][MAXJUDGES]) { int i= 0; int j= 0; int k= 0; //index judge count fin >> category; while (fin >> pianoPlayer[i]) { fin >> profLevel[i]; fin >> weightFactor[i]; fin >> judgeNumber[i][j]; while (judgeNumber[i][j] != -1) { fin >> score[i][j]; fin >> judgeNumber[i][j]; j++; } judgeCount[i] = j; i++; j = 0; } return i; } void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES], int numPlayers) { for (i = 0; i < MAXMEMBERS; i++) { fout << "Piano Player: "; fout << pianoPlayer[i]; for (j =0; j < MAXJUDGES; j++) { fout << "Judge Number: " << judgeNumber[i][j] << " "; fout << "Score: " << score[i][j] << "\n"; } } return; }
PrintReport method?
int ReadScores (ifstream& fin) { int i; for (i = 0; fin && i < MAXMEMBERS; ++i) { if (!(fin >> pianoPlayer[i])) break; if (!(fin >> profLevel[i])) break; if (!(fin >> weightFactor[i])) break; // Process the judges for (int j = 0; fin; ++j) { if (j >= MAXJUDGES) { // If the number of judges overflow the array size, // we have to clear off the remaining judge entries // so that we can continue reading from the next // player entry int k; if (!(fin >> k) || k == -1) break; else continue; } if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1) break; if (!(fin >> score[i][j])) break; } } return i; }
ReadScores and PrintReport methods above the main method so that you do not need to pre-declare them. I do not know how to put the while loop in there for sentinal control.
Is there a way to just disply the filled parts or do i have to use the while loop.
Also I am supposed to be calling by reference only. What does this mean?
How do I add up the scores to avereage them? I have been fiddling and I keep on using the wrong values?
sid is the array index of the student you want to average the score for.
int numJudges = 0; double totalScore = 0; double average = 0; for (; numJudges < MAXJUDGES && judgeNumber[sid][numJudges] != -1; ++numJudges) totalScore += score[sid][numJudges]; if (numJudges != 0) average = totalScore / numJudges;
MAXMEMBERS and MAXJUDGES limits, it'd be recommended to use a vector. But since you can't, you have to use malloc, realloc or new, delete; and pointers to manage and increase your memory use for these data. basically I need the loop to stop when it hits -1. Rather than read the judges till the MAXJUDGES has been reached.
int ReadScores (ifstream& fin)
{
int i;
for (i = 0; fin && i < MAXMEMBERS; ++i)
{
if (!(fin >> pianoPlayer[i]))
break;
if (!(fin >> profLevel[i]))
break;
if (!(fin >> weightFactor[i]))
break;
// Process the judges
for (int j = 0; fin; ++j)
{
if (j >= MAXJUDGES)
{
// If the number of judges overflow the array size,
// we have to clear off the remaining judge entries
// so that we can continue reading from the next
// player entry
int k;
if (!(fin >> k) || k == -1)
break;
else
continue;
}
if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
break;
if (!(fin >> score[i][j]))
break;
}
}
return i;
}she says call by reference not by value. you add a + to a variable in the function or something.
ReadScores method above uses ifstream& fin as its parameter; the & tells the compiler that the parameter is passed in by reference and not by value.See I need to setup an array that stores how many jdeges per student an I assume that controls how many it prints to screen. A judge counter array if you will. so the student with 5 judges and scores print 5 and ones with 4 print 4 rather than 8 every time.
-1 in the judgeNumber[sid][n], or hit MAXJUDGES, whichever comes first. The underlined portion in this excerpt of my previous post checks if we reached -1 yet:int numJudges = 0;
double totalScore = 0;
double average = 0;
for (; numJudges < MAXJUDGES && judgeNumber[sid][numJudges] != -1; ++numJudges)
totalScore += score[sid][numJudges];
if (numJudges != 0)
average = totalScore / numJudges;
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int MAXMEMBERS = 20; const int MAXJUDGES = 8; int ReadScores(ifstream& fin); void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES], int numPlayers); int ProcessScores(ifstream& fin); int pianoPlayer[MAXMEMBERS]; double score[MAXMEMBERS][MAXJUDGES]; double weightFactor[MAXMEMBERS]; int profLevel[MAXMEMBERS]; int judgeNumber[MAXMEMBERS][MAXJUDGES]; int category; //category of music the students are playing int judgeCount[MAXMEMBERS]; int numPlayers; int i= 0; int j= 0; int main() { ifstream fin; ofstream fout; fin.open("PianoREV.data"); if (fin.fail()) { cout << "Error: Input File"; exit (1); } fout.open("Report.out"); if (fout.fail()) { cout << "Error: Output File"; exit (1); } ReadScores (fin); ProcessScores (fin); numPlayers = ReadScores (fin); PrintReport(fout, pianoPlayer, score, numPlayers); fin.close(); fout.close(); } int ReadScores (ifstream& fin) { int i; fin >> category; for (i = 0; fin && i < MAXMEMBERS; ++i) { if (!(fin >> pianoPlayer[i])) break; if (!(fin >> profLevel[i])) break; if (!(fin >> weightFactor[i])) break; // Process the judges for (int j = 0; fin; ++j) { if (j >= MAXJUDGES) { // If the number of judges overflow the array size, // we have to clear off the remaining judge entries // so that we can continue reading from the next // player entry int k; if (!(fin >> k) || k == -1) break; else continue; } if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1) break; if (!(fin >> score[i][j])) break; } } return i; } int ProcessScores(ifstream& fin) { for (i = 0; i < MAXMEMBERS; i++) { averageRaw[i][j]= score[i][j] + sum; void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES], int numPlayers) { for (i = 0; i < MAXMEMBERS; i++) { fout << "Piano Player: "; fout << pianoPlayer[i] << "\n"; for (j =0; j < MAXJUDGES; j++) { fout << "Judge Number: " << judgeNumber[i][j] << " "; fout << "Score: " << score[i][j] << "\n"; } } return; }
void PrintReport(ofstream& fout)
{
for (i = 0; i < MAXMEMBERS; i++)
{
fout << "Piano Player: ";
fout << pianoPlayer[i] << "\n";
for (j =0; j < MAXJUDGES && judgeNumber[i][j] != -1; j++)
{
fout << "Judge Number: " << judgeNumber[i][j] << " ";
fout << "Score: " << score[i][j] << "\n";
}
}
return;
}numPlayers to the condition of the outer loop, as underlined:void PrintReport(ofstream& fout)
{
for (i = 0; i < MAXMEMBERS && i <numPlayers; i++)
{
fout << "Piano Player: ";
fout << pianoPlayer[i] << "\n";
for (j =0; j < MAXJUDGES && judgeNumber[i][j] != -1; j++)
{
fout << "Judge Number: " << judgeNumber[i][j] << " ";
fout << "Score: " << score[i][j] << "\n";
}
}
return;
}numPlayers condition alone should suffice, being that numPlayers will not exceed MAXMEMBERS (underlined):void PrintReport(ofstream& fout)
{
for (i = 0; i <numPlayers; i++)
{
fout << "Piano Player: ";
fout << pianoPlayer[i] << "\n";
for (j =0; j < MAXJUDGES && judgeNumber[i][j] != -1; j++)
{
fout << "Judge Number: " << judgeNumber[i][j] << " ";
fout << "Score: " << score[i][j] << "\n";
}
}
return;
}void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES], int numPlayers)#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int MAXMEMBERS = 20; const int MAXJUDGES = 8; int ReadScores(ifstream& fin); void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES], int numPlayers); int ProcessScores(ifstream& fin); int pianoPlayer[MAXMEMBERS]; double score[MAXMEMBERS][MAXJUDGES]; double weightFactor[MAXMEMBERS]; int profLevel[MAXMEMBERS]; int judgeNumber[MAXMEMBERS][MAXJUDGES]; int category; //category of music the students are playing int judgeCount[MAXMEMBERS]; int numPlayers; int i= 0; int j= 0; int main() { ifstream fin; ofstream fout; fin.open("PianoREV.data"); if (fin.fail()) { cout << "Error: Input File"; exit (1); } ...
ProcessScores method is deformed. What's with that?
int ProcessScores(ifstream& fin) { int numJudges; double weightedScore[i][j]; double average[i]; for (numJudges = 0; numJudges < MAXJUDGES && judgeNumber[i][j] != -1; ++numJudges) { totalScore[i][j] += score[i][j]; } if (numJudges != 0) { average[i] = totalScore[i][j] / numJudges; i++; } return i; }
int average[MAXMEMBERS]; int weightedScore[MAXMEMBERS];
void ProcessScores () { int numJudges; double totalScore; for (int i = 0; i < numPlayers; ++i) { for (numJudges = 0, totalScore = 0; numJudges < MAXJUDGES && judgeNumber[i][j] != -1; ++numJudges) { totalScore += score[i][numJudges]; } if (numJudges != 0) average[i] = totalScore / numJudges; weightedScore[i] = average[i] * weightFactor[i]; } }
void since it doesn't make sense to return anything. Also, I removed the ifstream& parameter because it is not necessary either.int i and int j globally, because usually those are used specifically in methods for loop counts, and it would be bad if you forget to reset them to zero for a certain loop.| DaniWeb Message | |
| Cancel Changes | |