help with parrallel arrays

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

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

help with parrallel arrays

 
0
  #1
Mar 26th, 2006
I am completly lost with arrays. I have scoured my book and the exaamples it gives are way to simple. I can wrerite them like those in the books, but I cannot seem to get the real problem to work. Also I am doing ifstream and ofstream so I cannot really see what is happening. I printed to screen once and it was just a bunch of garbage. Here is the problem: I have to take input from the file PianoREV.data and output it to Report.out. I need to setup parallel partially filled arrays for the contestant id's, student level, composision difficulty rating, num judges and overall averages. I need two dimensional arrays for the judge ids judges scores and weighted scores. Then a print report fucntion comes and prints it all. I have not attempted to write this as i cannot get the arrays to work to begin with. Here is an example of the PianoREV.

6010 1 1.3
23 7.0
25 8.5
34 7.0
12 7.5
-1
6012 1 1.2
23 7.5
34 7.0
45 7.0
50 7.5
-1

The first number is the player id. the second the proficiency level, the third is the weight factor, followed by judge and then score. I had a lot of help with psuedo code on the first assignment based on this task and would depply appreciate some more. MY teacher is really not helping any of us. Half the class has dropped and the other half is failing. Anyways here is what i have so far. Am I even going in the right direction?

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6. const int MAXMEMBERS = 25;
  7. int ReadScores (ifstream& fin, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS]);
  8.  
  9. int pianoPlayer[MAXMEMBERS];
  10. double score[MAXMEMBERS];
  11. double weightFactor[MAXMEMBERS];
  12. int profLevel[MAXMEMBERS];
  13.  
  14. const int MAXJUDGES = 7;
  15. int main()
  16. {
  17. ifstream fin;
  18. ofstream fout;
  19.  
  20. fin.open("PianoREV.data");
  21. if (fin.fail())
  22. {
  23. cout << "Error: Input File";
  24. exit (1);
  25. }
  26. fout.open("Report.out");
  27. if (fout.fail())
  28. {
  29. cout << "Error: Output File";
  30. exit (1);
  31. }
  32. ReadScores (fin, pianoPlayer, score);
  33. fin.close();
  34. fout.close();
  35. }
  36.  
  37. int ReadScores (ifstream& fin, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS])
  38. {
  39. int countPlayers = 0;
  40. while (countPlayers < MAXMEMBERS && (fin >> pianoPlayer[MAXMEMBERS]))
  41. {
  42. int i = 0;
  43. int judgeNumber[i];
  44. fin >> profLevel[countPlayers];
  45. fin >> weightFactor[countPlayers];
  46. while (judgeNumber[i] != -1)
  47. {
  48. for(i = 0; i<MAXJUDGES;i++)
  49. {
  50. fin >> judgeNumber[i];
  51. fin >> score[i];
  52. fin >> judgeNumber[i];
  53. }
  54. }
  55. }
  56. return countPlayers;
  57. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with parrallel arrays

 
0
  #2
Mar 26th, 2006
So, according to your description
Player ID = 6010
Proficiency Level = 1
Weight Factor = 1.3
Judge = 23
Score = 7.0

Then the next set is
Player ID = 25
Proficiency Level = 8.5
Weight Factor = 34
Judge = 7.0
Score = 12


That doesn't look right to me because the numbers in the first set are not the same type as those in the second set. You need to clarify what that data rally means before you can write a program to read them.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
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: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help with parrallel arrays

 
0
  #3
Mar 26th, 2006
The portion of the input file posted contains the data for two piano players, one with number 6010 and one with number 6012. The data is delimited by player with a -1. In this example each player has proficiency of 1 but one has weight factor 1.2 and the other weight factor 1.3. Each player received scores from 4 judges with the each judges number and score from that judge listed on a separate line. I assume that in other parts of the file a player may have scores from a different number of judges, hence the need to delineate player data by the -1 flag, but maybe it's just a learning exercise in how to identify and deal with delimeters when reading a file.

What I don't understand is how many arrays are needed:

>>I need to setup parallel partially filled arrays for the contestant id's, student level, composision difficulty rating, num judges and overall averages.

Does this imply 5 arrays are needed, one for each of the above sets of data?

And what do you mean by partially filled? Are you supposed to use tables of a given size but only certain elements within the table are going to be used---how will you know which elements are to be used and which aren't---by player number, judge number, develop a hash function?


Then what does this mean:

>>I need two dimensional arrays for the judge ids judges scores and weighted scores.

Are you supposed to read through the data for all scores from a given judges and place them in a single two dimensional array of type double?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with parrallel arrays

 
0
  #4
Mar 26th, 2006
if you know how to write a c++ class, you can do the project with no c-style arrays at all. Two classes and a vector should do the job.
  1. class judge
  2. {
  3. public:
  4. long JudgeID;
  5. float weight;
  6. };
  7.  
  8. class PianoRE
  9. {
  10. public:
  11. PianoRE() {PlayerID = Proficiency = 0; WeightFactor = 0.0f;}
  12. void SetPlayerID(long id) {PlayerID = id;}
  13. long GetPlayerID() {return PlayerID;}
  14. void SetProficiendy(long prof) {Proficiency = prof;}
  15. long GetProficiency() {return Proficiency;}
  16. void SetWeight(float w) {WeightFactor = w;}
  17. float GetWeight() {return WeightFactor;}
  18. void SetJudge(const judge& j) {judges.push_back(j);}
  19. const vector<judge>& GetJudges() {return judges;}
  20.  
  21. protected:
  22. long PlayerID;
  23. long Proficiency;
  24. float WeightFactor;
  25. vector<judge> judges;
  26.  
  27. };

or if you cannot yet handle that, you can create a structure and make a linked list of those structures
  1. struct judge
  2. {
  3. long JudgeID;
  4. float weight;
  5. struct judge* next;
  6. };
  7.  
  8. struct PianoRE
  9. {
  10. long PlayerID;
  11. long Proficiency;
  12. float WeightFactor;
  13. struct judges* HeadJudge;
  14. struct PianoRE* next;
  15. };
now just learn how to do linkes lists, and again you need not use
any c-style arrays.
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
  #5
Mar 26th, 2006
Hey lsu420luv, I would suggest the following way of implementing this part of your project. You would have to implement the output file logic yourself.
  1. typedef std::map<int, float> Ratings; // Use this type to store judge ratings
  2. typedef std::vector<PianoStudent> Players; // Use this type to store PianoStudents
  3.  
  4. struct PianoStudent
  5. {
  6. int player_id;
  7. int player_proficiency;
  8. float weight_factor;
  9. Ratings judge_ratings;
  10. };
  11.  
  12. int main ()
  13. {
  14. std::ifstream fin("PianoREV.data");
  15.  
  16. if (!fin) // Check if file open failed
  17. {
  18. cout << "Error: Input File";
  19. return 1;
  20. }
  21.  
  22. Players students; // PianoStudent vector
  23. PianoStudent ps;
  24.  
  25. while (fin)
  26. {
  27. if (!(fin >> ps.player_id))
  28. break;
  29. if (!(fin >> ps.player_proficiency))
  30. break;
  31. if (!(fin >> weight_factor))
  32. break;
  33.  
  34. // Process the judges
  35. while (fin)
  36. {
  37. int judge_id;
  38. float rating;
  39. if (!(fin >> judge_id) || judge_id == -1)
  40. break;
  41. if (!(fin >> rating))
  42. break;
  43. ps.judge_ratings.insert(std::make_pair(judge_id, rating)); // Add the judge rating
  44. }
  45. students.push_back(ps); // Add the student to the vector
  46. }
  47.  
  48. // Print out all the students
  49. cout << "Total students: " << students.size() << endl;
  50. for (Players::iterator iter = students.begin(); iter != students.end(); ++iter)
  51. {
  52. cout << "Player ID: " << iter->player_id << endl
  53. << "Player Proficiency: " << iter->player_proficiency << endl
  54. << "Weight Factor: " << iter->weight_factor << endl;
  55. for (Ratings::iterator jter = iter->judge_ratings.begin(); jter != iter->judge_ratings.end(); ++jter)
  56. {
  57. cout << "Judge ID: " << jter->first
  58. << " Rating: " << jter->second << endl;
  59. }
  60. }
  61.  
  62. return 0;
  63. }
I felt that you tried your best and I know there are many lecturers out there who don't do their job. I hope this would be of good help to you. I wrote the code to print out the information contained within the PianoStudent as a reference on how to retrieve the values from the vector and map. If you encounter any further problems, or if anyone has better suggestions to this solution, feel free to point them out.
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
  #6
Mar 26th, 2006
I am not supposed to know how to do vectors or structures yet. Lerner was right in his assumptions. They are supposed to be loaded into 5 partially filled arrays. Some of them hitting the max number of judges being 7 and some not. The judges is's the judges scores and the weighted scores are scored in seperate 2-d arrays that store the multiple scores for that player id. I hope this answers the questions.


Are you supposed to read through the data for all scores from a given judges and place them in a single two dimensional array of type double?

I am supposed to read through the scores for all judges for one player and score that in a 2-d array. Also the weighted scores and the jude ids.
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
  #7
Mar 26th, 2006
That's possible, but it could become very messy. It might be better to have three arrays, one for the judge ID, another for the score, and the last for the student ID. This student ID will allow you to match up the judge/score to the student's statistics, like proficiency and weight factor. If you could use pointers, dynamic means to allocating the memory is preferred because you do not know how large your future data files might be.

I thought lecturers wouldn't bother on structs, vectors and so on as long as the whole thing works properly. In fact I believe that most lecturers wouldn't bother to look at code if it is possible, say, unless you are supposed to submit an implementation of an algorithm or something somewhere along that line.
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
  #8
Mar 26th, 2006
Originally Posted by AstroNox
That's possible, but it could become very messy. It might be better to have three arrays, one for the judge ID, another for the score, and the last for the student ID. This student ID will allow you to match up the judge/score to the student's statistics, like proficiency and weight factor. If you could use pointers, dynamic means to allocating the memory is preferred because you do not know how large your future data files might be.

I thought lecturers wouldn't bother on structs, vectors and so on as long as the whole thing works properly. In fact I believe that most lecturers wouldn't bother to look at code if it is possible, say, unless you are supposed to submit an implementation of an algorithm or something somewhere along that line.
In the assignment sheet it requires the 5 arrays. She does do shit messy. She is an awful teacher I think. This whole class is code.
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
  #9
Mar 26th, 2006
Five arrays... well you could try your original method I guess. What more can I say since the situation is like that...
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
  #10
Mar 26th, 2006
Here is what I have been working on. It will not print to the report.out. It does not have all the arrays built, but it should fill the ones necessary at least to output some text to file. What am I doing wrong here?

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6. const int MAXMEMBERS = 25;
  7. const int MAXJUDGES = 7;
  8. int ReadScores (ifstream& fin, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES],
  9. int judgeNumber[MAXMEMBERS][MAXJUDGES]);
  10.  
  11. int pianoPlayer[MAXMEMBERS];
  12. double score[MAXMEMBERS][MAXJUDGES];
  13. double weightFactor[MAXMEMBERS];
  14. int profLevel[MAXMEMBERS];
  15. int judgeNumber[MAXMEMBERS][MAXJUDGES];
  16. int category; //category of music the students are playing
  17. //index of student id's
  18. //index of something
  19. int judgeCount[MAXMEMBERS];
  20. int numPlayers = 0;
  21. int main()
  22. {
  23. ifstream fin;
  24. ofstream fout;
  25.  
  26. fin.open("PianoREV.data");
  27. if (fin.fail())
  28. {
  29. cout << "Error: Input File";
  30. exit (1);
  31. }
  32. fout.open("Report.out");
  33. if (fout.fail())
  34. {
  35. cout << "Error: Output File";
  36. exit (1);
  37. }
  38. ReadScores (fin, pianoPlayer, score, judgeNumber);
  39. numPlayers = ReadScores (fin, pianoPlayer, score, judgeNumber);
  40. fin.close();
  41. fout.close();
  42. }
  43.  
  44. int ReadScores (ifstream& fin, int pianoPlayer[MAXMEMBERS], double score[MAXMEMBERS][MAXJUDGES],
  45. int judgeNumber[MAXMEMBERS][MAXJUDGES])
  46. {
  47. int i= 0;
  48. int j= 0;
  49. fin >> category;
  50. while (i < MAXMEMBERS && (fin >> pianoPlayer[i]))
  51. {
  52. fin >> profLevel[i];
  53. fin >> weightFactor[i];
  54. fin >> judgeNumber[i][j];
  55. while (judgeNumber[i][j] != -1)
  56. {
  57. j++;
  58. i++;
  59. fin >> score[i][j];
  60. fin >> judgeNumber[i][j];
  61. }
  62. }
  63. return i;
  64. }
  65. void PrintReport(ofstream& fout, int pianoPlayer[MAXMEMBERS], int score[MAXMEMBERS][MAXJUDGES],
  66. int numPlayers)
  67. {
  68. int i;
  69. int j;
  70. for (i = 0; i < numPlayers; i++)
  71. {
  72. fout << pianoPlayer[i];
  73. fout << score[i][j];
  74. j++;
  75. }
  76. return;
  77. }

I just couted to screen and it is not exiting the while loop when it hits the negative 1. Another things are the arrays filling right and how can i check this.
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