Prof. Alfred J Bird, Ph.D., NBCT Office – Wheatly 2nd floor Office Hours – MW 3:00PM to 4:00PM
Write a program that: Asks you the numeric value of the month and the year you were born, creates an array with the names of the months and an array that contains the number of days in a month (remember leap years) and use these arrays in a print statement to print out the month, the year and how many days are in the month you were born based on the info you entered.
Write a program that: Asks you the numeric value of the month and the year you were born, creates an array with the names of the months and an array that contains the number of days in a month (remember leap years) and use these arrays in a print statement to print out the month, the year and how many days are in the month you were born based on the info you entered. #!/usr/bin/perl/ print “Enter the number of the month you were born: “; chomp ($month = ); print “Enter year you were born: “; chomp ($year = = qw&January February March April May June July August September October November = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $numDaysCorrect = $numDays[$month-1]; if ($month==2 && $year%4==0){$numDaysCorrect=29;} print (“$months[$month-1] $year has $numDaysCorrect days \n”);
Write code using the previous program that will print out a series of statements like the following: The month of January has 31 days … Assume you do not know how many entries there are in the arrays but that both arrays have the same numbers of entries
How do we add data to an array? = $scalar); #is one way! But there is a better way!! $scalar; #will do the same thing! push will append the value in $scalar to the top Likewise pop will take the last value in an array and do something with it. $scalar =
push() and pop() act on the top of an array (the highest indexed end) shift() and unshift() act on the bottom of an array and perform the same function. We already know what reverse() does.
Another function is sort(). What do you think it does? Write a simple program to try it with your array of months. Predict the output before you try it. What happened? Now write a simple program to try it with an array of number between 0 and 100 (at least 20 numbers). Predict the output before you try it. What happened????? Why?????
Read pages 95 to 114 in the textbook.