By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,850 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,850 IT Pros & Developers. It's quick & easy.

How to plot a histogram from an array

P: 3
Dear All,

I am new to perl. I need your help with processing the grade.dat file. I have a file containing a list of students with their varying scores from different type of assignments and tests. I have calculated the final grade and need to know the score distribution in a simple histogram like this:
78 xxxxxxx
79 xxxxxxxx
80 xxxxx
81 xxx
where x indicates the number of students gaining the score that equals to the left string.
Thanks for your prompt response.

Regards,
Jang
Mar 16 '08 #1
Share this Question
Share on Google+
5 Replies


eWish
Expert 100+
P: 971
Please show us the code that you have done in tying to accomplish this on your own. Also, this sound like homework/coursework, is it?

--Kevin
Mar 16 '08 #2

P: 3
Please show us the code that you have done in tying to accomplish this on your own. Also, this sound like homework/coursework, is it?

--Kevin
Yes, I've been working on my assignment (If this is not allowed, then I won't be asking questions anymore).
Well, to be frank, I haven't been able to compute the final score since I'm still trying to find a way to target a specific column. For example, the mid-test scores are at column 20 and final-test scores at 21 and I have to add them in to get the overall score of each student. Below is what I had in mind:

Expand|Select|Wrap|Line Numbers
  1. $mid = $i;
  2. for ($i = 20) {
  3.       $overall = $overall + $mid;
  4. }
  5. $final = $i;
  6. for ($i = 21) {
  7.      $overall = $overall + $final;
  8. }
Are they correct? Anyone willing to help?
Thanks.
Mar 18 '08 #3

eWish
Expert 100+
P: 971
Would you please show some sample data and the format of the file that the data is in?

When it comes to school work we will make suggestions, but not offer complete solutions. That way you will still learn.

--Kevin
Mar 18 '08 #4

P: 3
Would you please show some sample data and the format of the file that the data is in?

When it comes to school work we will make suggestions, but not offer complete solutions. That way you will still learn.

--Kevin
00569, Cindy, 40, 40, 40 ,38, 40, 40, 20, 25, 25, 10, 8, 9, 8, 8, 7, 8, 66, 82, 92
00580, Melina, 40, 39, 38, 40, 37, 40, 25, 25, 25, 10, 9, 9, 9, 8, 9, 70, 83, 93

The data contain the ID, name, scores of projects, quizzes, exercises, mid and final terms. I have to add up all scores for every student by first counting the percentage of each element of scoring and then assign a letter grade like A, B, and etc. Then i have to display a histogram like this.

90 xxxx
91 xxx
92 x
93 x
which means four students got 90, 3 got 91 and so on.
I'd really appreciate you would give me some clue.
Thanks.
Mar 19 '08 #5

eWish
Expert 100+
P: 971
Expand|Select|Wrap|Line Numbers
  1. # This gets the last two elements the line.
  2. my ($mid, $final) = (split ', ')[-2,-1];
  3.  
  4. # This assumes that each line is the same fixed length.
  5. my ($mid, $final) = (split', ',)[19, 20];
Expand|Select|Wrap|Line Numbers
  1. open(my $FILE, '<', $file) || die "Can't open $file: $!\n";
  2.     while(<$FILE>) {
  3.         chomp;
  4.  
  5.             # This gets the last two elements the line.
  6.             my ($mid, $final) = (split ', ')[-2,-1];
  7.  
  8.             # Do something with $mid and $final
  9.             print "Mid: $mid\n";
  10.             print "Final: $final\n";
  11.  
  12. }
  13. close($FILE);
If you store each occurrence of the various grades in a hash you can access them like so.

Expand|Select|Wrap|Line Numbers
  1. # $keys are the grade and $values would be the number of occurrences.
  2. while (my ($keys, $values) = each %scores_count) {
  3.     print "$keys ", ('X' x $values), "\n";
  4. }
--Kevin
Mar 24 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.