COMS 261 Computer Science I

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSE Lecture 3 – Algorithms I
Passing Arrays to Functions Programming. COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters l Arrays are.
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Algorithm Efficiency and Sorting
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
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Templates and Polymorphism Generic functions and classes
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Arrays and Containers Mechanism for representing lists.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
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.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CSIS 3401 Introduction to Data structures and algorithms.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Prof. Amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Sorting Mr. Jacobs.
User-Written Functions
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Top 50 Data Structures Interview Questions
May 17th – Comparison Sorts
Basic Array Definition
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
8 Pointers.
Algorithm design and Analysis
ARRAYS Lecture
7 Arrays.
Arrays Continued.
8/04/2009 Many thanks to David Sun for some of the included slides!
Sorting … and Insertion Sort.
Mechanism for representing lists
Search,Sort,Recursion.
24 Searching and Sorting.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Review of Everything Arrays
Searching.
COMS 261 Computer Science I
7 Arrays.
Chapter 9: Data Structures: Arrays
CS150 Introduction to Computer Science 1
COMS 261 Computer Science I
Development and Implementation
COMS 261 Computer Science I
COMS 261 Computer Science I
COMS 261 Computer Science I
Review for Midterm 3.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Discrete Mathematics CS 2610
Vectors.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

COMS 261 Computer Science I Title: Classes and Arrays Date: November 10, 2004 Lecture Number: 32

Announcements Project 4 Exam 3 Friday, 11/12/04 at 5:00pm Wednesday, 11/17/2004

Review Classes

Outline Class Abstract Data Type Rational Array

Auxiliary Insertion Operator ostream& operator<<(ostream &sout, const Rational &r) { r.insert(sout); return sout; } Why a reference return? t.insert(cout); cout << endl; // unnatural cout << t << endl; // natural

Auxiliary Extraction Operator // extracting a Rational istream& operator>>(istream &sin, Rational &r) { r.extract(sin); return sin; } Why a reference return? t.extract(cin); // unnatural cin >> t; // natural

What’s Happening Here? Suppose the following definitions are in effect Rational a(2,3); Rational b(3,4); Rational c(1,2); Why do the following statements work Rational s(a); Rational t = b; c = a C++ automatically provides a copy constructor and an assignment operator

Copy Construction Default copy construction Copy of one object to another in a bit-wise manner The representation of the source is copied to the target in a bit-by-bit manner This type of copy is called shallow copying Class developers are free to implement their own copy constructor Rational technically does need a one, but we will write one anyway. Always!!

A Rational Copy Constructor Rational::Rational(const Rational &r) { int a = r.getNumerator(); int b = r.getDenomiator(); setNumerator(a); setDenominator(b); } Rational s(a); Rational t = b; Show Me

Gang Of Three Also define Gang of three refers to the Assignment operator Must be a member function Copy source to target and return target A = B = C Destructor Clean up the object when it goes out of scope Gang of three refers to the Copy constructor, assignment operator, and the destructor

A Rational Assignment Operator Rational& Rational::operator=(const Rational &r) { int a = r.getNumerator(); int b = r.getDenomiator(); setNumerator(a); setDenominator(b); return *this; } a = b; a = b = c; *this is C++ syntax for the object whose member function was invoked Show Me

Lists Problem solving often requires information be viewed as a list List may be one- or multi-dimensional C++ provides two list mechanisms Arrays Traditional and important because of legacy libraries Restrictions on its use Linked List (COMS 262)

Array Terminology List is composed of elements Elements in a list have a common name The list as a whole is referenced through the common name List elements are of the same type — the base type Elements of a list are referenced by subscripting or indexing the common name

C++ Restrictions Subscripts are denoted as expressions within brackets: [ ] Base type can be any fundamental, library-defined, or programmer-defined type The index type is integer and the index range must be 0 ... n-1 where n is a programmer-defined constant expression

C++ Restrictions Parameter passing style Always call by reference (no indication necessary)

Basic Array Definition // Subscripts are 0 through 99

Example Definitions If const int N = 20; const int M = 40; const int MaxStringSize = 80; const int MaxListSize = 1000; Then the following are all correct array definitions int A[10]; // array of 10 ints char B[MaxStringSize]; // array of 80 chars double C[M*N]; // array of 800 floats int Values[MaxListSize]; // array of 1000 ints Rational D[N-15]; // array of 5 Rationals Show Me

Subscripting Let int A[10]; // array of 10 ints A[0], … A[9] To access individual element must apply a subscript to list name A A subscript is a bracketed expression also known as the index First element of list has index 0 A[0] Second element of list has index 1, and so on A[1]

Subscripting Last element has an index one less than the size of the list A[9] Incorrect indexing is a common error A[10] // does not exist

Array Elements Suppose int A[10]; // array of 10 uninitialized ints To access an individual element we must apply a subscript to list name A

Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

Array Element Manipulation Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

Extracting Values For A List int A[MaxListSize]; int n = 0; int CurrentInput; while((n < MaxListSize) && (cin >> CurrentInput)){ A[n] = CurrentInput; ++n; }

Displaying A List for (int i = 0; i < n; ++i) { } // List A of n elements has already been set for (int i = 0; i < n; ++i) { cout << A[i] << " "; } cout << endl;

Smallest Value Problem Input Output Note Find the smallest value in a list of integers Input A list of integers and a value indicating the number of integers Output Smallest value in the list Note List remains unchanged after finding the smallest value!

Preliminary Design Realizations Design When looking for value with distinguishing characteristics, need a way of remembering best candidate found so far Make it a function -- likely to be used often Design Search array looking for smallest value Use a loop to consider each element in turn If current element is smallest so far, then update smallest value so far candidate

Preliminary Design When done examining all of the elements, the smallest value seen so far is the smallest value

Necessary Information Information to be maintained Array with values to be inspected for smallest value Number of values in array Index of current element being considered Smallest value so far

A More Detailed Design Solution Function that takes array of values and array size as its two in parameters; returns smallest value seen as its value Initialize smallest value so far to first element

A More Detailed Design For each of the other elements in the array in turn If it is smaller than the smallest value so far, update the value of the smallest value so far to be the current element Return smallest value seen as value of function

Passing An Array int ListMinimum(const int A[], int asize) { assert(asize >= 1); int SmallestValueSoFar = A[0]; for (int i = 1; i < asize; ++i) { if (A[i] < SmallestValueSoFar ) { SmallestValueSoFar = A[i]; } return SmallestValueSoFar ; Notice brackets are empty Could we just assign a 0 and have it work?

Using ListMinimum() What happens with the following? int Number[6]; Number[0] = 3; Number[1] = 88; Number[2] = -7; Number[3] = 9; Number[4] = 1; Number[5] = 24; cout << ListMinimum(Number, 6) << endl; Notice no brackets

Using ListMinimum() int List[3]; List[0] = 9; List[1] = 12; cout << ListMinimum(List, 3) << endl;

Remember Arrays are always passed by reference Artifact of C Can use const if array elements are not to be modified Do not need to include the array size when defining an array parameter

Some Useful Functions void DisplayList(const int A[], int n) { for (int i = 0; i < n; ++i) { cout << A[i] << " "; } cout << endl; void GetList(int A[], int &n, int MaxN = 100) { for (n = 0; (n < MaxN) &&(cin >> A[n]); ++n){ continue;

Useful Functions Being Used const int MaxNumberValues = 25; int Values[MaxNumberValues]; int NumberValues; GetList(Values, NumberValues, MaxNumberValues); DisplayList(Values, NumberValues);

Searching Problem Does it matter if Determine whether a value key is one of the element values Does it matter if Element values are not necessarily numbers Element values are not necessarily unique Elements may have key values and other fields

Sequential List Searching int Search(const int List[], int m, int Key) { for (int i = 0; i < m; ++i) { if (List[i] == Key) { return i; } return m; Run time is proportional to number of elements

Example Invocation cin >> val; int spot = Search(Values, NumberValues, val); if (spot != NumberValues) { // its there, so display it cout << Values[spot] << endl; } else { // its not there, so add it Values[NumberValues] = val; ++NumberValues;

Sorting Problem Major tasks Arranging elements so that they are ordered according to some desired scheme Standard is non-decreasing order Why don't we say increasing order? Major tasks Comparisons of elements Updates or element movement

Common Sorting Techniques Selection sort On ith iteration place the ith smallest element in the ith list location Bubble sort Iteratively pass through the list and examining adjacent pairs of elements and if necessary swap them to put them in order. Repeat the process until no swaps are necessary

Common Sorting Techniques Insertion sort On ith iteration place the ith element with respect to the i-1 previous elements In text Quick sort Divide the list into sublists such that every element in the left sublist <= to every element in the right sublist. Repeat the Quick sort process on the sublists

SelectionSort void SelectionSort(int A[], int n) { for (int i = 0; i < n-1; ++i) { int k = i; for (int j = i + 1; j < n; ++j) { if (A[j] < A[k]) k = j; } if (i != k) swap(A[k], A[i]);

Complexity SelectionSort() Question General question How long does the function take to run Proportional to n*n time units, where n is the number of elements in the list General question How fast can we sort using the perfect comparison-based method The best possible worst case time is proportional to n log n time units