Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

MATH 224 – Discrete Mathematics
CS0007: Introduction to Computer Programming Array Algorithms.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 16: Searching, Sorting, and the vector Type.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Chapter 8 Arrays and Strings
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 11 Scott Marino.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Data Structure and Algorithms
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Chapter 16: Searching, Sorting, and the vector Type.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Objectives You should be able to describe: One-Dimensional Arrays
Arrays Chapter 7.
Searching Arrays Linear search Binary search small arrays
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Arrays … The Sequel Applications and Extensions
Arrays Kingdom of Saudi Arabia
Chapter 8 Arrays Objectives
MSIS 655 Advanced Business Applications Programming
24 Searching and Sorting.
Chapter 8 Arrays Objectives
Arrays Arrays A few types Structures of related data items
Presentation transcript:

Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino

MSMIS Kean University Topics Arrays –Definition –Initialization –Referencing elements Sequential Search Sorting Binary Search Multidimensional Arrays Linked Lists Lab 7 / Homework

Scott Marino MSMIS Kean University Arrays There are instances where data comes to a program as a collection (group) of data values of the same type –Zip codes –State Abbreviations –Telephone area codes –Employees, Students, etc. Assigning a unique variable name to each value would be difficult to manage in a program

Scott Marino MSMIS Kean University Arrays Creating a common name to be used to refer to the collection of values and then using a special identifier to access each individual value simplifies the programming process In C++ a collection of values is called an array In addition to easier access, special operations can be written to access and update an array

Scott Marino MSMIS Kean University Arrays Each value in an array is referred to as an element Each element in the array must be of the same data type In C++ the array is given a name just like other variables Each individual element is identified by an index position that is enclosed in [ ] (square braces) The first index position in the array is at location 0 (zero)

Scott Marino MSMIS Kean University Arrays Area code 201 is at array position (offset) 0 In a program, Area_Code[1] contains the value 732 cout << Area_Code[3] << endl; –Will print Area_Codes

Scott Marino MSMIS Kean University Arrays Defining an array const int num_area_codes = 999; … int area_code[num_area_codes]; Use a constant value, num_area_codes to set the maximum number of elements in the array –Define the constant to be a global variable Defines an array named area_code that has num_area_codes (999) elements

Scott Marino MSMIS Kean University Arrays - Initial Values An array can be populated with initial values –int area_code[num_area_codes] = {201,732, 973}; Will populate array positions 0, 1, and 2 with the values and will initialize the rest of the elements to zero (based on data type) –int area_code[num_area_codes] = {0}; Will define the array, and explicitly initialize the first element to 0 and default the remaining elements to 0

Scott Marino MSMIS Kean University Arrays - Referencing Elements Individual elements can be updated –area_code[0] = 201; Individual elements can be referenced –my_area_code = area_code[0]; Can use an integer variable inside the brackets to refer to an element –array_pos = 0; cout << area_code[array_pos] << endl; –Will print element 0

Scott Marino MSMIS Kean University Arrays - Referencing Elements –for (loopctr = 0; loopctr < num_area_codes; loopctr++) { cout << “Element “ << loopctr << “ contains “ << area_code[loopctr] << endl; } Will iterate through each element, printing the value –This code takes advantage of the num_area_codes constant value to control the end of the loop

Scott Marino MSMIS Kean University Arrays and Functions long int sumarray (int area_code[]) { long int array_tot = 0; int i; for (i = 0; i < num_area_codes; i++) { array_tot = array_tot + area_code[i]; } return array_tot ; } The above function will return the sum total of all elements in the array

Scott Marino MSMIS Kean University Arrays and Functions The function will receive an array (passed by value) with the following syntax: –data-type array-name[] The data type of the array is required The number of elements in the array is omitted The array name can be a local name for the array as the data is being passed by value Large arrays are typically passed by reference for performance reasons

Scott Marino MSMIS Kean University Arrays and Functions Passing an array to a function has the following syntax: –function-name(array-name); The name of the array is passed to the function (by value) –There is no indication of the number of elements The global constant value for the number of elements would be used inside the receiving function

Scott Marino MSMIS Kean University Arrays and Functions The formal parameters of the above functions did not include the references to the number of elements To write more generic functions that could be reused much easier, the function should receive 2 parameters, the array itself, and the number of elements in the array –Sort, search, and other array functions could be put into a header file and used in many programs

Scott Marino MSMIS Kean University Sequential Searching There is often a need to locate a specific element within an array –Or to determine if an element even exists The simplest method of searching an array is to start at the first element and examine each element in order –This is known as a sequential search

Scott Marino MSMIS Kean University Sequential Search A simple search function –int areacodeexists (int area_code[], int find_area) { int i; for (i = 0; i < num_area_codes; i++) { if (area_code[i] == find_area) { return 1; // true } } // end for loop return 0; // not found }

Scott Marino MSMIS Kean University Sequential Search Invoking the search function cout > input_code; if (areacodeexists(area_code, input_code)) { cout << input_code << “ is valid\n”; } else { cout << input_code << “is not valid\n”; }

Scott Marino MSMIS Kean University Bubble Sort There is often a need to put the elements in an array into a sorted sequence One of the simplest sorting algorithms is the bubble sort The bubble sort works by iterating through the array, swapping elements in the array based upon their values This algorithm is called a bubble sort because the smaller values “float” to the top of the array

Scott Marino MSMIS Kean University Bubble Sort The bubble sort is typically implemented as a nested loop algorithm An early-exit test can be implemented to terminate the looping early if all the elements are in sorted sequence, improving performance

Scott Marino MSMIS Kean University Bubble Sort The bubble sort algorithm: –for (bottom = num_codes - 1; bottom > 0; bottom--) { for (pos = 0; pos < bottom; pos++) { if (area_code[pos+1] < area_code[pos]) { temp_var = area_code[pos+1]; area_code[pos+1] = area_code[pos]; area_code[pos] = temp_var; } // end if } // end inner for } // end outer for

Scott Marino MSMIS Kean University Bubble Sort The outer loop starts one element position from the end of the array –Because the swap algorithm looks at bottom + 1 –For each pass of the outer loop, one element has been swapped to the bottom of the array The inner loop starts at the beginning of the array and swaps elements down until it reaches the current bottom (from the outer loop) of the array –A complete cycle of the inner loop is performed for each iteration of the outer loop

Scott Marino MSMIS Kean University Bubble Sort Swapping two elements requires the use of a temporary variable To swap 2 elements, A and B, the following occurs: –Copy element A to a temporary variable –Copy element B to element A –Copy the temporary variable to element B

Scott Marino MSMIS Kean University Binary Search A binary search is more efficient than a sequential search A binary search is done on an ordered (sorted) array and works by a continual halving and compare algorithm The larger the array the more apparent the benefit of a binary search Binary searches are usually recursive algorithms –The binary search function calls itself

Scott Marino MSMIS Kean University Multi-Dimensional Arrays Arrays can have multiple dimensions Sometimes know as an array of arrays –int matrix[2] [4] = { {1, 3, 5, 7} {10, 20, 30, 40} }; –Creates an array of dimension 2 that is an array of dimension 4

Scott Marino MSMIS Kean University Pointers There are “things” and “pointers to things” Every thing has a memory address in the computer Pointers point to the memory addresses in the computer int thing; /* define a thing */ int *thing_ptr; /define a pointer to thing */

Scott Marino MSMIS Kean University Pointers Pointer operators * - Dereference (given a pointer, get the thing referenced) & - Address of (given a thing, point to it)

Scott Marino MSMIS Kean University Pointers Pointer Syntax –thing - Simple thing (variable) –&thing - Pointer to variable thing –thing_ptr - Pointer to an integer (not necessarily thing) –*thing_ptr - Integer

Scott Marino MSMIS Kean University Linked Lists A linked list is a chain of items where each item points to the next item in the chain Very flexible because the size changes with the number of elements A linked list structure is implemented as a structure with data and pointer to the next element

Scott Marino MSMIS Kean University Homework Write a program that creates an array of 999 elements Using the random number function, generate a random integer for each of the 999 elements Write program code to determine the mean (average), and median values for all the elements in the array

Scott Marino MSMIS Kean University Homework average (mean) = sum total of all values / number of elements median = in an ordered list of elements, this is the element that falls in the exact middle of the list –Odd number of elements - the value at position (number of elements / 2) + 1 –Even number of elements - average of the 2 middle elements - The value at position (number of elements / 2) + ((number of elements / 2) + 1) / 2

Scott Marino MSMIS Kean University Homework Run the program 5 times and print the 5 results Turn in program and results