This is an individual assignment and carries 100% of the final CPS 1000 grade.

Similar documents
STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

12 Abstract Data Types

Stack Abstract Data Type

March 13/2003 Jayakanth Srinivasan,

Project 1: Implementation of the Stack ADT and Its Application

Lab 7 1 Due Thu., 6 Apr. 2017

STACKS AND QUEUES. Problem Solving with Computers-II

CSE 230 Intermediate Programming in C and C++

Stack and Its Implementation

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

infix expressions (review)

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Assessment of Programming Skills of First Year CS Students: Problem Set

Lecture Data Structure Stack

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Formal Languages and Automata Theory, SS Project (due Week 14)

Lecture 12 ADTs and Stacks

DATA STRUCTURES AND ALGORITHMS

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 211 Programming Practicum Spring 2017

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks Calculator Application. Alexandra Stefan

A Simple Syntax-Directed Translator

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CSE 214 Computer Science II Stack

Online Expenses User Guide System Provided by Software Europe

Stacks and their Applications

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Problem with Scanning an Infix Expression

Stacks. Revised based on textbook author s notes.

CS 211 Programming Practicum Spring 2018

CS 211 Programming Practicum Fall 2018

[CS302-Data Structures] Homework 2: Stacks

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Stating the obvious, people and computers do not speak the same language.

Lecture 4 Stack and Queue

CS 206 Introduction to Computer Science II

CS2383 Programming Assignment 3

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Warm-up and Memoization

1. Stack Implementation Using 1D Array

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

EECS 311: Data Structures and Data Management Program 1 Assigned: 10/21/10 Checkpoint: 11/2/10; Due: 11/9/10

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Lecture 6 Sequences II. Parallel and Sequential Data Structures and Algorithms, (Fall 2013) Lectured by Danny Sleator 12 September 2013

Computer Systems and -architecture

The Stack and Queue Types

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

PA3 Design Specification

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

SFU CMPT 379 Compilers Spring 2018 Milestone 1. Milestone due Friday, January 26, by 11:59 pm.

Remaining Enhanced Labs

CS W3134: Data Structures in Java

An Introduction to Trees

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Draw a diagram of an empty circular queue and describe it to the reader.

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

CISC

Problem with Scanning an Infix Expression

In this chapter you ll learn:

Student Number: Lab day:

CS 406/534 Compiler Construction Putting It All Together

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)

Worksheet 3: Predictive Text Entry

Lecture No.04. Data Structures

Special Section: Building Your Own Compiler

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Data Structures Week #3. Stacks

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

CS 206 Introduction to Computer Science II

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Programming Model 2 A. Introduction

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Postfix (and prefix) notation

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Stacks, Queues (cont d)

3137 Data Structures and Algorithms in C++

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

PESIT Bangalore South Campus Department of MCA Course Information for

Data Structures and Algorithms Winter Semester

Stacks Fall 2018 Margaret Reid-Miller

Transcription:

CPS 1000 Assignment This is an individual assignment and carries 100% of the final CPS 1000 grade. Important instructions (read carefully and thoroughly): The firm submission deadline is Friday 5 th February 2016. Both a hard-copy of the report and the accompanying digital media should be handed in at Ms Vanessa Borg's office - FICT building, Level 1, Block B, Room 3. Make sure to consult the student hours in order not to miss the deadline if submitting on the final day. A backup submission, that includes a soft-copy of the report and all files submitted on digital media, should be uploaded to the VLE by the same deadline. All files must be archived into a single.zip or.tar.gzip file. You are to allocate 80 hours for this assignment. Students that do no carry out lab exercises should expect the actual time taken to complete this assignment to be considerably longer. This assignment consists of multiple tasks. Individual tasks will be introduced in class whenever sufficient content to carry them out has been duly covered. The first task will be introduced early November. Only students that regularly attend lab sessions are entitled to assistance from lab tutors. Tutors are strictly not allowed to spell out solutions, rather they will be pointing you towards theory/lab exercises to assist you in carrying out assignment tasks. You will also be given continuous hints about how best to carry out the assignment tasks during lectures. The first page in your assignment report must be the title of your assignment clearly showing your name, surname and study unit code. The second page must the table of contents. The report organization should follow the task sequence described in the following pages. The majority of the marks will be assigned for proper code explanations rather than for functional binaries. Therefore all source code must be properly presented in the report along with the necessary code fragment explanations. The full source code must also be supplied on an accompanying digital medium along with the makefiles for each task. A readme.txt file must be included in the root folder describing the content's organization. Reports that are difficult to follow due to low quality in the writing-style/organization/presentation will be penalized. Page 1 of 5

Task 1: Problem solving (Total: 50 marks) Warming up 1a. Write a program that requests the hours worked in a week and then prints the gross pay, the taxes, and the net pay. Assume the following: i. Basic pay rate = Eur 22.50/hr ; ii. Overtime (in excess of 40 hours) is paid at x1.5 the basic pay; iii. Tax rate: 0% of the first Eur 300, 15% of the next Eur 180, 29% of the rest. Given frequent tax law changes the source code must be written with ease of maintenance in mind. 1b. Write a program that reads input as a stream of characters until encountering EOF. Have it report the average number of letters per word as well as the ratio of upper:lowercase letters for the entire stream. You don t count whitespace or punctuation as being letters in a word. 1c. Write a function to_base_n(x,n) that transforms x to its base-n equivalent; where x is a decimal number and n is a decimal expressible as 2 y. 1d. Write a function str_reverse() that reverses any string passed to it as argument. Sort algorithms (4 marks each) 1e. Write a function, naive_sort() that takes as a parameter an array of randomly generated integers and returns them sorted. Research and use the insertion sort method, an intuitive sorting algorithm which however scales badly with the size of the input array. 1f. An improvement on the previous sort function is to use quick sort instead, which takes a divideand-conquer approach to the sorting problem by splitting up the array into smaller re-organized chunks and passes them individually to the same routine. Research this sort algorithm and use it to implement the smarter_sort() function. 1g. Write a single test application for both the above functions, taking care of generating the random array content as well as verifying their correct operation. 1h. Enhance the previous test application with a performance profiling routine that generates varying input array sizes of 10, 100, 1000, 5000, and 10000, timing the running times for both functions in each case. Plot a single graph comparing the running times for both functions. Classic puzzles re-visited (4 marks each) 1i. Passengers making use of the Maltese public transport service (tal-linja) look forward to kill journey times by counting stray cats observed through the bus windows. Because of this, tal-linja takes a keen interest in the health of the local stray cat population and decides to take a tally of the number of cats observed between each pair of stops as reported by passengers to their employees stationed at each stop. Unfortunately, the job of computing the grand total per journey is complicated by a primitive communication system where each bus stop employee can call only its immediate neighboring bus stop as opposed to all employees contacting the employee at the final stop stationed at the bus terminus. The implication is that the running total must be communicated all along the way. Write a program that simulates such a communication sequence as initiated at the final bus stop, using recursion. Passenger reports can be either be prefixed inside an array or simply ask the user to simulate the reported counts. (8 marks) Page 2 of 5

1j. As the programmer of a vending machine controller your are required to compute the minimum number of coins that make up the required change to give back to customers. An efficient solution to this problem takes a dynamic programming approach, starting off computing the number of coins required for a 1 cent change, then for 2 cents, then for 3 cents, until reaching the required change and each time making use of the prior computed number of coins. Write a program containing the function ComputeChange(), that takes a list of valid coins and the required change. This program should repeatedly ask for the required change from the console and call ComputeChange() accordingly. It should also make use of caching, where any previously computed intermediate values are retained for subsequent look-up. Task 2: Data structures (Total: 20 marks) The stack data structure (see Figure 1) is a container data structure that stores a collection of data items where the last added item is the first to be removed (LIFO last in, first out). Adding items to the stack is referred to as a push operation, whilst removing the last-added item is referred to as a pop operation. Figure 1: The stack data structure A stack machine is a real or emulated computing machine characterized by how it evaluates expressions. Focusing on arithmetic expressions, a stack machine would take the infix expression (4+(5*6)) and first converts it to its postfix equivalent 4 5 6 * +, precisely denoting that the multiplication sub-expression is to be evaluated before addition. Similarly ((4+5)*6) would be converted to 4 5 + 6 *, where this time it is the addition sub-expression that will be evaluated first. Once in postfix notation a stack machine evaluates expressions by pushing operands on the stack, scanned left-right, and evaluating each sub-expression as soon as an operator is encountered. Subexpression evaluation proceeds by popping the required number of operands off the stack and pushing back the result, as per example shown in Figure 2. Page 3 of 5

Figure 2: Stack machine computation 2a. Make use of a stack data structure implemented as an array in order to validate input infix expressions in terms of balanced parenthesis e.g. ( ( ) ) and not ) ( ( ). Balanced parenthesis can be validated by: Scanning input express left-right, Pushing all left parenthesis on the stack, and Matching each right parenthesis with the top of the stack, popping the latter accordingly in the case of a left-right parenthesis match. Balanced expressions terminate with an empty stack, whilst unbalanced ones either leave extra parenthesis on the stack or encounter an empty stack when parsing a right parenthesis. 2b. Make use of a stack data structure implemented as an array in order to convert infix expressions to postfix. Only consider arithmetic operations involving + * / (all binary). The conversion essentially involves shifting operators out of their immediate enclosing parenthesis. In the case of ((4+5)*6), the + is shifted from (4+5) to (4 5)+ whilst the * is shifted to after the outermost parenthesis pair, resulting in the final ((4 5)+ 6)*. Given that postfix notation does not require the use of parenthesis, the final result is actually 4 5 + 6 *. Notice how the operands sequence remains the same, with just a shift in the operator positions occurring. There could be cases where operators are also re-ordered as in the case of (4+(5*6)) 4 5 6 * +. This conversion process is best implemented using a stack data structure for operators: the opstack. Expressions are scanned left-right. Operands are simply appended to the resulting postfix expression. On the other hand parenthesis and operators follow the procedure: Push left parenthesis on the opstack. On encountering a right parenthesis pop the opstack until the corresponding left parenthesis is removed. Append each popped operator to the end of the output list. On encountering an operator push it on the opstack, however first pop and append to the output any operator with equal or higher precedence, or until a left parenthesis is encountered. When expression scanning has terminated, pop and append to the output expression any remaining operators. 2c. Write a main function that accepts input infix expressions from the console and proceeds by validating and converting arithmetic expressions to postfix format. Finally the postfix expressions are evaluated as described above and the results sent to the console. Page 4 of 5

Task 3: Allocated memory and I/O (Total: 5 marks) Make the previous program more memory efficient and practical to use by making use of dynamic arrays and making the program accept a sequence of expressions for evaluation from an input file. Likewise, evaluation results are to be written to an output file. In your report only focus on the modified code. Task 4: Abstract data types (ADT) (Total: 15 marks) 4a. Make your code more reusable and maintainable by reimplementing the stack data structure as an ADT that exposes the following interface: stack(max_size): creates a new stack with at most max_size elements. push(s, p): pushes element p on stack S. pop(s): pops the top element from stack S. top(s): returns, but without removing, the top element from stack S. is_empty(s): returns true if stack S contains no elements; false otherwise. 4b. At the same time further improve memory efficiency by re-implementing the stack as a linked list. 4c. Update the previous program accordingly to use the ADT implementation of the stack. Task 5: Shared libraries (Total: 10 marks) Make your ADT even more re-usable and maintainable by shifting its implementation to a shared (dynamically linked) library. In the report focus on documenting the process of creating and linking to the shared library. END. Page 5 of 5