CS Unit 9

Ace your homework & exams now with Quizwiz!

Why do we use two for loops with two-dimensional arrays? One to move over the rows, and one for the columns. To correctly print out the contents. Because they can store both numbers and words. To do number calculations.

One to move over the rows, and one for the columns.

Why do we use two for loops with two-dimensional arrays? To correctly print out the contents. Because they can store both numbers and words. To do number calculations. One to move over the rows, and one for the columns.

One to move over the rows, and one for the columns.

What does it mean for an array to be row-major? All of the data are stored in rows. The row comes first in the element of an index. The row comes second in the element of an index. The row comes first in the index of an element.

The row comes first in the index of an element.

A two-dimensional array is essentially: an array of arrays a looped data structure a primitive data type a Boolean

an array of arrays

In an array, the element is ______________________________. the location of an individual piece of data in the array all of the data stored in the array the dimensions of the array an individual piece of data stored in the array

an individual piece of data stored in the array

In two-dimensional arrays, the _____________ is always listed second. dimension row initializer list column

column

Which loop initializes the array values to the following values? 1 2 3 1 2 3 1 2 3 1 2 3 Assume the array called values is already declared and initialized as a 4 x 3 array. for r in range (len (values)): for c in range (len(values[0])): values[r][c] = r + 1 for r in range (len (values)): for c in range (len(values[0])): values[r][c] = c + 1 for r in range (len (values)): for c in range (len(values[0])): values[r][c] = r * c for r in range (len (values)): for c in range (len(values[0])): values[r][c] = r + c

for r in range (len (values)): for c in range (len(values[0])): values[r][c] = c + 1

Consider the following code: vals = [] Which loop correctly builds a 14 x 17 array initialized to random 2-digit numbers? for r in range(14): vals.append([]) for c in range(17): vals[r][c] = random.randint(10,99) for r in range(14): for c in range(17): vals[r].append (random.randint(10,99)) for r in range(17): vals.append([]) for c in range(14): vals[r].append (random.randint(10,99)) for r in range(14): vals.append([]) for c in range(17): vals[r].append (random.randint(10,99))

for r in range(14): vals.append([]) for c in range(17): vals[r].append (random.randint(10,99))

A ___________ variable is available to all methods in a program. local global swap temp

global

Two-dimensional arrays are used to store data that can be represented in a _____________. string number row grid

grid

Consider the following code: grid = [] grid.append (["frog", "cat", "hedgehog"]) grid.append (["fish", "emu", "rooster"]) print (grid[0][2]) What is output? hedgehog cat emu rooster

hedgehog

In Python arrays are also called ____________. data lists initializers columns

lists

undervalue windmill knitting pretzel rational honey planetary bandana iron employment effort fabric What is in element [1][1]? windmill rational pretzel undervalue

rational

In two-dimensional arrays, the ___________ is always listed first. row initializer list dimension column

row

Which of the following lines of code will NOT correctly create a two-dimensional array? stuff.append([]) stuff[0].append(7) stuff = []stuff.append ([1, 5, 7, 2]) stuff = [][] stuff = [[3, 4, 5], [6, 3, 1]]

stuff = [][]

Consider the following two-dimensional array: 11 25 57 97 50 67 22 45 89 38 42 12 98 73 41 88 61 82 71 62 44 27 97 54 98 88 76 77 29 66 93 12 46 12 87 95 38 82 22 35 35 26 18 83 97 73 13 26 12 94 66 42 74 78 32 53 43 10 72 10 Which loop correctly adds the values in the fourth column (i.e. the column accessed by index 3)? sum = 0 for r in range(len(a)): sum = sum + a[3][r] sum = 0 for r in range(len(a)): sum = sum + a[r][3] sum = 0 for i in range(12): for j in range(5): sum = sum + a[r][c] sum = 0 for r in range(len(a)): a[r][3] = a[r][3] + sum

sum = 0 for r in range(len(a)): sum = sum + a[r][3]

For questions 5 and 6, consider the following two-dimensional array called element: undervalue windmill knitting pretzel rational honey planetary bandana iron employment effort fabric What is in element [0][1]? rational undervalue pretzel windmill

windmill

Consider the following code that stores values in a 5 x 3 array called grid: grid = [] grid.append(["frog", "cat", "hedgehog"]) grid.append(["fish", "emu", "rooster"]) grid.append(["dog", "bird", "rabbit"]) grid.append(["deer", "chipmunk", "opossum"]) grid.append(["fox", "coyote", "wolf"]) for r in range (len (grid)): for c in range (len(grid[0])): grid[r][c] = r * c After this code is run, what does the array grid hold? 1 1 1 1 2 3 1 3 5 1 4 7 1 5 9 0 0 0 0 1 2 0 2 4 0 3 6 0 4 8 0 1 2 1 2 3 2 3 4 3 4 5 4 5 6 2 2 2 3 2 2 4 3 2 5 3 3 6 4 3

0 0 0 0 1 2 0 2 4 0 3 6 0 4 8

Consider the following code: grid = [] grid.append([]) grid[0].append(9) grid[0].append(7) grid[0].append(5) grid[0].append(3) grid.append([]) grid[1].append(2) grid[1].append(8) grid[1].append(1) grid[1].append(3) How many rows and columns does the array created by this code have? 4 rows and 2 columns 2 rows and 2 columns 2 rows and 4 columns 4 rows and 4 columns

2 rows and 4 columns

Consider the following code: grid = [] grid.append([63, 59, 25, 86, 86, 24, 31]) grid.append([17, 28, 67, 68, 44, 27, 33]) grid.append([24, 31, 28, 67, 68, 44, 27]) What are the dimensions of the two-dimensional array created by this code? 3 x 7 4 x 7 7 x 7 7 x 3

3 X 7

For questions 2 and 3, consider the following two-dimensional array: 42 31 32 79 37 85 82 66 35 39 28 25 What is in element [2][1]? 39 42 82 85

39

Consider the following code: ar = [] ar.append ([29, 21, 33, -30]) ar.append ([39, 26, -43, 42]) ar.append ([123, 43, 33, 46]) How many columns does the array above have? 3 2 1 4

4

Consider the following code: ar = [] ar.append ([29, 21, 33, 52, 12, -30]) ar.append ([39, 18, 42, 26, -43, 42]) ar.append ([123, 43, 33, 46, 100, 9]) ar.append ([29, 11, 32, 27, -13, 15]) How many rows does the array created by the code above have? 4 1 2 3

4

Consider the following code: temp = [] temp.append ([25, 38, 47, 47, 24, 50, 20, 48, 46, 24, 21, 32, 40]) temp.append ([50, 20, 48, 46, 24, 21, 32, 40, 44, 47, 25, 22, 29]) temp.append ([21, 32, 40, 44, 47, 25, 22, 29, 26, 39, 43, 30, 49]) print (temp[1][2]) What is output? 32 48 20 38

48

Consider the following code: temp = [] temp.append ([25, 38, 47, 47, 24]) temp.append ([50, 20, 48, 46, 24]) temp.append ([21, 32, 40, 44, 47]) print(temp[1][2]) What is output by this code? 20 48 38 32

48

42 31 32 79 37 85 82 66 35 39 28 25 What is in element [1][1]? 39 42 82 85

85

Which method correctly swaps two rows of an array? def swapRows (grid, a, b): if ( a >=0 and a < len(grid)): if ( b >=0 and b < len(grid)): for c in range(len(grid[0])): temp = grid[a][c] grid[a][c] = grid[b][c] grid[b][c] = temp def swapRows (grid, a, b): if ( a >=0 and a < len(grid)): if ( b >=0 and b < len(grid)): for r in range(len(grid)): temp = grid[a][r] grid[a][r] = grid[b][r] grid[b][r] = temp def swapRows (grid, a, b): if ( a >=0 and a < len(grid)): if ( b >=0 and b < len(grid)): for c in range(len(grid[0])): temp = grid[c][a] grid[c][a] = grid[c][b] grid[c][b] = temp def swapRows (grid, a, b): if ( a >=0 and a < len(grid)): if ( b >=0 and b < len(grid)): for r in range(len(grid)): temp = grid[r][a] grid[r][a] = grid[r][b] grid[r][b] = temp

def swapRows (grid, a, b): if ( a >=0 and a < len(grid)): if ( b >=0 and b < len(grid)): for c in range(len(grid[0])): temp = grid[a][c] grid[a][c] = grid[b][c] grid[b][c] = temp

Consider the following code: grid = [] grid.append (["frog", "cat", "hedgehog"]) grid.append (["fish", "emu", "rooster"]) grid.append (["dog", "llama", "iguana"]) print(grid[2][0]) What is output? fish dog cat hedgehog

dog

Which loop initializes the array grid to the following values? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Assume the array called values is already declared and initialized as a 5 x 3 array. for r in range (len (grid)): for c in range (len(grid[0])): grid[r][c] = r + c for r in range (len (grid)): for c in range (len(grid[0])): grid[r][c] = r + 1 f = 0 for r in range (len (grid)): for c in range (len(grid[0])): f = f + 1 grid[r][c] = f for r in range (len (grid)): for c in range (len(grid[0])): grid[r][c] = c + 1

f = 0 for r in range (len (grid)): for c in range (len(grid[0])): f = f + 1 grid[r][c] = f


Related study sets

Bible test - 1st semester memory verses

View Set

Chapter 15: Decision Making Mgmt 310

View Set

Lesson 08 - Psychological Disorders

View Set

PHARM - Cardiac Practice Questions

View Set

AP Lang College board MCQ Practice

View Set

ECON 222 Production and Growth TEST 2 BOOK

View Set

Chapter 20: Lymphatic System; 21: Immune System

View Set