help with parrallel arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 50
Reputation: AstroNox is an unknown quantity at this point 
Solved Threads: 2
AstroNox AstroNox is offline Offline
Junior Poster in Training

Re: help with parrallel arrays

 
0
  #41
Mar 28th, 2006
This is as simple as I can go without breaking the logic...
  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:
  1. #include <algorithm>
Then the following is the method that does this:
  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
  1. void SwitchProfLevel(ofstream& fout, const int& i)
Hope these help.
Best Regards, God Bless,
AstroNox
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 49
Reputation: lsu420luv is an unknown quantity at this point 
Solved Threads: 0
lsu420luv lsu420luv is offline Offline
Light Poster

Re: help with parrallel arrays

 
0
  #42
Mar 28th, 2006
the sort array gives me a segentation fault.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 50
Reputation: AstroNox is an unknown quantity at this point 
Solved Threads: 2
AstroNox AstroNox is offline Offline
Junior Poster in Training

Re: help with parrallel arrays

 
0
  #43
Mar 29th, 2006
Sort array? Which array?

Try this out anyway, I fixed a logic error. Hope this helps.
  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. }
Best Regards, God Bless,
AstroNox
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 49
Reputation: lsu420luv is an unknown quantity at this point 
Solved Threads: 0
lsu420luv lsu420luv is offline Offline
Light Poster

Re: help with parrallel arrays

 
0
  #44
Mar 29th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help with parrallel arrays

 
0
  #45
Mar 30th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 49
Reputation: lsu420luv is an unknown quantity at this point 
Solved Threads: 0
lsu420luv lsu420luv is offline Offline
Light Poster

Re: help with parrallel arrays

 
0
  #46
Apr 2nd, 2006
any ideas on how to get the selection sort to work. And it should be reading in a -1. There is one .
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help with parrallel arrays

 
0
  #47
Apr 3rd, 2006
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 49
Reputation: lsu420luv is an unknown quantity at this point 
Solved Threads: 0
lsu420luv lsu420luv is offline Offline
Light Poster

Re: help with parrallel arrays

 
0
  #48
Apr 6th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 49
Reputation: lsu420luv is an unknown quantity at this point 
Solved Threads: 0
lsu420luv lsu420luv is offline Offline
Light Poster

Re: help with parrallel arrays

 
0
  #49
Apr 7th, 2006
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?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 49
Reputation: lsu420luv is an unknown quantity at this point 
Solved Threads: 0
lsu420luv lsu420luv is offline Offline
Light Poster

Re: help with parrallel arrays

 
0
  #50
Apr 7th, 2006
I have fiddlesd with it, but am obviously missing some points Here it is now::;
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC