| | |
help with parrallel arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
I have setup the arrays sucessfully I think. At least the score and the player ids. For some reason it is pulling in the negative one before exiting though. Is there a way to avoid this? Here it is
Basically after the completion of one cycle the player id becomes the negative one and the all the data becomes off by one each repition. There is something wrong with my loops it is not stopping after the players are read I do not think, but it is not trapped in an infinite loop. I do not know what to do. I think I finnally am getting some grasp on this
C++ Syntax (Toggle Plain Text)
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; }
Basically after the completion of one cycle the player id becomes the negative one and the all the data becomes off by one each repition. There is something wrong with my loops it is not stopping after the players are read I do not think, but it is not trapped in an infinite loop. I do not know what to do. I think I finnally am getting some grasp on this
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
It's not printing anything because you did not call the
Your reading method seems to be flawed. Try this method (it's a modified version what I wrote earlier):
Hope this helps.
Oh yes, it would be good to place your
PrintReport method?Your reading method seems to be flawed. Try this method (it's a modified version what I wrote earlier):
C++ Syntax (Toggle Plain Text)
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; }
Oh yes, it would be good to place your
ReadScores and PrintReport methods above the main method so that you do not need to pre-declare them. Best Regards, God Bless,
AstroNox
AstroNox
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
that rocks. Thank you. I am still running into the problem of it reading 8 judges for every player. It is not exiting at the -1. I see that it is reading until MAXJUDGES has been read. I do not know how to put the while loop in there for sentinal control. Also it is reading 25 players rather than just reading until the last player is processed. How can i get around this. 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?
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?
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
Sorry, but I do not fully understand what you have posted (no, I'm not pointing out the spelling mistakes):
What do you mean by "sentinal control"?
What do you mean by "disply the filled parts"? What filled parts do you want to display?
Honestly I would not know unless you give me the context in which your lecturer placed that statement.
This is pretty simple. Assume that
If you would like to get past the
•
•
•
•
Originally Posted by lsu420luv
I do not know how to put the while loop in there for sentinal control.
•
•
•
•
Originally Posted by lsu420luv
Is there a way to just disply the filled parts or do i have to use the while loop.
•
•
•
•
Originally Posted by lsu420luv
Also I am supposed to be calling by reference only. What does this mean?
•
•
•
•
Originally Posted by lsu420luv
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. C++ Syntax (Toggle Plain Text)
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;
If you would like to get past the
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. Best Regards, God Bless,
AstroNox
AstroNox
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
basically I need the loop to stop when it hits -1. Rather than read the judges till the MAXJUDGES has been reached. she says call by reference not by value. you add a + to a variable in the function or something. 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.
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
•
•
•
•
Originally Posted by lsu420luv
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;
}•
•
•
•
Originally Posted by lsu420luv
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.•
•
•
•
Originally Posted by lsu420luv
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; Best Regards, God Bless,
AstroNox
AstroNox
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
It is still posting all eight judges regardless of the -1. Here is the code I am using.
Here is the Report.out
Piano Player: 6010
Judge Number: 23 Score: 7
Judge Number: 25 Score: 8.5
Judge Number: 34 Score: 7
Judge Number: 12 Score: 7.5
Judge Number: -1 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Piano Player: 6012
Judge Number: 23 Score: 7.5
Judge Number: 34 Score: 7
Judge Number: 45 Score: 7
Judge Number: 50 Score: 7.5
Judge Number: -1 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Is the problem in how i am outputting the results?
C++ Syntax (Toggle Plain Text)
#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; }
Here is the Report.out
Piano Player: 6010
Judge Number: 23 Score: 7
Judge Number: 25 Score: 8.5
Judge Number: 34 Score: 7
Judge Number: 12 Score: 7.5
Judge Number: -1 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Piano Player: 6012
Judge Number: 23 Score: 7.5
Judge Number: 34 Score: 7
Judge Number: 45 Score: 7
Judge Number: 50 Score: 7.5
Judge Number: -1 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Judge Number: 0 Score: 0
Is the problem in how i am outputting the results?
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
You need to have the underlined portion in your condition, as mentioned in my previous post:
You might also consider adding
In fact, the
I noticed that you are placing redundant variables (underlined) in your method declarations, which I cleared off in ReadScores, and now here in PrintReport:
There is no need to do that because your arrays and numPlayers variables are all outside of any method declaration. This makes it globally available, which means any method in that .cpp file can access these variables (underlined):
By the way, looking at the source code you gave, that shouldn't compile at all. Your
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? Best Regards, God Bless,
AstroNox
AstroNox
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
I copied it while in the middle of doing something. Kick ass I have prevented it from printing judges that dont exist. I am having problems with the average score thing. I need to average the score and then multiply it by the weight factor. From your post it doesnt seem like an aarray takes care of this. a 2-d array is supposed to handle the judges scores and weighted scores. How do i set that up. I did it like you pointed out and it kept on just shooting up 0. Also I am still reading beyong the number of players, instead i am reading until the max number I think. You are saving my ass by the way. Know I appreciate all this help. Here is what i did for the average score:
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
You might want to add two more global array variables first, assuming you need the unweighted and weighted:
Then you can calculate for each value with three loops. I don't know but you seemed to have thrown things together without much thought or understanding.
Assuming you want to calculated the averages for each student:
I've changed your return type to
Oh yes, you should not declare
Hope this helps, and I'm glad that I was able to provide you with assistance. If you truly like what I've done for you, I'd appreciate a small donation of any amount (via PayPal); PM me if you are willing and I'll give you my e-mail. Thanks!
C++ Syntax (Toggle Plain Text)
int average[MAXMEMBERS]; int weightedScore[MAXMEMBERS];
Assuming you want to calculated the averages for each student:
C++ Syntax (Toggle Plain Text)
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.Oh yes, you should not declare
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.Hope this helps, and I'm glad that I was able to provide you with assistance. If you truly like what I've done for you, I'd appreciate a small donation of any amount (via PayPal); PM me if you are willing and I'll give you my e-mail. Thanks!
Best Regards, God Bless,
AstroNox
AstroNox
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need help with Array Validation
- Next Thread: Stacks - balanced parentheses
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





