Discrete Structures Lecture 11

Similar documents
Programming II Test 1 Revision 1 CMSC 2613 Autumn 2003

CMSC 4023 Chapter 11

Programming II Test 3 Revision 1 CS 2613 Autumn 2003

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

COMP 2355 Introduction to Systems Programming

Due Date: See Blackboard

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Programming II Test 2 Revision 2 CMSC 2613 Autumn 2003

Linked List using a Sentinel

Programming II Lecture 2 Structures and Classes

Objects and streams and files CS427: Elements of Software Engineering

C++ For Science and Engineering Lecture 27

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

The number of nodes, N, in a binary tree of height, h is: 2 N 2 The height of a binary tree is: h =

Due Date: See Blackboard

Unified Modeling Language a case study

CPSC 427: Object-Oriented Programming

Due Date: See Blackboard

CE221 Programming in C++ Part 1 Introduction

AN OVERVIEW OF C++ 1

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Programming Languages Test 2 Revision 1 CMSC 4023 CRN Spring 2007

Due Date: See Blackboard

C++_ MARKS 40 MIN

File I/O Christian Schumacher, Info1 D-MAVT 2013

Review: C++ Basic Concepts. Dr. Yingwu Zhu

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Lab: Supplying Inputs to Programs

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

Interview Questions of C++

Due Date: See Blackboard

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

ADTs: Stacks and Queues

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Intermediate Programming, Spring 2017*

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

G52CPP C++ Programming Lecture 17

CSCE 110 PROGRAMMING FUNDAMENTALS

Program Organization and Comments

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CMSC 341 Lecture 7. Announcements. Proj 2 up Project Preview tonight and tomorrow

C++ Programming Fundamentals

BITG 1113: Files and Stream LECTURE 10

Lecture-5. STL Containers & Iterators

Programming in C/C Lecture 3

Stack memory - "scratch pad" memory that is used by automatic variables.

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

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

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

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

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

C++ STREAMS; INHERITANCE AS

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

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

Input and Output File (Files and Stream )

CSC 222: Computer Programming II. Spring 2004

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCI 104 Classes. Mark Redekopp David Kempe

Lab 6 Due Date: Wednesday, April 5, /usr/local/3302/include/direct linking loader.h Driver File:

G52CPP C++ Programming Lecture 16

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

What can go wrong in a Java program while running?

CSCI 104 Templates. Mark Redekopp David Kempe

September 19,

Chapter 2. Procedural Programming

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

Appendix M: Introduction to Microsoft Visual C Express Edition

Chapter 14 Sequential Access Files

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Exam duration: 3 hours Number of pages: 10

Object Oriented Programming COP3330 / CGS5409

CPSC 427: Object-Oriented Programming

Programming in C++: Assignment Week 8

Lab 1: First Steps in C++ - Eclipse

CS 103 Unit 14 - Streams

Strings and Stream I/O

Introduction to Programming using C++

Lab 2: ADT Design & Implementation

ECE 2036 Lab 1: Introduction to Software Objects

C++ Programming Fundamentals

Chapter 7: Stacks. Exercises 7.2

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Chapter 15 - C++ As A "Better C"

EECE.3220: Data Structures Spring 2017

Simple File I/O.

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

COMP6771 Advanced C++ Programming

Preview 8/28/2018. Review for COSC 120 (File Processing: Reading Data From a File)

Object Oriented Programming COP3330 / CGS5409

Scientific Computing

CPSC 427: Object-Oriented Programming

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

C++ basics Getting started with, and Data Types.

Due Date: See Blackboard

Transcription:

Lecture 11 Overview Programming Instructions 1. Create file p02.cpp that manages command line arguments and directs the process of creating a Cartesian product. 1.1. Start by entering the command line arguments program found lecture note 1 of CMSC 2613 Programming II http://cs2.uco.edu/~trt/cs2613/l001.pdf. 1.2. Modify the program so that it accepts three file names, two input file names and one output file name. 1.3. Make the manager function CartesianMgr as shown below. void CartesianMgr(istream& i1,istream& i2,ostream& o) { List L1(i1); L1.Print(o,"L1"); //Create List L1. Scan input file stream i1 into the list. //Print the list. List L2(i2); L2.Print(o,"L2"); // Create List L1. Scan input file stream i1 into the list. //Print the list. List C; //Create an empty list C for the Cartesian product. CartesianProduct(L1,L2,C); //Create the Cartesian product from lists L1 and L2. //Store the product in list C. C.Print(o,"C"); //Print the Cartesian product. 1.4. Create function CartesianProduct. void CartesianProduct(List& L1,List& L2,List& C) { for (L1.First();!L1.IsEol();L1.Next()) { for (L2.First();!L2.IsEol();L2.Next()) { C.Insert(Pair(L1.Member(),L2.Member())); 1.5. You will also need function Pair. string Pair(string x,string y){return "(" + x + "," + y + ")"; 1.6. File p02.cpp is organized as shown below. //File p02.cpp directs the process of creating a Cartesian product. //Author Identification block ---- //C++ include files and namespace std. #include <cstdlib> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <fstream> 1

Lecture 11 Overview using namespace std; //Application include files #include "List02.h" string Pair(string x,string y){ void CartesianProduct(List& L1,List& L2,List& C){ void CartesianMgr(istream& i1,istream& i2,ostream& o){ struct CommandLineException { ; struct FileException { ; int main(int argc, char* argv[]) { try { char ifn1[255],ifn2[255],ofn[255]; switch (argc) { case 1: case 2: //Prompt for 2 input file names and 1 output file name. //Copy input file name 1. Prompt for the input file name 2 and the //output file name. case 3: case 4: default: throw CommandLineException(3,argc-1); //end switch ifstream i1(ifn1); if (!i1) throw FileException(ifn1); ifstream i2(ifn2); if (!i2) throw FileException(ifn2); ofstream o(ofn); if (!o) throw FileException(ofn); CartesianMgr(i1,i2,o); o.close(); i1.close(); i2.close(); catch (... ) { return 0; //Read input file names 1 and 2. Prompt for the output file name. //Read input file names 1 and 2. Read the output file name. 2

Overview 2. Create file List02.h that contains class List. #ifndef List02 #define List02 1 //Author Identification Comment ---- //C++ include files and namespace std. #include <cstdlib> #include <cstring> #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; struct ListException { ; class List { int size; //Number of elements available in the list int count; //Actual number of elements occupied. //Index of the next available element string* L; //A pointer to a list of strings. int cursor; //Index of the current element public: List(int sz=100):size(sz),count(0),cursor(-1){l=new string[size]; List(istream& i,int sz=100):size(sz),count(0),cursor(0){l=new string[size];scan(i); ~List(){if (L) delete[] L; bool IsFull(void){ void Insert(string v){ void Scan(istream& i){ void Print(ostream& o,const char* title){ void First(void){cursor=0; void Next(void){if (cursor<count) cursor++; bool IsEol(void){return cursor>=count; string Member(void){return L[cursor]; ; #endif 3

Overview Submission Instructions 1. Create file p02.cpp in the root directory of your account for this class on the departmental computer cs.uco.edu. 2. Enter your author identification block consisting of 2.1. Your name, for example, Mr. Alan Turing 2.2. Your student identifier, for example, *00000000 2.3. You e-mail address, for example, aturing@uco.edu 2.4. This course Discrete Structures 2.5. The CRN, term, and year of the class in which you are enrolled, for example, 12599, Autumn, 2012 2.6. The project identifier p02 2.7. The date that the project is due, for example, October 10, 2012 2.8. The account number of the account that you were assigned tt000. 3. Design, code, and test your algorithm for producing the results of operations specified in the project assignment. 4. Download a copy of the Project Template (MS Word) from the class webpage http://cs2.uco.edu/~trt/cs2123.html changing the name of the file to crn-last-first-p02.docx where crn is the CRN of the class in which you are enrolled, last is your last name, and first is your first name. For example, if your name is Alan Turing and you are enrolled in CRN 12599, the name of the file you downloaded is 12599-Turing-Alan-p02.docx. 5. Complete the author identification block in the document you downloaded changing the name and other fields in the block to appropriate values. 6. Change the name in the header of the document to your name. 7. Create a new section and a new header in your document. 7.1. Select the Page Layout tab. 7.2. Select Breaks drop-down list. 7.3. Select the Next Page in the Section Breaks list. 7.4. In the Navigation Tab uncheck Link to Previous 7.5. Change Cover Page to File p02.cpp. 8. Employ WinSCP3 that can be obtained from Dr. McDaniel s web page to copy the source file p02.cpp from your account on the departmental computer to the PC having the document that you will submit to your instructor. 9. Insert File p02.cpp into the section having the header p02.cpp. 9.1. Select Insert. 9.2. In the Text tab, select the Object drop-down list. 9.3. Select Text from file. 9.4. Navigate to the folder containing file p02.cpp. 9.5. Select file p02.cpp to be inserted. 10. Submit the project to your instructor by sending the document to your instructor. The fields of your note contain: 10.1. To: trturner@uco.edu 10.2. Subject: crn-last-first-p02 where crn is the CRN of the class in which you are enrolled, last is your last name, and first is your first name. For example, if your name is Alan Turing and you are enrolled in CRN 12599, the name of the file you downloaded is 12599-Turing-Alan-p02 10.3. Attach the document containing your project template and source file. 4

File p02.cpp //File p02.cpp exercises class Stack // //Author: Ms. Petunia Perfect //Student ID: *00000001 //Course: - Discrete Structures //CRN: 11786, Autumn, 2014 //Project: p02 //Due: October 8, 2014 //Account: tt000 //Copyright October, 2014 by Ms. Petunia Perfect //Do not reproduce without permission from Ms. Petunia Perfect //C and C++ standard include files #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstring> #include <cstdlib> using namespace std; //Application include files #include "Stack02.h" -- //Function ReverseMgr scans the input stream i for strings, pushes them on the //Stack, then pops them from the stack in reverse order. -- void ReverseMgr(istream& i,ostream& o) { Stack S; for (;;) { string c; i >> c; if (i.eof()) o << " " << c; S.Push(c); o << endl; while (!S.IsEmpty()) o << " " << S.Pop(); o << endl; // 5

File p02.cpp //FileException is thrown when too many arguments appear on the command line. struct FileException { FileException(const char* fn) { cout << "File " << fn << "could not be opened."; ; // //CommandLineException is thrown when too many arguments appear //on the command line. // struct CommandLineException { CommandLineException(int m,int a) { cout << "Too many arguments on the command line."; cout << "A maximum of " << m << " arguments may appear on the command line."; cout << a << " arguments were on the command line."; ; // //Function main processes command line arguments main(int argc,char* argv[]) { try { char ifn[255],ofn[255]; switch (argc) { case 1: cout << "Enter the input file name. "; cin >> ifn; cout << "Enter the output file name. "; cin >> ofn; case 2: strcpy(ifn,argv[1]); cout << "Enter the output file name. "; cin >> ofn; case 3: strcpy(ifn,argv[1]); strcpy(ofn,argv[2]); default: throw CommandLineException(2,argc-1); ifstream i(ifn); if (!i) throw FileException(ifn); ofstream o(ofn); if (!o) throw FileException(ofn); 6

File p02.cpp ReverseMgr(i,o); o.close(); i.close(); catch (...) { cout << "Program terminated!"; cout << "I won't be back!"; exit(exit_failure); return 0; 7

File Stack02.h #ifndef Stack02_h #define Stack02_h 1 //-- //File Stack02.h defines class Stack. Class Stack is implemented //using a dynamically allocated array of strings. //Author: Ms. Petunia Perfect //E-Mail: pperfect@uco.edu //Student ID: *00000001 //Course: - Discrete Structures //CRN: 11786, Autumn, 2014 //Project: p02 //Due: October 8, 2014 //Account: tt000 //Copyright October, 2014 by Ms. Petunia Perfect. //Do not reproduce without permission from Ms. Petunia Perfect. // //C++ Standard include files // #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstring> #include <cstdlib> using namespace std; //class Stack contains the attributes and member functions of a //stack implemented using a dynamically allocated array class Stack { int size; int tos; string* S; public: Stack(int sz=100); ~Stack(); void Push(string v); bool IsFull(void); bool IsEmpty(void); string Pop(void); ; #endif //Number of available elements //Top Of Stack - index of the //element on top of //the stack //Points to an array of strings. //Constructor //Destructor //Push a string onto the stack //Is the stack full? //Is the stack empty? //Pop a string from the stack 8

File Stack02.cpp -- //File Stack02.cpp implements class Stack //Author: Ms. Petunia Perfect //Student ID: *00000001 //Course: - Discrete Structures //CRN: 11786, Autumn, 2014 //Project: p02 //Due: October 8, 2014 //Account: tt000 //Copyright October, 2014 by Ms. Petunia Perfect //Do not reproduce without permission from Ms. Petunia Perfect //C and C++ standard include files #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstring> #include <cstdlib> using namespace std; //Application include files #include "Stack02.h" //A StackException is thrown when the stack is empty or full. struct StackException { StackException(const char* m) { cout << "I am the stack and I am " << m << "."; ; Stack::Stack(int sz):size(sz),tos(-1){s=new string[size]; -- //Destructor 9

File Stack02.cpp -- Stack::~Stack(){if (S) delete[] S; -- //Member function Push -- void Stack::Push(string v) { if (IsFull()) throw StackException("full"); S[++tos]=v; -- //Member function Pop -- string Stack::Pop(void) { if (IsEmpty()) throw StackException("empty"); return S[tos--]; -- //Member function IsFull -- bool Stack::IsFull(void){return tos>=size-1; -- //Member function IsEmpty -- bool Stack::IsEmpty(void){return tos<0; 10

File p02make # # File p02make contains instructions that create executable file # p02. # # Author: Ms. Petunia Perfect # Student ID: *00000001 # E-Mail: pperfect@uco.edu # Course: - Discrete Structures # CRN: 11786, Autumn, 2014 # Project: p02 # Due: Ocotber 8, 2014 # # Object files (no colon) # obj = p02.o Stack02.o # # Bind (Link) the object files and create executable p02 # p02: ${obj g++ -o p02 ${obj -lm # # Compile p02.cpp that exercises class Stack. # p02.o: p02.cpp Stack02.h g++ -g -c p02.cpp # # Compile Stack02.cpp that implements class Stack. # Stack02.o: Stack02.cpp Stack02.h g++ -g -c Stack02.cpp 11

File i02.dat One, two, buckle my shoe. 12

File o02.dat One, two, buckle my shoe. shoe. my buckle two, One, 13