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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gopalsd
    New Member
    • Feb 2008
    • 1

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

    Hi Friends,

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

    as follows.....

    [CODE=perl]#!/usr/bin/perl
    @data=N;
    $sum=0;
    print"enter the required No. of numbers to be inputed : f";
    $N=<STDIN>;
    print"enter the number";
    for($i=0;$i<$N; $i+=1)
    {$data[$i]=<STDIN>;
    # chomp $data[$i];
    $sum=$sum + $data[$i];}

    # for($j=0;$j=$da ta[$i];$j+=1)
    print"the entered numbers are",@data,"\n" ;
    print"sum of the number:",$sum," \n";
    print"Average value the numbers:",$sum/$N,"\n";

    @sorting=sort(@ data);
    $lengt=@data;
    for ($j=0;$j < $lengt;$j++)
    {# print $sorting[$j],"\n";
    }

    $X= (@sorting[($j-1)/2]+ @sorting[$j/2])/2;
    print"the median is :$X";[/CODE]


    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.
    Last edited by eWish; Feb 12 '08, 02:23 PM. Reason: Please use [CODE][/CODE] tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    The way you have used sort function:

    [CODE=text]
    @sorting=sort(@ data);
    [/CODE]

    works for sorting strings in ascending order, numbers will be sorted according to ASCII value. If you want to sort the list numerically, use:

    [CODE=text]
    @sorting=sort {$a<=>$b} @data; # ascending order
    @sorting=sort {$b<=>$a} @data; # descending order
    [/CODE]

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      You can find median as follows:

      [CODE=text]
      @sorting=sort {$a<=>$b} @data;
      $length=@data;
      if(($length%2)= =0) { ###check for even no. of elements
      $X1=$sorting[$length/2-1]; ##remember:leng th of array=last index+1
      $X2=$sorting[$length/2];
      print"the medians are :$X1 , $X2"; ##display 2 medians
      }
      else {
      $X= $sorting[($length-1)/2];
      print"the median is :$X"; # median incase of odd elements
      }

      [/CODE]

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Please do not help people with their school tests nithinpes.

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Originally posted by KevinADC
          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.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by 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.

            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

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              nithinpes,

              please check your PM's

              Comment

              Working...