Array Processing Lecture 7.

Slides:



Advertisements
Similar presentations
1.
Advertisements

One Dimensional Arrays
Chapter 9: Advanced Array Manipulation
Repetition There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is.
Repetition Control Structures
Programming Logic and Design Sixth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Chapter 9: Searching, Sorting, and Algorithm Analysis
Repetition Control Structure
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Repetition Control Structures
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
Pseudocode Algorithms Using Sequence, Selection, and Repetition.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Combination of Sequence, Selection and Repetition
Pseudocode algorithms using sequence, selection and repetition
Lists in Python.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Array Processing.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Pseudocode Algorithms Using Sequence, Selection, and Repetition
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Flowcharts. Symbol Terminal Symbol: indicates the starting or stopping pointin the logic. Input/Output Symbol: Represents an input or output process in.
Flowcharts Lecture 12.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 7 Arrays.
Siti Nurbaya Ismail Senior Lecturer
For Monday Read WebCT quiz 18.
Starting Out with Programming Logic & Design
Arrays Part 2.
Programming Logic and Design Fifth Edition, Comprehensive
Presentation transcript:

Array Processing Lecture 7

Motivation One of the most powerful programming tools available Help to organize a collection of homogeneous data items (same type and lenght). The individual data items that make up the array are called as Element of the array Example: Scores[3]  indicated the third exam score

Operation on Arrays Loading initial value into the element of array Processing the elements of the array Searching an array (linear or binary search) Writing out the array content to the report

Simple Algorithm to manipulate Array

Example 7.1 Find the sum of the elements of an array Each elemen of the array is accumulated into a variable called sum. When all elements have been added, the variable sum is printed.

Find_sum_of_elements Set sum to zero DO index = 1 to number_of_elements sum = sum + array(index) ENDO Print sum END

Example 7.2 Find the average of the elements of an array Each element of the array is accumulated into a variable called sum. When all elements have been added, the average of the elements is found and printed.

Find_element_average Set sum to zero DO index = 1 to number_of_elements sum = sum + array(index) ENDO Average = sum / number_of_elements Print average END

Example 7.3 Find the largest elements of an array The elements of an array are searched to determine which element is the largest. The algorithm starts by putting the first element of the array into the variable largest_element, and then looks at the other elements of the array to see if a larger value exists. The largest value is then printed.

Find_largest_element Set largest_element to array(1) DO index = 2 to number_of_elements IF array(index) > largest_element THEN largest_element = array(index) ENDIF ENDO Print largest_element END

Example 7.4 Find the smallest of the elements of an array The elements of an array are searched to determine the smallest element. The algorithm starts by putting the first element of the array into the variable smallest_element, and then looks at the other elements of the array to see if a smaller value exists. The smallest value is then printed.

Find_smallest_element Set smallest_element to array (1) DO index = 2 to number_of_elements IF array(index) < smallest_element THEN smallest_element = array(index) ENDIF ENDO Print smallest_element END

Example 7.5 Find the range of the elements of an array The elements of an array are searched to determine the smallest and the largest elements. The algorithm starts by putting the first element of the array into the variables smallest_element and largest_element, and then looks at the other elements to see if a smaller or larger value exists. The two values are then printed.

Find_range_of_element Set smallest_element to array(1) Set largest_element to array(1) DO index = 2 to number_of_elements IF array(index) < smallest_element THEN smallest_element = array(index) ELSE IF array(index) > largest_element THEN largest_element = array(index) ENDIF ENDO Print the range as smallest_element followed by largest_element END

Initialising the elements of an array Because an array is an iternal data structure, initial values must be placed into the array before any information can be retrieved from it. The initial values can be assigned to the element as constant or be read from file.

Loading constant values into array (only be used for data which is unlikely to be changed) Initialise_month_table month_table(1) = `Januari` month_table(2) = `February` . month_table(12) = `December` END

2. Loading initial values into an array from an input file Read_values_into_array Set max_num_elements to required value Set index to zero Read first input value DOWHILE (input value exist) AND (index<max_num_elements) index = index + 1 array(index) = input value Read next input value ENDDO IF (input value exist) AND index = max_num_elements THEN Print `Array size is too small` ENDIF END

Array of variable size Read_values_into_variable_array Set max_num_elements to required value Set index to zero Read first input value DOWHILE (input value NOT = 9999) AND (index<max_num_elements) index = index + 1 array(index) = input value Read next input value ENDDO IF index < max_num_elements THEN array(index) = 9999 ELSE Print `Array size is too small` ENDIF END Sentinel value  indicate the end of input records during initial processing and the last element of the array during further processing

Paired Arrays Two array with the same number of elements are paired because they correspond each other. Example: A student number ID array and student name array

Paired Arrays Example (Algorithm to read a file of product codes and corresponding selling price and load them into corresponding arrays) Read_values_into_paired_array Set max_num_elements to required value Set index to zero Read first input value DOWHILE (NOT EOF input record) AND (index<max_num_elements) index = index + 1 product_codes(index) = input_product_code selling_prices (index) = input_selling_price Read next record ENDDO IF (NOT EOF input record) AND index = max_num_elements THEN Print `Array size is too small` ENDIF END

Searching an Array The reason: To edit an input value (ie. Valid element or not) To retrieve information from array To retrieve information from corresponding element in a paired array.

Linear search array Linear_search_of_an_array Set max_num_elements to required value Set element_found to false Set index to 1 DOWHILE (NOT element_found) AND (index <= max_num_elements) IF array(index) = input_value THEN Set element_found to true ELSE index = index + 1 ENDIF ENDDO IF element_found THEN Print array (index) Print ´value not found´, input_value END

Binary Search Array (effective method for elements more than 25 and sorted into ascending sequence) Binary_search_of_an_array Set element_found to false Set low_element to 1 Set high_element to max_num_elements DOWHILE (NOT element_found) AND (low_element <= high_elements) index = (low_element + high_element)/2 IF input_value = array(index) THEN Set element_found to true ELSE IF input_value < array (index) THEN high_element = index – 1 low_element = index + 1 ENDIF ENDDO IF element_found THEN Print array (index) Print ´value not found´, input_value END

Writing out the contents of an Array Write_values_of_array DO index = 1 to number_of_elements Print array (index) ENDDO END

Programming Example Using Array

Example 7.6 Process exam scores Design a program that will prompt for and receive 18 examination scores from a mathematics test, compute the class average, and display all the scores and the class average to the screen.

Defining diagram Input Processing Output 18 exam scores Prompt the scores Get scores Compute class average Display scores Display class average Class_average

Control Structures required An array to store the exam scores – called ´scores´ An index to identify each element in the array A DO loop to accept the scores Another DO loop to display the scores to the screen.

Solution Algorithm Process_exam_scores Set total_score to zero DO index = 1 to 18 Prompt operator for score Get scores(index) total_score = total_score + scores(index) ENDDO Compute average_score = total_score / 18 DO index = 1 to 18 Display scores(index) Display average_score END

Example 7.7 Process integer array Design an algorithm that will read an array of 100 integer values, calculate the average integer value, and count the number of integers in the array that are greater than the average integer value. The algorithm is to display the average integer value and the count of integers greater than average.

Defining diagram Input Processing Output Read integer values Compute integer values Compute integer count Display integer average Display integer count Integer_average integer_count

Control Structures required An array of integer values – called ´numbers´ A DO loop to calculate the average of the integers Another DO loop to count the number of integers greater than the average.

Solution Algorithm Process_integer_array Set integer_total to zero Set integer_count to zero DO index = 1 to 100 integer_total = integer_total + numbers(index) ENDDO integer_average = integer_total / 100 DO index = 1 to 100 IF numbers(index) > integer_average THEN add 1 to integer_count ENDIF Display integer_average, integer_count END

Example 7.8 Validate sales number Design an algorithm that will read a file of sales transactions and validate the sales numbers on each record. As each sales record is read, the sales number on the record is to be verifief against an array of 35 sales numbers. Any sales number not found in the array is to be flagged as an error.

Defining diagram Input Processing Output Sales_record Sales_number Read sales records Validate sales numbers Print error message Error_message

Control Structures required A previously initialised array of sales numbers, called ´sales_numbers´ A DOWHILE loop to read the sales file A DOWHILE loop to perform linear search A variable element_found to stop the search

Solution Algorithm Validate_sales_numbers Set max_num_elements to 35 Read sales record DOWHILE sales_records exist Set element_found to false Set index to 1 DOWHILE (NOT element_found) AND (index <= max_num_elements) IF sales_numbers(index) = input sales number THEN Set element_found to true ELSE index = index + 1 ENDIF ENDDO IF NOT element_found THEN Print `invalid sales numer`, input sales number END

Example 7.9 Calculate shipping Charge Design an algorithm that will read an input weight for an item to be shipped, search an array of shipping weigths and retrieve a corresponding shipping charge. In this algorithm, two paired arrays, each containing six elements, have been established and initialised. The array, shipping_weights, contains a range of shipping weights in grams, and the array, shipping_charges, contains a corresponding array of shipping charges in dollars, as follows:

Shipping weights (grams) Shipping charges 1 – 100 3.00 101 – 500 5.00 501 – 1000 7.50 1001 – 3000 12.00 3001 – 5000 16.00 5001 - 9999 35.00

Defining diagram Input Processing Output Entry weight Prompt for entry weight Get entry weight Search shipping weight array Computer shipping charges Display shipping charge Shipping_charge error_message

Control Structures required Two arrays, called ´shipping_weigths´ and ´shipping_charges` A DOWHILE loop to search the shipping_weigths array and then retrieve the shipping_charges A variable element_found to stop the search when entry weight is found

Solution Algorithm Calculate_shipping_charge Set max_num_elements to 6 Read index to 1 Set element found to false Prompt for entry weigth Get entry weight DOWHILE (NOT element_found) AND (index <= max_num_elements) IF shipping_weights(index) < entry weight THEN add 1 to index ELSE set element_found to true ENDIF ENDDO IF element_found THEN shipping_charge = shipping charges(index) Display `shipping charge is`, shipping_charge Display `invalid shipping weight`, entry weight END

Two-dimensional Arrays Where two subscripts are required to locate an element in an array. The number of elements is calculated as the product of the number of rows and the number of columns Specified by: array(row_index,column_index)

Shipping weights (grams) One dimensional Array Shipping weights (grams) 1 – 100 101 – 500 501 – 1000 1001 – 3000 3001 – 5000 5001 - 9999

Shipping charges ($) (by shipping zone) Two dimensional Array Shipping charges ($) (by shipping zone) 1 2 3 4 2.50 3.50 4.00 5.00 6.50 4.50 7.50 10.00 11.00 12.00 13.50 16.00 20.00 27.50 32.00 34.00 35.00 38.00

Loading a two-dimensional array (from previous table) Read_values_into_array Set max_num_elements to 24 Set row_index to zero Read input file DOWHILE (input values exist) AND (row_index < 6) row_index = row_index + 1 DO column_index = 1 to 4 shipping_charges(row_index,column_index) = input value read input file ENDDO IF (input values exist) AND row_index = 6 THEN Print ´Array size to small` ENDIF END

Searching Two dimensional Array Calculate_shipping_Charges Set row_index to 1 Set element_found to false Prompt for shipping_weight, zone Get shipping_weight, zone DOWHILE (NOT element_found) AND (row_index <= 6) IF shipping_weights(row_index) < input shipping_weight THEN add 1 to row_index ELSE set element_found to true ENDIF ENDDO IF element_found THEN IF zone = (1 or 2 or 3 or 4) THEN shipping_charge = shipping_charges(row_index, zone) display ´shipping charge is`, shipping_charge display `invalid zone`, zone Display `invalid shipping weight`, input shipping_weight END

Writing out the contents of 2-d array Write_values_of_array Set number_of_rows to required value Set number_of_columns to required value DO row_index = 1 to number_of_rows DO column_index = 1 to number_of_columns Print array (row_index, column_index) ENDDO END