Chapter 8 - Arrays

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is wrong with the following?: Constant Integer SIZE = 100 Declare Real hours[SIZE] Declare Integer index = 0 Module main() For index = 0 to SIZE Set hours[SIZE] = "Forty" End For End Module

1. - There is an off-by-one error with the For statement; it should read SIZE - 1 2. - The value being stored into the "hours[SIZE]" array is a string when it is supposed to be a real number. You can not mix data types within an array.

Name ___ operations that you can perform on an array using a loop.

1.) Calculating the sum of all the values within an array. 2.) Averaging the values in an array. 3.) Finding the highest value in an array. 4.) Finding the lowest value in an array. 5.) Copying an Array 6.) Passing an array as an argument to a module or a function. 7.) 8.) 9.)

How many elements are in the following: Constant Integer ROWS = 3 Constant Integer COLS = 4 Declare Integer values[ROWS][COLS]

12

Suppose a teacher has six student and each student takes five exams during the semester - what is the best array to use for this in coding?

A 2D array. You could use six one-dimensional arrays, one for each student, with each array having 5 elements, one for each exam score, but this approach is cumbersome because you would have to separately process each of the arrays. Using a 2D array with six rows (one for each student) and five columns (one for each exam score) is a much better option.

[This is a to-my-understanding question - it could be incorrect] What is the difference between a parallel array and a 2D+ array?

A parallel array can only hold 1:1 data sets (ie , a person's name and how many hours they worked) while a 2D array can hold multiple data sets (ie , student names and their 5 test scores)

What type of array would you use when a user wants to input a ton of numbers but isn't sure how many numbers there are?

A partially filled array.

What is the sequential search algorithm?

A simple technique for finding an item in an array. It steps through the array, beginning at the first element, and compares each element to the item being searched for. The search stops when the item is found or the end of the array is reached. If the value being searched for is not in the array, the algorithm unsuccessfully searches to the end of the array.

What is a partially filled array? When are they typically used?

An array that has a size declarator larger than it's stored values / not all of the elements are filled with values. These are used when you need to store items in an array, but do not know the exact number of items in the series. For instance, if you don't know how many students you may have but need to enter names when they become available, you can make the Size Declarator 100 (even if you only end up using 30).

What is a two-dimensional array?

An array with two sets of data. You can view this array as having rows and columns of elements.

What is wrong here? What would catch this error?: Constant Integer SIZE = 5 Declare Integer numbers[SIZE] Set numbers[5] = 99

An error will occur because a value is being assigned to an incorrect subscript (only 0 - 4 are created) Array bounds checking would catch this error.

What numbers can a size declarator be?

Any nonnegative integer.

When does array bounds checking typically occur?

At runtime.

How do you declare a 2D array?

By having two bracketed numbers after the array name (ie , Declare Integer values[3][4]).

How are arrays passed in C#?

By reference because it is inefficient to make a copy of an entire array each time it is passed as an argument to a module or function.

How are arrays passed in C++?

By reference because it is inefficient to make a copy of an entire array each time it is passed as an argument to a module or function.

How are arrays passed in Java? Why are they passed this way?

By reference because it is inefficient to make a copy of an entire array each time it is passed as an argument to a module or function.

How do you calculate the total of the values of an array?

By using a loop with an accumulator variable. The loop steps through the array, adding the value of each array element to the accumulator.

What is an easier way to maintain the size of an array? Why is this easier?

By using constants as the size declarator. This makes it so if you need to modify the size of an array, you will only have to change the constant instead of changing the number everywhere that it appears in the source code.

[CODING - LONG ANSWER Design a program that requests the total sales for a company's 3 divisions for each quarter of the year. At the end of the program, display the total sales for the entire company.

Constant Integer ROWS = 3 Constant Integer COLS = 4 Declare Integer sales[ROWS][COLS] Declare Real totalSales = 0.0 Declare Integer row = 0, col = 0 For row = 0 to ROWS - 1 For col = 0 to COLS - 1 Display "Please enter the sales for Division ", row + 1, ", quarter ", col + 1 Input sales[row][col] End For End For For row = 0 to ROWS - 1 For col = 0 to COLS - 1 Set totalSales = totalSales + sales[row][col] End For End For Display "The total company sales are: $", currencyFormat(totalSales)

George needs a program that can let him input numbers, but he isn't sure how many numbers he needs to input (do not worry about what these numbers represent). He also needs a way to exit the program (-1) when he is finished inputting the numbers. When George exits, he needs the numbers displayed back to him. Write this program in Pseudocode for George.

Constant Integer SIZE = 1000 Declare Integer numbers[SIZE] Declare Integer count = 0 Declare Integer value = 0 Display "Please enter a number or -1 to quit: " Input value While count < SIZE AND value != -1 Set numbers[SIZE] = value Set count = count + 1 Display "Please enter a number or -1 to quit:" Input value End While Display "Here are the numbers you entered: " For index = 0 to count - 1 numbers[index] End For

Sally needs a program that can request the number of hours worked by three employees and then display the inputted hours afterwards. How would this look in Pseudocode? (Requested hours from 3 employees ... THEN ... Display hours from 3 employees)

Constant Integer SIZE = 3 Declare Integer hours[SIZE] Declare Integer index = 0 For index = 0 to SIZE - 1 Display "Please enter the number of hours worked by employee ", index + 1 Input hours[index] End For For index = 0 to SIZE - 1 Display "Employee ", index + 1, " worked ", hours[index], " hours." End For

How do you initialize an array that has 5 elements with the values 1, 2, 3, 4, and 5?

Constant Integer SIZE = 5 Declare Integer array[SIZE] = 1, 2, 3, 4, 5

Write a program that sequentially searches for the number 8. If found, display the element that the value is in. If not found, tell the user the element was not found. Use an array values with 5 elements (1, 2, 4, 8, 16).

Constant Integer SIZE = 5 Declare values[SIZE] = 1, 2, 4, 8, 16 Declare Integer index = 0 Declare searchValue = 8 Declare Boolean found = false While index < SIZE AND found == false If values[index] = searchValue Then Set found = true Else Set index = index + 1 End If End While If found == true Then Display "The search value was located at element ", index + 1 Else Display "The search value was not found." End If

Design a program that requests the name and hours worked of 4 employees. At the end of the program, display the gross pay of each employee. "Enter the name of employee 3 George Tarow Enter the hours worked by this employee. 18 ... Please enter the hourly wage for the employees. $12.75 Here is each employee's gross pay. George Tarow: $xx.xx ..."

Constant Integer SIZE = 6 Declare Integer names[SIZE] Declare Integer hours[SIZE] Declare Integer index = 0 Declare Real hourly = 0.0 Declare Real grossPay = 0.0 For index = 0 to SIZE - 1 Display "Please enter the name of employee ", index + 1 Input names[index] Display "Please enter the amount of hours worked by this employee." Input hours[index] End For Display "Please enter the hourly wage for the employees" Input hourly For index = 0 to SIZE - 1 Set grossPay = hourly * hours[index] Display names[index], ": $" currencyFormat(grossPay) End For

When the sequential search algorithm finished, the found variable will be set to true if the searchValue was found in the array. When this is the case, the index variable will be set to the subscript of the element containing the searchValue. If the searchValue was not found in the array, found will be set to False.

Explains what happens when this sequential search algorithm finishes (two answers/routes - found being true and found being false).

Write a For loop that copies the contents of one array into another array. Imagine declarations have already been made.

For index = 0 to SIZE - 1 Set secondArray[index] = firstArray[index] End For

Programs that process two-dimensional arrays commonly do so with __________ _________.

Nested loops

Do all programming languages have subscripts start at 0?

No, some start at 1.

How do you pronounce "numbers[0]"?

Numbers sub zero

What's wrong with the following?: Module main() Constant Integer SIZE = 5 Declare Integer numbers[SIZE] = 2, 4, 6, 8, 10 Declare Integer sum Set sum = GetTotal(numbers) Display "The sum of the array's elements are: ", sum End Module Function Integer getTotal(Integer array[]) Declare Integer index = 0 Declare Integer total = 0 For index = 0 to arraySize - 1 Set total = total + array[index] End For Return total End Function

Set sum = GetTotal(numbers) did not pass the total amount of elements in the array. It should have been (numbers, SIZE)

What is a search algorithm?

Techniques that locate a specific item in a larger collection of data.

How can you view the following declaration statement?: Declare Real seats[3][5][8]

The array has 3 sets of 5 rows with each row containing 8 elements.

Which array element does the sequential search algorithm first look at?

The first element (subscript 0)

What is the size declarator?

The number/variable inside the brackets of an array; it dictates how many elements are in an array. In... Set hours[NUMBER] = 14 ... NUMBER is the size declarator

What happens if a user searches for "Stephan" in an array that has three elements with values of "Matthew LeBlanc; Stephan Lament; Tyler Thomson"?

The search will return false because the user did not input the entire name (Stephan Lament).

What are elements?

The storage locations in an array.

What are subscripts? What are they also known as?

The unique number assigned to each element in an array. They are also known as indexes.

How many elements are in the following array? What are their subscripts?: Constant Integer SIZE = 5 Declare Integer numbers[SIZE]

There are 5 elements with subscripts 0, 1, 2, 3, and 4.

What's wrong with the following?: Constant Integer SIZE = 5 Module main() Declare Integer numbers[SIZE] = 2, 4, 6, 8, 10 Declare Integer sum Set sum = GetTotal(numbers) Display "The sum of the array's elements are: ", sum End Module Function Integer getTotal(Integer array[]) Declare Integer index = 0 Declare Integer total = 0 For index = 0 to arraySize - 1 Set total = total + array[index] End For Return total End Function

There is nothing wrong - the size declarator did not need to be passed to the getTotal function because it is a global constant.

How do you figure out the average of an array's values?

There should be a loop for totaling the values (with an accumulator variable) and then a statement that divides the values by the total number of elements in the array.

What is a similarity between an array and a variable?

They are both a named storage location in memory.

Where are an array's elements usually located?

They are located in consecutive memory locations.

Why is array bounds checking important?

This checking is critical for software verification and validation because subscripting arrays beyond their declared sizes may produce unexpected results, security holes, or failures.

What are parallel arrays?

Two or more arrays that hold related data, and the related elements in each array are accessed with a common subscript.

How do you change the size of an array?

You must change it in the source code, then recompile the program with the new size declarator (or rerun the program if you are using an interpreted language).

When passing an array as an argument to a method/function, what must you do?

You must include two arguments; the array and the size declarator/total amount of used elements (unless the size declarator/total elements is a global constant).

How can you force a program to search for partial string matches? For instance, if a user searches "Courtney" and there is an array element with a value of "Courtney Anderson", then "Courtney Anderson" will return (found == true)

You use the contains function.

How do you use the contains function in Pseudocode?

contains(string1, string2) This function returns True if string1 contains string2.

Is this true or false?: contains("Matthew", "Matt")

True - Matt is within Matthew.

If you wanted to store a person's name and their address, what is the best way to do this so they are always together?

Use a parallel array.

(FLIP THIS CARD OVER FOR THE QUESTION + PICTURE) 1.) Array is the array being searched. 2.) searchValue is the value that the algorithm is searching for. 3.) found is a Boolean variable that is used as a flag. Setting found to False indicates that searchValue has not been found. Setting found to True indicates that searchValue has been found. 4.) index is an Integer variable used as a loop counter. A sequential search algorithm.

What is this a diagram of? Explain the following: array, searchValue, found, and index. (The first question's answer is at the bottom of the card)

What is an Off-by-One error and what are two examples?

When a loop iterates one time too many or one time too few. For index = 0 to SIZE For index = 1 to SIZE - 1

What is array bounds checking?

When a programming language does not allow a statement to use a subscript outside the range of subscripts for an array.

What is an initialization list?

When a series of values are initialized to an array (separated by commas). ie. , Declare Integer numbers[SIZE] = >>> 10, 20, 30, 40, 50 <<<

When is a 2D array useful?

When you are working with multiple sets of data.

What is wrong with the following? Constant Integer INDEX = 10 Declare Integer hours[INDEX] Display "Your number is up! Actually... it's ..." Set INDEX = 14 Display INDEX

You can not change the size of an array in most programming languages.

A program uses two parallel arrays: names and creditScore. The names array holds customer names and the creditScore array holds customer credit scores. If a particular customer's name is stored in names[82], where would that customer's credit score be stored?

creditScore[82]

When you process a partially filled array, you must _______________________________.

process only the elements that contain valid data items.

By using the same _______, you can establish more relationships between data stored in two or more _______.

subscript; arrays.

In Pseudocode, write the elements (with subscripts, of course) of the third row from the following code: Constant Integer ROWS = 3 Constant Integer COLS = 4 Declare Integer values[ROWS][COLS]

values[2][0] values[2][1] values[2][2] values[2][3]

With an array, values, how do you access the element in the 5th row, 19th column?

values[4][18]

With an array, values, how do you access the element in the 25th column, 82nd row?

values[81][24]

What is the subscript of the first element of an array?

0

From the following code, what value is stored in testScores[1][2]?: Declare Integer testScores[3][4] = 88, 72, 90, 92, 67, 72, 91, 85, 79, 65, 72, 84

91

What is an array?

A data structure that allows you to store a group of values of the same data type together in memory.

What data types can use a sequential search algorithm for?

All data types.

What is happening here?: Constant Integer SIZE = 100 Declare Integer numbers[SIZE] Declare Integer index For index = 1 to SIZE - 1 Set numbers[index] = 0 End For

An Off-by-One error will occur because the For loop begins with a 1 instead of a 0 (meaning the loop will only iterate 99 times and not a full 100, according to the SIZE constant - this skips numbers[0])

What is a one-dimensional array?

An array that can only hold one set of data.

What is a partially filled array typically accompanied by?

An integer variable that holds the exact number of items that are actually stored in the array.

(FLIP THE CARD TO VIEW THE QUESTION & ANSWER) The following is in this format in order to prevent an accidental viewing of the answer prior to viewing the question. >>>[30]<<<

Based off the information in the attached graphic, what would the return value be equal to?

A bookstore keeps books on 50 racks with 10 sheleves each. Each shelf holds 25 books. Declare a 3D String array to hold the names of all the books in the store.

Declare books[50][10][25]

What must you do before you write the loop that determines which element has the highest/lowest value?

Initialize the highest/lowest variable with the value of the array's first element.

Design a For loop that checks for which element has the highest value and then displays that value to the user. Imagine index, SIZE, and values[SIZE] are already declared.

Set highest = values[0] For index = 1 to SIZE - 1 If values[index] > highest Then Set highest = values[index] End If End While Display "The highest value in the array is ", highest

Design a For loop that checks for which element has the lowest value.

Set lowest = values[0] For index = 1 to SIZE - 1 If values[index] < lowest Then Set lowest = values[index] End If End While Display "The lowest value in the array is ", lowest


Set pelajaran terkait

A&P Final Mastering questions (Heart)

View Set

MTA 98-366 Networking Fundamentals Lesson 4 Practice Questions

View Set

Behavioral Genetics and Epigenetics

View Set

Ch 4 Respiratory disorders lz peds exam 3

View Set

Art Appreciation Final Exam Study Guide

View Set