CSCE 110 PROGRAMMING FUNDAMENTALS

Similar documents
CSCE 110 PROGRAMMING FUNDAMENTALS

EECE.3220: Data Structures Spring 2017

Lab 2: ADT Design & Implementation

Linked List using a Sentinel

CSCE 110 PROGRAMMING FUNDAMENTALS

AN OVERVIEW OF C++ 1

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Data Structures and Algorithms

CSCE 110 PROGRAMMING FUNDAMENTALS

การทดลองท 8_2 Editor Buffer Array Implementation

Example Final Questions Instructions

Implementing an ADT with a Class

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CSCE 110 PROGRAMMING FUNDAMENTALS

Function Overloading

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Intermediate Programming, Spring 2017*

Chapter 17: Linked Lists

Lecture 7. Log into Linux New documents posted to course webpage

Example Final Questions Instructions

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

PIC 10A. Lecture 17: Classes III, overloading

Programming Language. Functions. Eng. Anis Nazer First Semester

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

CMSC 202 Midterm Exam 1 Fall 2015

Chapter 17: Linked Lists

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CSCE 110 PROGRAMMING FUNDAMENTALS

Object Oriented Design

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Due Date: See Blackboard

Fast Introduction to Object Oriented Programming and C++

CSI33 Data Structures

2. It is possible for a structure variable to be a member of another structure variable.

Object Oriented Design

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CS242 COMPUTER PROGRAMMING

Object-Oriented Programming in C++

III. Classes (Chap. 3)

CpSc212 Goddard Notes Chapter 10. Linked Lists

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

An Introduction to Queues With Examples in C++

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

C++_ MARKS 40 MIN

Faculty of Information and Communication Technologies

Due Date: See Blackboard

Due Date: See Blackboard

Introduction to Programming using C++

Object oriented programming

CSC1322 Object-Oriented Programming Concepts

Inheritance, and Polymorphism.

Lecture 23: Pointer Arithmetic

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Due Date: See Blackboard. {a n+1 b 2n n 0}

Due Date: See Blackboard

Unified Modeling Language a case study

True or False (12 Points)

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Operator Overloading

Introduction to Classes

CSI33 Data Structures

Introduction to Programming session 24

19.1 The Standard Template Library

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

ADTs in C++ In C++, member functions can be defined as part of a struct

Object Oriented Design

Review Questions for Final Exam

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CMSC 4023 Chapter 11

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

CPSC 427: Object-Oriented Programming

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

C++ For Science and Engineering Lecture 27

Classes: A Deeper Look

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

Introduction to C++ Systems Programming

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Name Section: M/W or T/TH. True or False (14 Points)

Dynamic Data Structures

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Program template-smart-pointers-again.cc

C++ Programming Assignment 3

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

Queue Implementations

Short Notes of CS201

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

Programming in C++: Assignment Week 8

CS250 Final Review Questions

Object Oriented Programming COP3330 / CGS5409

CS 376b Computer Vision

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Transcription:

CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1

Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2

A Key Table Class The Key Table as a Dictionary ADT Key Table Key Table Data Members Key Table Operations Class Template Element Specification Key Table Class Definition Key Table Class Implementation Example Application Prof. Amr Goneid, AUC 3

1. The Key Table as a Dictionary A key table is essentially a Dictionary ADT. A form of container that permits access by content. An element (e) has a key part (k) Supports the following main operations: Insert (D, e): Insert item e in dictionary D Delete (D, e): Delete item e from D Search (D, k): search for key k in D Prof. amr Goneid, AUC 4

2. ADT Key Table Key Table: a linear configuration of elements in which we can insert and delete such elements. It also supports search by content (key), and can represent a dictionary ADT. Element: contains one key and associated data item Current Element: special element in the table, indicated by a pointer to the current position. Prof. Amr Goneid, AUC 5

ADT Key Table We will construct a class Ktable whose objects are key tables. A key table will be implemented as a dynamic array. The data members will be the elements and a pointer (index) to these elements. An element contains a key field and a data field. Search is by content (key) The table will have a default maximum size MaxSize of 128 elements. The application program can specify other sizes since the table is dynamic. The application program will also specify the actual types for the key and data fields. Prof. Amr Goneid, AUC 6

3. Key Table Data Members Elements. Each element has: 1. A key of type keytype 2. Data or information field of type datatype Others: T, a pointer to a dynamic array of elements; P, a pointer (index) to the current element; MaxSize, The maximum size (Capacity) of the table N, index of the last element in the table ( N < 0 if the table is empty, and N = Maxsize 1 if the table is full). Prof. Amr Goneid, AUC 7

4. Key Table Operations construct: Create table destruct: Destroy table tableisempty bool : return True if table is empty tableisfull bool : return True if table is full occupancy int : return the current no. of elements in the table Prof. Amr Goneid, AUC 8

Key Table Operations update (d) : to update the data portion of the current element to contain d; assume the current position is nonempty. retrieve (d): to return the data (d) in the current element; assume the current position is nonempty. delete: delete the current element. Assume the current position is nonempty initially. Prof. Amr Goneid, AUC 9

Key Table Operations search (k) bool : Search the table for the slot with key part that matches (k). If found, set p to the slot and return True, else return false insert (k,d) : insert an element at the end of the table. traverse: traverse table to print key and data fields. Prof. Amr Goneid, AUC 10

5. Class Template We will allow the class to receive the type of key and type of data stored in the class via parameters. This is called a class template We achieve this by doing the following: In the header file, declare the template class with its parameters, e.g., template <class keytype, class datatype> class KTable {.. }; Prof. amr Goneid, AUC 11

Class Template In the implementation file, every function is preceded by the class template. In the implementation file, the class name in a function header, is succeeded by the template < >. Example: // return True if table is full template <class keytype, class datatype> bool Ktable <keytype, datatype>::tableisfull() const { return (N == MaxSize-1); } Prof. amr Goneid, AUC 12

6. Element Specification // The element structure can be specified as a Class // in the private part of the main Ktable class. class element // Hidden from user { public: keytype key; // key datatype data; // Data }; // end of class element declaration Prof. Amr Goneid, AUC 13

7. Key Table Class Definition // File: Ktable.h // Definition of Ktable Template Class #ifndef KTABLE_H #define KTABLE_H // Specification of the class template <class keytype, class datatype> class Ktable { public: Prof. Amr Goneid, AUC 14

Key Table Class Definition // Member Functions Ktable(int nelements = 128); ~Ktable(); // Functions Prototype Definitions bool tableisempty() const; bool tableisfull() const; int occupancy() const; void update (const datatype & ); void retrieve (datatype &) const; void delete (); // Constructor // Destructor Prof. Amr Goneid, AUC 15

Key Table Class Definition bool search (const keytype & ); bool insert (const keytype &, const datatype & ); void traverse () const; private: // Element Class class element { public: keytype key; // key datatype data; // Data }; // end of class element declaration Prof. Amr Goneid, AUC 16

Key Table Class Definition element *T; int p; // Pointer to Storage Array // Pointer to current element // Maximum and index of last element int MaxSize, N; }; // end of Ktable Class definition #endif // KTABLE_H #include "Ktable.cpp" Prof. Amr Goneid, AUC 17

8. Key Table Class Implementation // File:Ktable.cpp Class implementation file #include <iostream> using namespace std; // Constructor with argument, size is nelements, default is 128 template <class keytype, class datatype> Ktable <keytype, datatype> ::Ktable(int nelements) { MaxSize = nelements; T = new element[maxsize]; p = -1; N = -1; } Prof. Amr Goneid, AUC 18

Key Table Class Implementation // Destructor template <class keytype, class datatype> Ktable <keytype, datatype> ::~Ktable() { delete [ ] T; } // return True if table is empty template <class keytype, class datatype> bool Ktable <keytype, datatype> ::tableisempty() const { return (N < 0); } Prof. Amr Goneid, AUC 19

Key Table Class Implementation // return True if table is full template <class keytype, class datatype> bool Ktable <keytype, datatype> ::tableisfull() const { return (N == MaxSize - 1); } // return the current occupancy of the table template <class keytype, class datatype> int Ktable <keytype, datatype> ::occupancy() const { return (N+1); } Prof. Amr Goneid, AUC 20

Key Table Class Implementation // to update the data in the current position template <class keytype, class datatype> void Ktable <keytype, datatype>::update (const datatype &d) { if ((p >= 0)&&(p <= N)) T[p].data = d; } // Retrieve the data part of the current position template <class keytype, class datatype> void Ktable<keyType, datatype>::retrieve (datatype &d) const { if ((p >= 0)&&(p <= N)) d = T[p].data; } Prof. Amr Goneid, AUC 21

Key Table Class Implementation // delete the current element. // After deletion, current element becomes empty template <class keytype, class datatype> void Ktable <keytype, datatype> ::delete () { if ((p >= 0)&&(p <= N)) { if (p < N) for (int i = p; i < N; i++) T[i] = T[i + 1]; N--; p = -1; } } Prof. Amr Goneid, AUC 22

Key Table Class Implementation // search the table for the element with key (k). // If found, set p to it and return True, else return false template <class keytype, class datatype> bool Ktable <keytype, datatype> ::search (const keytype &k) { bool found = false; p = -1; if(!tableisempty()) { i = 0; while ((! found) && (i <= N)) if (k == T[i].key) { found = true; p = i ; } else i++; } return found; } Prof. Amr Goneid, AUC 23

Key Table Class Implementation // insert element at the end of the table template <class keytype, class datatype> bool Ktable <keytype, datatype> :: insert (const keytype &k, const datatype &d) { if (!tableisfull()) { N++; T[N].key = k; T[N].data = d; return true ; } else return false; } Prof. Amr Goneid, AUC 24

Key Table Class Implementation // traverse table to print key and data fields template <class keytype, class datatype> void Ktable <keytype, datatype> ::traverse () const { for(int i = 0; i <= N; i++) cout << T[i].key << " " << T[i].data << endl; } Prof. Amr Goneid, AUC 25

9. Example Application Build a list of characters and their frequencies in a string: Given a string, search for every character in a key table. If a character does not exist in the table, insert it with a count of 1, otherwise, increment its count. Prof. Amr Goneid, AUC 26

Character Frequency Table // File: KTabletest.cpp // Applies Ktable Class #include <iostream> #include <string> using namespace std; #include "Ktable.h int main() { Ktable <char, int> ctable (50); string s; char c; int i, count; bool keyfound; Prof. Amr Goneid, AUC 27

Character Frequency Table // Read a string cout << "Enter a string:" << endl; getline(cin,s); cout << s << endl; // display it for (i = 0; i < s.length(); i++) // for every character { c = toupper(s.at(i)); // Search for character in the table keyfound = ctable.search (c); Prof. Amr Goneid, AUC 28

Character Frequency Table if (keyfound) // if found { ctable.retrieve (count); // get data count++; // increment count ctable.update (count); // store back } // Not found, a new element is inserted else ctable.insert(c,1); } Prof. Amr Goneid, AUC 29

Character Frequency Table } // print characters and their frequencies ctable.traverse(); // current table size cout << ctable,occupancy() << endl; // Free Memory ctable. ~Ktable(); return 0; Prof. Amr Goneid, AUC 30

Sample Output Enter a string: The New View The New View T 1 H 1 E 3 2 N 1 W 2 V 1 I 1 8 Press any key to continue Prof. Amr Goneid, AUC 31