943,635 Members | Top Members by Rank

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

Re: help with parrallel arrays

This is as simple as I can go without breaking the logic...
C++ Syntax (Toggle Plain Text)
  1. fin >> judgeNumber[i][j];
  2. if (!fin || judgeNumber[i][j] == -1)
  3. {
  4. judgeCount[i] = j;
  5. break;
  6. }
  7.  
  8. fin >> score[i][j];
  9. if (!fin)
  10. {
  11. judgeCount[i] = j;
  12. break;
  13. }

I've done the sorting thing, and I believe it should work. Didn't test because I did not have the data. You must add this to the top of the file, together with the other includes:
C++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
Then the following is the method that does this:
C++ Syntax (Toggle Plain Text)
  1. void SortAverage ()
  2. {
  3. int mvt[MAXMEMBERS];
  4. for (int i = 0; i < numPlayers - 1; ++i)
  5. {
  6. double max = average[i];
  7. for (int j = i + 1; j < numPlayers; ++j)
  8. {
  9. if (max < average[j])
  10. {
  11. max = average[j];
  12. mvt[i] = j;
  13. }
  14. }
  15. if (i != mvt[i])
  16. swap(average[i], average[mvt[i]]);
  17. }
  18. for (int i = 0; i < (numPlayers - 1); ++i)
  19. {
  20. if (mvt[i] != 0)
  21. {
  22. swap(pianoPlayer[i], pianoPlayer[mvt[i]]);
  23. swap(weightFactor[i], weightFactor[mvt[i]]);
  24. swap(profLevel[i], profLevel[mvt[i]]);
  25. swap(judgeCount[i], judgeCount[mvt[i]]);
  26. for (int j = 0; j < MAXJUDGES; ++j)
  27. {
  28. swap(score[i][j], score[mvt[i]][j]);
  29. swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]);
  30. swap(weightedScore[i][j], weightedScore[mvt[i]][j]);
  31. }
  32. }
  33. }
  34. }

The switch for your SwitchProfLevel is not working because:
  1. The method should take in an int, which represents the array index of the student you want to print. This applies to SwitchCatType too.
  2. As a sideline, you don't need to pass in the array, as mentioned before, I'll mention again, profLevel is a global variable, and globals do not need to be passed into the method to be accessed. This applies to SwitchCatType too.
In otherwords, your method signature for SwitchProfLevel should be
C++ Syntax (Toggle Plain Text)
  1. void SwitchProfLevel(ofstream& fout, const int& i)
Hope these help.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006
Mar 28th, 2006
0

Re: help with parrallel arrays

the sort array gives me a segentation fault.
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Mar 29th, 2006
0

Re: help with parrallel arrays

Sort array? Which array?

Try this out anyway, I fixed a logic error. Hope this helps.
C++ Syntax (Toggle Plain Text)
  1. void SortAverage ()
  2. {
  3. int mvt[MAXMEMBERS];
  4. for (int i = 0; i < numPlayers - 1; ++i)
  5. {
  6. double max = average[i];
  7. for (int j = i + 1; j < numPlayers; ++j)
  8. {
  9. if (max < average[j])
  10. {
  11. max = average[j];
  12. mvt[i] = j;
  13. }
  14. }
  15. if (mvt[i] != 0)
  16. swap(average[i], average[mvt[i]]);
  17. }
  18. for (int i = 0; i < (numPlayers - 1); ++i)
  19. {
  20. if (mvt[i] != 0)
  21. {
  22. swap(pianoPlayer[i], pianoPlayer[mvt[i]]);
  23. swap(weightFactor[i], weightFactor[mvt[i]]);
  24. swap(profLevel[i], profLevel[mvt[i]]);
  25. swap(judgeCount[i], judgeCount[mvt[i]]);
  26. for (int j = 0; j < MAXJUDGES; ++j)
  27. {
  28. swap(score[i][j], score[mvt[i]][j]);
  29. swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]);
  30. swap(weightedScore[i][j], weightedScore[mvt[i]][j]);
  31. }
  32. }
  33. }
  34. }
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
AstroNox is offline Offline
50 posts
since Mar 2006
Mar 29th, 2006
0

Re: help with parrallel arrays

What is the statement with cont int& i what does that mean? The sorting still gives me segmentation fault. If i use this it causes an infinite loop. Why?

while (judgeNumber[i][j] != -1)
{
fin >> score[i][j];
fin >> judgeNumber[i][j];
}

I subsituted that for the if(1( lines my teacher counted off for those because I wasnt supposed to know how to do that yet
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Mar 30th, 2006
0

Re: help with parrallel arrays

Can't say for sure based on the snippet you posted, but based on what you have posted the most likely scenario is the information you are reading from doesn't have a -1 value to be read into judgeNumber[][]

Segmentation faults frequently occur when you try to overread an array, that is, when you try to access an invalid index of an array.

const int& i could be used when you want a variable of type referece (the & character) to type int with name i and you don't want the value of i to change, that is you want i to be constant, so you use the const keyword.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 2nd, 2006
0

Re: help with parrallel arrays

any ideas on how to get the selection sort to work. And it should be reading in a -1. There is one .
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Apr 3rd, 2006
0

Re: help with parrallel arrays

Selection sort strategy---find the element with the extreme value (low or high) that hasn't been sorted yet and swap it with the firt unsorted element.

Pseudocode for a selectionSort:
C++ Syntax (Toggle Plain Text)
  1. void selectionSort(arrayType * arrayName, int array_size)
  2. int i, j; //loop counters
  3. int index; //index of the minimal (or maximum if you are doing descending sort) value
  4.  
  5. declare outer loop to control what element you start at within arrayName. i should range from zero to arraySize minus two.
  6. In the outer loop body:
  7. assume that current element is the extreme (smallest or largest, whatever) so assign i to index
  8. use an inner loop to look at all element in array with indexes higher than i (j should range from i + 1 to arraySize minus 1)
  9. in inner loop body:
  10. if(arrayName[index] less than (or greater than depending on whether sorting in ascending or descending order) arrayName[j])
  11. assign j to index
  12. when inner loop is done the element at arrayName[index] will be the extreme value (low or high) of the unsorted elements
  13. if(index is not the same as i)
  14. then need to swap elements with indexes of index and i in all appropriate parallel arrays
  15.  
  16. Pseudocode to swap elements of an array:
  17. void swapElements(int i, int j, arrayType1 * array1Name)
  18. arrayType1 temp
  19.  
  20. temp = array1Name[i]
  21. array1Name[i] = array1Name[j]
  22. array1Name[j] = temp
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 6th, 2006
0

Re: help with parrallel arrays

Is there a way to make just the weightedd average be a fout.precision(2);?I trid to just put that in front but that obviously int the way to do it. thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Apr 7th, 2006
0

Re: help with parrallel arrays

nevermind on the precision thing i fixedi it. I am completely lost on the selection sort though. Here is what I got. It is incomplete. Can you point me in the right direction?
C++ Syntax (Toggle Plain Text)
  1. void SelectionSort()
  2. {
  3. int numPlayers;
  4. int i, j;
  5. int lowIndex, temp;
  6. int mvt[MAXMEMBERS];
  7. for (int i = 0; i < (numPlayers - 1); i++)
  8. {
  9. lowIndex=i;
  10. for (numPlayers=0; numPlayers < i-1; numPlayers++)
  11. {
  12. if (mv
  13. if (mvt[i] != 0)
  14. {
  15. swap(pianoPlayer[i], pianoPlayer[mvt[i]]);
  16. swap(weightFactor[i], weightFactor[mvt[i]]);
  17. swap(profLevel[i], profLevel[mvt[i]]);
  18. swap(judgeCount[i], judgeCount[mvt[i]]);
  19. for (int j = 0; j < MAXJUDGES; ++j)
  20. {
  21. swap(score[i][j], score[mvt[i]][j]);
  22. swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]);
  23. swap(weightedScore[i][j], weightedScore[mvt[i]][j]);
  24. }
  25. }
  26. }
  27. }
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 posts
since Mar 2006
Apr 7th, 2006
0

Re: help with parrallel arrays

I have fiddlesd with it, but am obviously missing some points Here it is now::;
C++ Syntax (Toggle Plain Text)
  1. void SelectionSort(double WeightedScore[MAXMEMBERS], int numPlayers)
  2. {
  3. int i, j;
  4. double indexOfNextSmallest;
  5. for (int i = 0; i < (numPlayers-1); i++)
  6. {
  7. for (j = i+1; j < numPlayers+1; j++)
  8. {
  9. indexOfNextSmallest = indexOfSmallest(weightedScore, startIndex, numPlayers);
  10. SwapValues(weightedScore[i], weightedScore[indexOfNextSmallest]);
  11. }
  12. }
  13. }
  14. void SwapValues(int& v1, int& v2)
  15. {
  16. int temp;
  17. temp = v1;
  18. v1 = v2;
  19. v2 = temp;
  20. }
  21. double indexOfSmallest(double weightedScore[MAXMEMBERS], int startIndex, int numPlayers)
  22. {
  23. int min = weightedScore[startIndex],
  24. indexOfMin = startIndex;
  25. for (i = startIndex +1; i < numPlayers; i++)
  26. {
  27. if (weightedScore[i] < min)
  28. {
  29. min = weightedScore[i];
  30. indexOfMin = index;
  31. }
  32. }
  33. return indexOfMin;
  34. }

I dont even know if I am using the right arrays. it is do tommorow by noon so I may be screwed. It is the revision I got 18 out of 20 already, but really need to get the 20. Thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
lsu420luv is offline Offline
49 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