Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello perlguru22,

Since this looks like a homework assignment, I won’t solve it for you, but I will give you a few pointers:

  1. use strict; and declare variables as lexicals: my @freq;, my @num = 0 .. 54;

  2. @freq is specified to contain 55 elements, with indexes 0 to 54. So there is no point in setting aside 10,000 elements up front! In fact, you needn’t assign any elements up front; just declare the array as my @freq; and the elements will be autovivified (created as needed) as they are referenced in the for loop.

  3. For random integers in the range 0 to 54 inclusive, the formula is int(rand(55)) — see rand.

So, your solution should begin as follows:

#!/usr/bin/perl use strict; use warnings; my @num = (0 .. 54); my @freq; ++$freq[int(rand(55))] for 1 .. 10_000;

At this point, you have to sort the frequencies in descending order. The problem specification says:

You must then manually sort the frequency array in descending order and when swapping values...

The word “manually”, together with the reference to “swapping values”, rules out any recourse to Perl’s sort function. You will no doubt have been given one or more sorting algorithms in your coursework (for example, bubble sort, selection sort, quicksort — see Sorting_algorithm), and your task is to implement one of these to sort both @freq and @num in parallel.

Hint: The swapping part of your code should be something like this:

{ my $temp = $freq[$i]; # swap frequencies $freq[$i] = $freq[$j]; $freq[$j] = $temp; $temp = $num[$i]; # swap numbers $num[$i] = $num[$j]; $num[$j] = $temp; }

Hope that helps,

Athanasius <°(((><contra mundum


In reply to Re: Lottery using arrays by Athanasius
in thread Lottery using arrays by perlguru22

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found