Download presentation
Presentation is loading. Please wait.
Published byAlannah Rose Modified over 9 years ago
2
Data Structures and Algorithms
3
What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate data. So, all programs represent data in some way. Data manipulation requires an algorithm.
4
What The Course Is About Algorithm design methods needed to develop programs that do the data manipulation. The study of data structures and algorithms is fundamental to Computer Science.
5
Software Design Process
6
Programming Life Cycle Activities Problem analysisunderstand the problem Requirements definition specify what program will do High- and low-level designhow it meets requirements Implementation of designcode it Testing and verificationdetect errors, show correct Deliveryturn over to customer Operationuse the program Maintenancechange the program
7
An Algorithm Is... A logical sequence of discrete steps that describes a complete solution to a given problem computable in a finite amount of time.
8
Goals of Quality Software It works. It can be read and understood. It can be modified. It is completed on time and within budget.
9
Specification: Understanding the Problem Detailed Program Specification Tells what the program must do, but not how it does it. Is written documentation about the program.
10
Writing Detailed Specifications Detailed Program Specification Includes: Inputs Outputs Processing requirements Assumptions
11
Abstraction A model of a complex system that includes only the details essential to the perspective of the viewer of the system.
12
Information Hiding Hiding the details of a function or data structure with the goal of controlling access to the details of a module or structure. PURPOSE: To prevent high-level designs from depending on low-level design details that may be changed.
13
Two Approaches to Building Manageable Modules Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded. Identifies various objects composed of data and operations, that can be used together to solve the problem. FUNCTIONAL DECOMPOSITION OBJECT-ORIENTED DESIGN FOCUS ON: processes FOCUS ON: data objects
14
Find Weighted Average Print Weighted Average Functional Design Modules Main Print Data Print Heading Get Data Prepare File for Reading
15
Object-Oriented Design A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.
17
More about OOD l Languages supporting OOD include: C++, Java, … l A class is a programmer-defined data type and objects are variables of that type. l In C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream.h and fstream.h contain definitions of stream classes.
18
Procedural vs. Object-Oriented Code “Read the specification of the software you want to build. Underline the verbs if you are after procedural code, the nouns if you aim for an object-oriented program.” Brady Gooch, “What is and Isn’t Object Oriented Design,” 1989.
19
Verification of Software Correctness Testing Debugging Program verification
20
Program Verification is the process of determining the degree to which a software product fulfills its specifications. Program Verification PROGRAM SPECIFICATIONS Inputs Outputs Processing Requirements Assumptions
21
Program Testing Testing is the process of executing a program with various data sets designed to discover errors. DATA SET 1 DATA SET 2 DATA SET 3 DATA SET 4...
22
Origin of Bugs Various Types of Errors: Design errors occur when specifications are wrong Compile errors occur when syntax is wrong Run-time errors result from incorrect assumptions, incomplete understanding of the programming language, or unanticipated user errors.
23
22 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation
24
23 1. What do we mean by data? 2. Data abstraction and encapsulation 3. Data structure 4. Abstract data type operator categories Different Views of Data
25
24 2. Data Abstraction Separation of a data type’s logical properties from its implementation. LOGICAL PROPERTIESIMPLEMENTATION What are the possible values? How can this be done in C++? What operations will be needed?
26
25 APPLICATION 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 REPRESENTATION Data Encapsulation is the separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding. int y; y = 25;
27
26 Encapsulated C++ Data Type int Value range: INT_MIN.. INT_MAX Operations: + prefix - prefix + infix - infix * infix / infix % infix Relational Operators infix TYPE int (inside) Representation of int as 16 bits two’s complement + Implementation of Operations
28
27 Abstract Data Type (ADT) A data type whose properties (domain and operations) are specified independently of any particular implementation.
29
28 3. Data Structures Defined by the logical arrangement of data elements the set of operations we need to access the elements
30
29 Application (or user) level: modeling real-life data in a specific context. Logical (or ADT) level: abstract view of the domain and operations. WHAT Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW Data from 3 different levels
31
30 Viewing a library from 3 different levels Application (or user) level: Library of Congress. Logical (or ADT) level: domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book. Implementation level: representation of the structure to hold the “books”, and the coding for operations.
33
4. ADT Operator Categories Constructor -- creates a new instance (object) of an ADT. Transformer -- changes the state of one or more of the data values of an instance. Observer -- allows us to observe the state of one or more of the data values without changing them. Iterator -- allows us to process all the components in a data structure sequentially. 32
34
Strings are lists of characters; Many languages have strings as a built-in data type. The C++ standard library provides a string class, whose declarations are available in the header file. – The operations given in the string class include concatenation of two strings using the + operator, searching a string for a substring, determining the number of characters in a string, and some input/output operations. – C++ also inherits a primitive string type from C, which is simply an array of type char, with the null character (\0) used to signal the end of the string. – C++ also inherits from C a set of string-handling functions in
35
Let’s create our own abstract data type String that has general-purpose input and output functions and encapsulate it into a class. We will call our class StrType so as not to confuse it with the library class string.
36
String ADT Specification Structure: A sequence of alphanumeric characters.
37
Operations: MakeEmpty – Function: Initializes string to empty. – Precondition: None. – Postcondition: String is empty.
38
GetStringFile(Boolean skip, InType charsAllowed, ifstream& inFile) Function: Gets a string from the file InFile. Assumptions: (1) InType is a data type consisting of the following constants: – ALPHA: Only alphabetic characters are stored. – ALPHA_NUM: Only alphanumeric characters are stored. – NON_WHITE: All nonwhitespace characters are stored. – NOT_NEW: All characters excluding the newline character are stored. (2) If skip is true, characters not allowed are skipped until the first allowed character is found. Reading and storing begins with this character and continues until a character not allowed is encountered. That character is read but not stored. If skip is false, reading and storing begins with the current character in the stream.
39
Precondition: inFile has been opened Postconditions: If no allowable characters are found, the empty string is returned; else, a string has been input according to the skip and charsAllowed parameters. inFile is left positioned following the last character read.
40
GetString(Boolean skip, InType charsAllowed) – Function: Gets a string from the standard input stream. – Assumptions: Same as those for GetStringFile. – Precondition: None. – Postconditions: String has been input according to the skip and charsAllowed parameters. Input stream is left positioned following the last character read, which is a nonallowed character.
41
PrintToScreen(Boolean newLine) – Function: Prints an end-of-line character if needed before printing the string to the screen. – Precondition: String has been initialized. – Postcondition: If newLine is true, string has been printed on the screen starting on a new line; otherwise, string has been printed on the current line.
42
PrintToFile(Boolean newLine, ofstream& outFile) – Function: Prints an end-of-line character if needed before – printing the string to outFile. – Preconditions: String has been initialized. – outFile has been opened. – Postconditions: If newLine is true, string has been printed on outFile starting on a new line; otherwise, string has been printed on the current line of outFile. outFile is left positioned after the last character written.
43
int LengthIs() – Function: Returns the number of characters in the string. – Precondition: String has been initialized. – Postcondition: Function value = number of characters in the string.
44
CopyString(StrType& newString) – Function: Copies self into newString. – Precondition: Self has been initialized. – Postcondition: Self has been copied into newString.
45
Application Level #include #include "StrType.h" #include const int MAX_WORDS = 10; int main() { using namespace std; StrType word; ifstream inFile; StrType words[MAX_WORDS]; int numWords = 0; inFile.open("words.in");
46
word.MakeEmpty(); word.GetStringFile(true, ALPHA_NUM, inFile); while (inFile && numWords < MAX_WORDS) { word.CopyString(words[numWords]); numWords++; word.GetStringFile(true, ALPHA_NUM, inFile); } if (inFile) cout << "First " << MAX_WORDS << " words on the file: "; else cout << " Words on the file: "; for (int index = 0; index < numWords; index++) words[index].PrintToScreen(true); return 0; }
47
Implementation Level Now we must determine how we will represent our strings. – Recall that C++ implements strings as one- dimensional char arrays with the null character (\0) signaling the end of the string. – Another way of implementing a string would be a struct or a class with two data members: an array of type char and an integer variable representing the length. The string characters would be located between position 0 and position length – 1 in the array. Which approach shall we use?
48
The amount of storage required for both array-based designs is nearly the same, and the amount of processing is approximately the same although the algorithms differ somewhat. Let’s use the null-terminated method here.
49
To accommodate the null character, we must remember to allocate one more position than the maximum number of characters expected.
50
The maximum number of characters—where does this number come from? Nothing in the specification hints at a limit on the number of characters allowed in a string, but our array- based implementation requires us to specify an array size. Let’s arbitrarily choose a reasonably large number—say, 100—for the maximum string length. – In the specification file, StrType.h, we define a constant MAX_CHARS to be 100, letting the user know that it is the maximum length allowed.
51
– Should the client be responsible for making sure that the string is within the allowable length, – or should the code of GetString and GetStringFile check for this problem and discard any characters that cannot be stored? Both approaches have merit. We choose the latter and do the checking within StrType. The specifications need to be changed to reflect this decision.
52
// Header file for class StrType, a specification for the // String ADT #include const int MAX_CHARS = 100; enum InType {ALPHA_NUM, ALPHA, NON_WHITE, NOT_NEW};
53
class StrType { public: // Assumptions: // InType is a data type consisting of the following constants: // ALPHA: only alphabetic characters are stored; // ALPHA_NUM: only alphanumeric characters are stored; // NON_WHITE: all nonwhitespace characters are stored; // NOT_NEW: all characters excluding the newline character // are stored. // If skip is true, characters not allowed are skipped until the // first allowed character is found. Reading and storing // begins with this character and continues until a character // not allowed is encountered. This character is read but not // stored. If skip is false, reading and storing begins with // the current character in the stream.
54
void MakeEmpty(); void GetString(bool skip, InType charsAllowed); // Post: If the number of allowable characters exceeds // MAX_CHARS, the remaining allowable characters have // been read and discarded.
55
void GetStringFile(bool skip, InType charsAllowed, std::ifstream& inFile); // Post: If the number of allowable characters exceeds // MAX_CHARS, the remaining allowable characters have been // read and discarded. void PrintToScreen(bool newLine); void PrintToFile(bool newLine, std::ofstream& outFile); int LengthIs(); void CopyString(StrType& newString); private: char letters[MAX_CHARS + 1]; };
56
void StrType::MakeEmpty() // Post: letters is empty string. { letters[0] = '\0'; }
57
GetStringFile(Boolean skip, InType charsAllowed, ifstream& inFile) #include // Prototypes of auxiliary functions. /* Note: If skip is true, nonallowable leading characters are skipped. If end-of- file is encountered while skipping characters, the empty string is returned. If the number of allowable characters exceeds MAX_CHARS, the rest are read and discarded.*/ void GetAlphaNum(bool skip, char letters[], std::ifstream& inFile); // Post: letters array contains only alphanumeric characters. void GetAlpha(bool skip, char letters[], std::ifstream& inFile); // Post: letters array contains only alphabetic characters. void GetNonWhite(bool skip, char letters[], std::ifstream& inFile); // Post: letters array contains only nonwhitespace characters. void GetTilNew(bool skip, char letters[], std::ifstream& inFile); // Post: letters array contains everything up to newline character
58
void StrType::GetStringFile(bool skip, InType charsAllowed, std::ifstream& inFile) { switch (charsAllowed) { case ALPHA_NUM : GetAlphaNum(skip, letters, inFile); break; case ALPHA : GetAlpha(skip, letters, inFile); break; case NON_WHITE : GetNonWhite(skip, letters, inFile); break; case NOT_NEW : GetTilNew(skip, letters, inFile); break; }
59
We can use the functions available in to control our reading in each of the functions. If charsAllowed is ALPHA_NUM, we skip characters until the function isalnum returns true, and store them until isalnum returns false or inFile goes into the fail state. If charsAllowed is ALPHA, we skip characters until the function isalpha returns true, and store them until isalpha returns false or inFile goes into the fail state. If charsAllowed is NON_WHITE, we skip characters until the function isspace returns false, and store them until isspace returns true or inFile goes into the fail state. If charsAllowed is NOT_NEW, we skip characters until the character is not '\n', and store them until the character is '\n' or inFile goes into the fail state.
60
void GetAlphaNum(bool skip, char letters[], std::ifstream& inFile) { using namespace std; char letter; int count = 0; if (skip) {// Skip non-alphanumeric characters. inFile.get(letter); while (!isalnum(letter) && inFile) inFile.get(letter); } else inFile.get(letter);
61
if (!inFile || !isalnum(letter)) // No legal character found; empty string returned. letters[0] = '\0'; else {// Read and collect characters. do { letters[count] = letter; count++; inFile.get(letter); } while (isalnum(letter) && inFile && (count < MAX_CHARS));
62
letters[count] = '\0'; // Skip extra characters if necessary. if (count == MAX_CHARS && isalnum(letter)) do { inFile.get(letter); } while (isalnum(letter) && inFile); }
63
void GetTilNew(bool skip, char letters[], std::ifstream& inFile) { using namespace std; char letter; int count = 0; if (skip) {// Skip newlines. inFile.get(letter); while ((letter == '\n') &&inFile) inFile.get(letter); } else inFile.get(letter);
64
if (!inFile || letter == '\n') letters[0] = '\0'; else {// Read and collect characters. do { letters[count] = letter; count++; inFile.get(letter); } while ((letter != '\n') && inFile && (count < MAX_CHARS)); letters[count] = '\0'; // Skip extra characters if necessary. if (count == MAX_CHARS && letter != '\n') do { inFile.get(letter); } while ((letter != '\n') && inFile); }
65
GetString This operation is nearly identical to GetStringFile, with inFile changed to cin.
66
void StrType::PrintToScreen(bool newLine) // Post: letters has been sent to the output stream. { using namespace std; if (newLine) cout << endl; cout << letters; }
67
PrintToFile is nearly identical to PrintToScreen, with cout replaced with outFile.
68
#include void StrType::CopyString(StrType& newString) // Post: letters has been copied into newString.letters. { std::strcpy(newString.letters, letters); } int StrType::LengthIs() // Post: Function value = length of letters string { return std::strlen(letters); }
69
Test Plan
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.