943,600 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9173
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 26th, 2006
0

Re: help with parrallel arrays

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


C++ Syntax (Toggle Plain Text)
  1. int ReadScores (ifstream& fin, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES],
  2. int judgeNumber[MAXMEMBERS][MAXJUDGES])
  3. {
  4. int i= 0;
  5. int j= 0;
  6. int k= 0; //index judge count
  7. fin >> category;
  8. while (fin >> pianoPlayer[i])
  9. {
  10. fin >> profLevel[i];
  11. fin >> weightFactor[i];
  12. fin >> judgeNumber[i][j];
  13. while (judgeNumber[i][j] != -1)
  14. {
  15. fin >> score[i][j];
  16. fin >> judgeNumber[i][j];
  17. j++;
  18. }
  19. judgeCount[i] = j;
  20. i++;
  21. j = 0;
  22. }
  23. return i;
  24. }
  25. void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES],
  26. int numPlayers)
  27. {
  28. for (i = 0; i < MAXMEMBERS; i++)
  29. {
  30. fout << "Piano Player: ";
  31. fout << pianoPlayer[i];
  32. for (j =0; j < MAXJUDGES; j++)
  33. {
  34. fout << "Judge Number: " << judgeNumber[i][j] << " ";
  35. fout << "Score: " << score[i][j] << "\n";
  36. }
  37. }
  38. return;
  39. }

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
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Mar 26th, 2006
0

Re: help with parrallel arrays

It's not printing anything because you did not call the 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)
  1. int ReadScores (ifstream& fin)
  2. {
  3. int i;
  4. for (i = 0; fin && i < MAXMEMBERS; ++i)
  5. {
  6. if (!(fin >> pianoPlayer[i]))
  7. break;
  8. if (!(fin >> profLevel[i]))
  9. break;
  10. if (!(fin >> weightFactor[i]))
  11. break;
  12.  
  13. // Process the judges
  14. for (int j = 0; fin; ++j)
  15. {
  16. if (j >= MAXJUDGES)
  17. {
  18. // If the number of judges overflow the array size,
  19. // we have to clear off the remaining judge entries
  20. // so that we can continue reading from the next
  21. // player entry
  22. int k;
  23. if (!(fin >> k) || k == -1)
  24. break;
  25. else
  26. continue;
  27. }
  28. if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
  29. break;
  30. if (!(fin >> score[i][j]))
  31. break;
  32. }
  33. }
  34. return i;
  35. }
Hope this helps.

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

Sorry, but I do not fully understand what you have posted (no, I'm not pointing out the spelling mistakes):
Quote originally posted by lsu420luv ...
I do not know how to put the while loop in there for sentinal control.
What do you mean by "sentinal control"?

Quote originally posted by lsu420luv ...
Is there a way to just disply the filled parts or do i have to use the while loop.
What do you mean by "disply the filled parts"? What filled parts do you want to display?

Quote originally posted by lsu420luv ...
Also I am supposed to be calling by reference only. What does this mean?
Honestly I would not know unless you give me the context in which your lecturer placed that statement.

Quote 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?
This is pretty simple. Assume that sid is the array index of the student you want to average the score for.
C++ Syntax (Toggle Plain Text)
  1. int numJudges = 0;
  2. double totalScore = 0;
  3. double average = 0;
  4. for (; numJudges < MAXJUDGES && judgeNumber[sid][numJudges] != -1; ++numJudges)
  5. totalScore += score[sid][numJudges];
  6. if (numJudges != 0)
  7. 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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

Quote 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.
That has already been done in my previous post (look at the underlined portion):
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;
}

Quote originally posted by lsu420luv ...
she says call by reference not by value. you add a + to a variable in the function or something.
Call which method with parameters by reference or value? The 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.

Quote 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.
That's already been done in my previous post too. You do not need another array to store the number of judges; all you need is to read the array until you either hit a -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;
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

It is still posting all eight judges regardless of the -1. Here is the code I am using.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6. const int MAXMEMBERS = 20;
  7. const int MAXJUDGES = 8;
  8. int ReadScores(ifstream& fin);
  9. void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES],
  10. int numPlayers);
  11. int ProcessScores(ifstream& fin);
  12. int pianoPlayer[MAXMEMBERS];
  13. double score[MAXMEMBERS][MAXJUDGES];
  14. double weightFactor[MAXMEMBERS];
  15. int profLevel[MAXMEMBERS];
  16. int judgeNumber[MAXMEMBERS][MAXJUDGES];
  17. int category; //category of music the students are playing
  18. int judgeCount[MAXMEMBERS];
  19. int numPlayers;
  20. int i= 0;
  21. int j= 0;
  22. int main()
  23. {
  24. ifstream fin;
  25. ofstream fout;
  26.  
  27. fin.open("PianoREV.data");
  28. if (fin.fail())
  29. {
  30. cout << "Error: Input File";
  31. exit (1);
  32. }
  33. fout.open("Report.out");
  34. if (fout.fail())
  35. {
  36. cout << "Error: Output File";
  37. exit (1);
  38. }
  39. ReadScores (fin);
  40. ProcessScores (fin);
  41. numPlayers = ReadScores (fin);
  42. PrintReport(fout, pianoPlayer, score, numPlayers);
  43. fin.close();
  44. fout.close();
  45. }
  46.  
  47. int ReadScores (ifstream& fin)
  48. {
  49. int i;
  50. fin >> category;
  51. for (i = 0; fin && i < MAXMEMBERS; ++i)
  52. {
  53. if (!(fin >> pianoPlayer[i]))
  54. break;
  55. if (!(fin >> profLevel[i]))
  56. break;
  57. if (!(fin >> weightFactor[i]))
  58. break;
  59.  
  60. // Process the judges
  61. for (int j = 0; fin; ++j)
  62. {
  63. if (j >= MAXJUDGES)
  64. {
  65. // If the number of judges overflow the array size,
  66. // we have to clear off the remaining judge entries
  67. // so that we can continue reading from the next
  68. // player entry
  69. int k;
  70. if (!(fin >> k) || k == -1)
  71. break;
  72. else
  73. continue;
  74. }
  75. if (!(fin >> judgeNumber[i][j]) || judgeNumber[i][j] == -1)
  76. break;
  77. if (!(fin >> score[i][j]))
  78. break;
  79. }
  80. }
  81. return i;
  82. }
  83. int ProcessScores(ifstream& fin)
  84. {
  85. for (i = 0; i < MAXMEMBERS; i++)
  86. {
  87.  
  88. averageRaw[i][j]= score[i][j] + sum;
  89. void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES],
  90. int numPlayers)
  91. {
  92. for (i = 0; i < MAXMEMBERS; i++)
  93. {
  94. fout << "Piano Player: ";
  95. fout << pianoPlayer[i] << "\n";
  96. for (j =0; j < MAXJUDGES; j++)
  97. {
  98. fout << "Judge Number: " << judgeNumber[i][j] << " ";
  99. fout << "Score: " << score[i][j] << "\n";
  100. }
  101. }
  102. return;
  103. }

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

You need to have the underlined portion in your condition, as mentioned in my previous post:
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;
}
You might also consider adding 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;
}
In fact, the 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;
}
I noticed that you are placing redundant variables (underlined) in your method declarations, which I cleared off in ReadScores, and now here in PrintReport:
void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES], int numPlayers)
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):
#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);
	}
...
By the way, looking at the source code you gave, that shouldn't compile at all. Your ProcessScores method is deformed. What's with that?
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

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)
  1. int ProcessScores(ifstream& fin)
  2. {
  3. int numJudges;
  4. double weightedScore[i][j];
  5. double average[i];
  6. for (numJudges = 0; numJudges < MAXJUDGES && judgeNumber[i][j] != -1; ++numJudges)
  7. {
  8. totalScore[i][j] += score[i][j];
  9. }
  10. if (numJudges != 0)
  11. {
  12. average[i] = totalScore[i][j] / numJudges;
  13. i++;
  14. }
  15. return i;
  16. }
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Mar 27th, 2006
0

Re: help with parrallel arrays

You might want to add two more global array variables first, assuming you need the unweighted and weighted:
C++ Syntax (Toggle Plain Text)
  1. int average[MAXMEMBERS];
  2. int weightedScore[MAXMEMBERS];
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:
C++ Syntax (Toggle Plain Text)
  1. void ProcessScores ()
  2. {
  3. int numJudges;
  4. double totalScore;
  5. for (int i = 0; i < numPlayers; ++i)
  6. {
  7. for (numJudges = 0, totalScore = 0; numJudges < MAXJUDGES && judgeNumber[i][j] != -1; ++numJudges)
  8. {
  9. totalScore += score[i][numJudges];
  10. }
  11. if (numJudges != 0)
  12. average[i] = totalScore / numJudges;
  13. weightedScore[i] = average[i] * weightFactor[i];
  14. }
  15. }
I've changed your return type to 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!
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with Array Validation
Next Thread in C++ Forum Timeline: Stacks - balanced parentheses





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC