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

Can anyone pls help me to resolve my school test in PERL

P: 1
Hi Friends,

Can anyone pls help me to resolve my school test in PERL................

as follows.....

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. @data=N;
  3. $sum=0;
  4. print"enter the required No. of numbers to be inputed : f";
  5. $N=<STDIN>;
  6. print"enter the number";
  7. for($i=0;$i<$N;$i+=1)
  8. {$data[$i]=<STDIN>;
  9. # chomp $data[$i];
  10. $sum=$sum + $data[$i];}
  11.  
  12. # for($j=0;$j=$data[$i];$j+=1)
  13. print"the entered numbers are",@data,"\n";
  14. print"sum of the number:",$sum,"\n";
  15. print"Average value the numbers:",$sum/$N,"\n";
  16.  
  17. @sorting=sort(@data);
  18. $lengt=@data;
  19. for ($j=0;$j < $lengt;$j++)
  20. {# print $sorting[$j],"\n";
  21. }
  22.  
  23. $X= (@sorting[($j-1)/2]+ @sorting[$j/2])/2;
  24. print"the median is :$X";

QUE: the median is the middle value if the numbers have been sorted in
ascending order (for even numbers of values it is your choice to pick
the first or second of the middle numbers.
Feb 12 '08 #1
Share this Question
Share on Google+
6 Replies


nithinpes
Expert 100+
P: 410
The way you have used sort function:

Expand|Select|Wrap|Line Numbers
  1. @sorting=sort(@data);
  2.  
works for sorting strings in ascending order, numbers will be sorted according to ASCII value. If you want to sort the list numerically, use:

Expand|Select|Wrap|Line Numbers
  1. @sorting=sort {$a<=>$b} @data; # ascending order
  2. @sorting=sort {$b<=>$a} @data; # descending order
  3.  
Feb 12 '08 #2

nithinpes
Expert 100+
P: 410
You can find median as follows:

Expand|Select|Wrap|Line Numbers
  1. @sorting=sort {$a<=>$b} @data;
  2. $length=@data;
  3. if(($length%2)==0) {   ###check for even no. of elements
  4.   $X1=$sorting[$length/2-1];     ##remember:length of array=last index+1
  5.   $X2=$sorting[$length/2];
  6.   print"the medians are :$X1 , $X2";      ##display 2 medians
  7. }   
  8. else {
  9. $X= $sorting[($length-1)/2];
  10. print"the median is :$X";         # median incase of odd elements
  11. }
  12.  
  13.  
Feb 12 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Please do not help people with their school tests nithinpes.
Feb 12 '08 #4

nithinpes
Expert 100+
P: 410
Please do not help people with their school tests nithinpes.
I apologize for that. Since, some effort in scripting was done by the person, I thought it won't be against posting guidelines.
Anyway, this won't be repeated.
Feb 12 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
I apologize for that. Since, some effort in scripting was done by the person, I thought it won't be against posting guidelines.
Anyway, this won't be repeated.

Homework I can see, but not tests. They have also posted this question on another forum so I think they are cheating, we should not encourage that, IMHO.

Kevin
Feb 12 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
nithinpes,

please check your PM's
Feb 12 '08 #7

Post your reply

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