CSCI 152: LAB 6
Skill set for this LAB:
- Use Top-Down Design
- Lists, Functions and TWO-DIMENSIONAL LISTS (Chapter 8, special attention on 8.8)
- Testing programs
Use the following statement for
List initialization:
a = [[ 0 for i in range(COL)] for j in range(ROW)]
The above line will create a list of ROW rows and COL columns and assign value 0 to each element
- Problem 1:
Write a program that randomly generates the 2-dimensional list. Make sizes of the list user input. Program prints the list
in 3 different ways: all elements on different rows, each row on separate line, and list form ([[ first row], [second
row]...[..]]
- Problem 2:
Write a program that randomly generates the 2-dimensional list. Make sizes of the list user input. Program prints all odd
elements, finds the number of odd elements, their average and sum. Make a one-dimensional list of all odd elements. Have a separate
function that counts odd elements. Use the return value, counter, to decide on calling function average and sum. Only if there are odd
elements, you can calculate the average and sum
- Problem 3: Write a function that finds and returns the number of even and number
of odd elements in 2-dimensional list, their sums and averages.
Write main to test
your program. Pay attention, if there are no even elements or no odd element, the first return value will be 0, the
sum and average could be any numbers and you would need to use if statemet to print correct results.
- Problem 4:
Write a function that finds and returns the number of elements that are divisible by 3. Function also creates and returns
the new ONE-DIMENSIONAL list that includes all elements that are divisible by 3.
Write main to test your program
- Problem 5: Write a program that asks user to enter the number of family members with Facebook
account, their names, and the number of hours they spend on Facebook per day during the weekdays. The program finds the total
number of hours per family member and the family member who spend the most time on Facebook. Store hours in 2-d list. One rows
stores hours for one family member. The number of rows is the number of family members, the number of columns is 5 in our
example (there are 5 weekdays). Store family names in additional 1-d list.
Solution
Solution for question 5