Homeworks: Arrays and foreach

Arrays and the foreach loop hw2

In this homework we are going to do some simple changes to a perl program, like we did during the last lab session. If you have Linux on your laptop you can download the program and change it with a text editor like we did in the lab. Note that your Linux may be a little different, so you may need to looks for the Terminal and the Text Editor, but they are there somewhere. If you do not have Linux yet, you can use codepad. This task is coded “hw2“.

We have seen that a variable $variabile is like a box where we can put values (numbers or strings). An array @array is like many boxes, or a box with many boxes inside, each one having a numeric index 0 to $#array. The easiest way to do something on the content of each box in the array is the foreach loop.

Consider the following script:

@numeri = (3,66,2,34,12,57);
$lunghezza = 0;

foreach $i (@numeri) {
    $lunghezza++;
    print "$i\n";
}

print "Lunghezza dell'array: $lunghezza\n";

As we did in the last lab, here is a list of changes that you have to make to the program. After you do one, run the program and check that the output is correct. Do not try to do them all together or you will die and burn in hell. OK, maybe not that, but we can assure you it won’t be pretty. Here are the changes:

  1. compute and print the sum of the numbers in the array,
  2. compute and print the arithmetic mean of the numbers in the array,
  3. compute and print the maximum number in the array,
  4. instead of computing the length of the array like we did, use the $#numeri special variable

You can easily check if you are doing things right. You will need to add other variables, print statements and stuff. For change 3 you will probably need to add an if. Feel free to peruse the slides, copy pieces from old programs, google things, etc. Try to do things by yourself before asking other people, it is the only way to learn. It is fine to compare your work with those of others (we recommend you do) but you should do your own first. If you can not do it, ask for help, but think about it with your head, otherwise it is useless.

You can ask your questions in the comments to this post, so everybody can see the answer. Let us know if you find the homeworks too hard! (or too easy 😉

How to submit your work

Once you checked that your script is okay, you must submit the work done filling this form. Remember to put your login ID, and the correct homework ID (this is hw2, okay?). In this case you can just put the final script on the first box (perl script 01).
In the future you might be required to produce more than one script.

Tagged

8 thoughts on “Homeworks: Arrays and foreach

  1. Carlo says:

    Ciao! Non ho capito il 4 punto..che bisogna fare? $#numeri non restituisce l’ultimo valore di un array? Non capisco il nesso con la lunghezza.. Grazie

    • proch says:

      Ciao,
      grazie per averci scritto!
      no, $#numeri non restituisce l’ultimo VALORE di un array, ma per verificare cosa ti restituisca ti invito a provare su codepad.org con un array a tuo piacere a farti stampare il contenuto di $#array.

      Per chiarire invece il punto chiesto, occorre specificare che per “lunghezza” si intende il numero di elementi che l’array contiene. Infatti nell’esempio riportato $lunghezza alla fine contiene il numero di elementi dell’array, giusto?

      Ciao

  2. Matteo says:

    Ciao,
    non è un problema se esagero con la tabulazione in modo ossessivo-compulsivo vero? Il disordine nel terminale mi infastidisce 🙂

    • proch says:

      Ciao, esagera pure, ma… tabulare cosa? Terminale?
      Non è chiarissimo dove vuoi tabulare, ma se intendi “indentare” il codice Perl… sono dalla tua!

  3. Lisa says:

    Seri problemi con il terzo punto….usare un “if”??? Come faccio? Una cosa del tipo “se questo numero è più grande di un altro fai sta cosa, altrimenti ne fai un’altra, e così in modo ciclico”??? C’è mica un comando, tipo int dei numeri interi, solo che si chiama max? Oppure, avevo pensato, posso mica mettere in ordine crescente i numeri nell’array e farmi stampare il contenuto dell’ultimo (che a questo punto è il più grande?). La mia testa mi dice che quest’ultima cosa è più logica :)!

    • Andrea Telatin says:

      Ciao,
      nessun trucco, bensi la strada che hai più o meno abbozzato a parole: SE questo numero è piu grande di (quale altro???) allora fai qualcosa (cosa?).
      Ricorda che possiamo creare variabili a piacimento e usarle per tenere a mente (ehm,… memoria) qualsiasi pezzetto di informazione.
      Sono come le “dita” usate per contare… 🙂

  4. […] solution for hw2 is reported in […]

Leave a Reply to Carlo Cancel reply

Your email address will not be published. Required fields are marked *