| | |
help with parrallel arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
This is as simple as I can go without breaking the logic...
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:
Then the following is the method that does this:
The switch for your
Hope these help.
C++ Syntax (Toggle Plain Text)
fin >> judgeNumber[i][j]; if (!fin || judgeNumber[i][j] == -1) { judgeCount[i] = j; break; } fin >> score[i][j]; if (!fin) { judgeCount[i] = j; break; }
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)
#include <algorithm>
C++ Syntax (Toggle Plain Text)
void SortAverage () { int mvt[MAXMEMBERS]; for (int i = 0; i < numPlayers - 1; ++i) { double max = average[i]; for (int j = i + 1; j < numPlayers; ++j) { if (max < average[j]) { max = average[j]; mvt[i] = j; } } if (i != mvt[i]) swap(average[i], average[mvt[i]]); } for (int i = 0; i < (numPlayers - 1); ++i) { if (mvt[i] != 0) { swap(pianoPlayer[i], pianoPlayer[mvt[i]]); swap(weightFactor[i], weightFactor[mvt[i]]); swap(profLevel[i], profLevel[mvt[i]]); swap(judgeCount[i], judgeCount[mvt[i]]); for (int j = 0; j < MAXJUDGES; ++j) { swap(score[i][j], score[mvt[i]][j]); swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]); swap(weightedScore[i][j], weightedScore[mvt[i]][j]); } } } }
The switch for your
SwitchProfLevel is not working because:- The method should take in an
int, which represents the array index of the student you want to print. This applies toSwitchCatTypetoo. - As a sideline, you don't need to pass in the array, as mentioned before, I'll mention again,
profLevelis a global variable, and globals do not need to be passed into the method to be accessed. This applies toSwitchCatTypetoo.
C++ Syntax (Toggle Plain Text)
void SwitchProfLevel(ofstream& fout, const int& i)
Best Regards, God Bless,
AstroNox
AstroNox
•
•
Join Date: Mar 2006
Posts: 50
Reputation:
Solved Threads: 2
Sort array? Which array?
Try this out anyway, I fixed a logic error. Hope this helps.
Try this out anyway, I fixed a logic error. Hope this helps.
C++ Syntax (Toggle Plain Text)
void SortAverage () { int mvt[MAXMEMBERS]; for (int i = 0; i < numPlayers - 1; ++i) { double max = average[i]; for (int j = i + 1; j < numPlayers; ++j) { if (max < average[j]) { max = average[j]; mvt[i] = j; } } if (mvt[i] != 0) swap(average[i], average[mvt[i]]); } for (int i = 0; i < (numPlayers - 1); ++i) { if (mvt[i] != 0) { swap(pianoPlayer[i], pianoPlayer[mvt[i]]); swap(weightFactor[i], weightFactor[mvt[i]]); swap(profLevel[i], profLevel[mvt[i]]); swap(judgeCount[i], judgeCount[mvt[i]]); for (int j = 0; j < MAXJUDGES; ++j) { swap(score[i][j], score[mvt[i]][j]); swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]); swap(weightedScore[i][j], weightedScore[mvt[i]][j]); } } } }
Best Regards, God Bless,
AstroNox
AstroNox
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
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:
Pseudocode for a selectionSort:
C++ Syntax (Toggle Plain Text)
void selectionSort(arrayType * arrayName, int array_size) int i, j; //loop counters int index; //index of the minimal (or maximum if you are doing descending sort) value declare outer loop to control what element you start at within arrayName. i should range from zero to arraySize minus two. In the outer loop body: assume that current element is the extreme (smallest or largest, whatever) so assign i to index 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) in inner loop body: if(arrayName[index] less than (or greater than depending on whether sorting in ascending or descending order) arrayName[j]) assign j to index when inner loop is done the element at arrayName[index] will be the extreme value (low or high) of the unsorted elements if(index is not the same as i) then need to swap elements with indexes of index and i in all appropriate parallel arrays Pseudocode to swap elements of an array: void swapElements(int i, int j, arrayType1 * array1Name) arrayType1 temp temp = array1Name[i] array1Name[i] = array1Name[j] array1Name[j] = temp
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
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)
void SelectionSort() { int numPlayers; int i, j; int lowIndex, temp; int mvt[MAXMEMBERS]; for (int i = 0; i < (numPlayers - 1); i++) { lowIndex=i; for (numPlayers=0; numPlayers < i-1; numPlayers++) { if (mv if (mvt[i] != 0) { swap(pianoPlayer[i], pianoPlayer[mvt[i]]); swap(weightFactor[i], weightFactor[mvt[i]]); swap(profLevel[i], profLevel[mvt[i]]); swap(judgeCount[i], judgeCount[mvt[i]]); for (int j = 0; j < MAXJUDGES; ++j) { swap(score[i][j], score[mvt[i]][j]); swap(judgeNumber[i][j], judgeNumber[mvt[i]][j]); swap(weightedScore[i][j], weightedScore[mvt[i]][j]); } } } }
•
•
Join Date: Mar 2006
Posts: 49
Reputation:
Solved Threads: 0
I have fiddlesd with it, but am obviously missing some points Here it is now::;
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
C++ Syntax (Toggle Plain Text)
void SelectionSort(double WeightedScore[MAXMEMBERS], int numPlayers) { int i, j; double indexOfNextSmallest; for (int i = 0; i < (numPlayers-1); i++) { for (j = i+1; j < numPlayers+1; j++) { indexOfNextSmallest = indexOfSmallest(weightedScore, startIndex, numPlayers); SwapValues(weightedScore[i], weightedScore[indexOfNextSmallest]); } } } void SwapValues(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; } double indexOfSmallest(double weightedScore[MAXMEMBERS], int startIndex, int numPlayers) { int min = weightedScore[startIndex], indexOfMin = startIndex; for (i = startIndex +1; i < numPlayers; i++) { if (weightedScore[i] < min) { min = weightedScore[i]; indexOfMin = index; } } return indexOfMin; }
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
![]() |
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 based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






