

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CS 110: Introduction to Computer Science Spring 2008
A loop is often used to iterate through a list of items, performing some operation. i= while i CS 110: Introduction to Computer Science Spring 2008 A matrix is represented as a list whose elements are lists (a list of lists). For matrix[0][1] the 0 index gives us the list [10,20,30], the 1 index gives us the value 20. We say that the first index is the row # and the second index the column #. So ‘row’ and ‘column’ would be appropriate loop variables (as an alternative to i and j) Adding up all the elements in the matrix could be done with: total = matrix[0][0]+matrix[0][1]+matrix[0][2]+ matrix[1,0]+matrix[1][1]+matrix[1][2] But this code is not very general-- it will only work for a 2x matrix. Can a nested loop be used instead? In class-Problem: Write a program that creates a matrix as in the statement on the previous page(***), then computes the sum of all elements in the matrix. The program should work no matter what size or content is in the matrix.