Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: New student, can we write this program in perl...

by choroba (Chancellor)
on Oct 19, 2012 at 16:36 UTC ( #999977=note: print w/replies, xml ) Need Help??

in reply to New student, can we write this program in perl...

Hallo rahulraina7, wellcome to the Monastery.

This is not a code writing service. Can you show how you would solve the problem in C? We can then describe what changes you should do to the code to translate it to Perl.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: New student, can we write this program in perl...

Replies are listed 'Best First'.
Re^2: New student, can we write this program in perl...
by rahulraina7 (Initiate) on Oct 19, 2012 at 16:45 UTC

    Thank you Choroba Sir for your interest , please have a look at C code.

    #include<stdio.h> #include<conio.h> void main() { int a[20],b[20],c[20],i,j=0,k=0,n; clrscr(); printf("Enter the number of elements"); scanf("%d",&n); printf("Enter n numbers\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { if((a[i]%2)==0) { b[j]=a[i]; j++; } else { c[k]=a[i]; k++; } } printf("The even numbers are\n"); for(i=0;i<j;i++) { printf("%d\n",b[i]); } printf("The odd numbers are\n"); for(i=0;i<k;i++) { printf("%d\n",c[i]); } getch(); }
    It will basically input an array of 'n' numbers and then separate it into two arrays, one contains odd numbers and one contains even numbers. Hoping to hear from you soon.
      I would change your program to this one:
      #!/usr/bin/perl use warnings; use strict; print 'Enter the number of elements: '; my $n = <>; # Read the input. chomp $n; # Remove the newline. print "Enter $n numbers.\n"; my @a; for (1 .. $n) { push @a, scalar <>; # Push numbers to the +array. } chomp @a; # Remove the newlines. print join (' ', 'The even numbers are:', grep $_ % 2, @a), # grep filters the num +bers = 1 modulo 2 "\n"; print join (' ', 'The odd numbers are:', grep 1 - $_ % 2, @a), "\n";
      Not really similar, is it? The main differences:
      • No main function needed.
      • Variables are declared where needed.
      • The diamond operator is used to read from the input.
      • for can be used with simpler syntax, avoiding off by 1 errors (see perlsyn)
      • grep can be used to filter arrays. No need for temporary variables to keep the results if you do not need them later.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      use strict; # #include<conio.h> #void main() #{ my (@a,@b,@c,$i,$j,$k,$n); print("Enter the number of elements\n"); $n = <>; printf("Enter %d numbers\n",$n); for($i=0;$i<$n;$i++) { push @a, scalar(<>); } for($i=0;$i<$n;$i++) { if(($a[$i]%2)==0) { $b[$j]=$a[$i]; $j++; } else { $c[$k]=$a[$i]; $k++; } } print("The even numbers are\n"); for($i=0;$i<$j;$i++) { printf("%d\n",$b[$i]); } printf("The odd numbers are\n"); for($i=0;$i<$k;$i++) { printf("%d\n",$c[$i]); } <>; #}

      Not that this was the best (or even just a particularly good) way to write this, but it's the minimal change. Huge difference from the C code, right?

      Enoch was right!
      Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999977]
[erix]: Makefile.PL should tell me, probably

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-06-22 16:48 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (524 votes). Check out past polls.