CP104 Introduction to Programming Array Lecture 25 __ 1 Josephus Problem In the Jewish revolt against Rome, Josephus and 39 of his comrades were holding.

Slides:



Advertisements
Similar presentations
Discrete Mathematics Section 1.1 First Examples. A MAGIC TRICK.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COMPSCI 105 S Principles of Computer Science 13 Stacks.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8: Arrays Problem Solving & Program Design in C Sixth Edition.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Chapter 8 Arrays and Strings
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Arrays.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Stacks and Queues Introduction to Computing Science and Programming I.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Arrays Why we need data structure? Simple data types use a single memory cell to store a variable. Sometimes (for example scores of a class) it is more.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Data Structures & Algorithms
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CHP-3 STACKS.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
STACK Data Structure
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Unit 6 Data Types and Arrays. Key Concepts Explicit and automatic conversion ASCII Enumerated types Function parameters Arrays Loops and arrays Passing.
Click to edit Master text styles Stacks Data Structure.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 4 Stacks
Chapter 12 – Data Structures
Stacks.
Pointers and Linked Lists
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CSC 205 – Java Programming II
Popping Items Off a Stack Lesson xx
Chapter 8: Arrays Problem Solving and Program Design in C 5th Edition
Stacks: Implemented using Linked Lists
Stack.
Stacks.
Lecture 9: Stack and Queue
Presentation transcript:

CP104 Introduction to Programming Array Lecture 25 __ 1 Josephus Problem In the Jewish revolt against Rome, Josephus and 39 of his comrades were holding out against the Romans in a cave. With defeat imminent, they resolved that, like the rebels at Masada, they would rather die than be slaves to the Romans. They decided to arrange themselves in a circle. One man was designated as number one, and they proceeded clockwise killing every second man... Josephus (according to the story) was among other things an accomplished mathematician; so he instantly figured out where he ought to sit in order to be the last to go. But when the time came, instead of killing himself he joined the Roman side. Question: If there are 16 people, which position will Joseph choose? First one be executed

CP104 Introduction to Programming Array Lecture 25 __ 2 Stack What is a stack? –A stack is a collection of data that works on the principle of Last In First Out (LIFO) –Stack has two operations Push: put an element on top of the stack; Pop: removes the top element of the stack

CP104 Introduction to Programming Array Lecture 25 __ 3 Example of Stack Reverse an input string of bits –E.g. input a string of characters, print them in inverse order –Ex. Read a binary bit string, calculate its decimal number Check the balance of parenthesis of an expression e.g. {…(…)…} is balanced, {…(…}…) is unbalanced How do we check the balanced or not using stack?

CP104 Introduction to Programming Array Lecture 25 __ 4 Stack Implementation by Array void push(char stack[], /* input/output - the stack */ char item, /* input - data being pushed onto the stack */ int *top, /* input/output - pointer to top of stack */ int max_size) /* input - maximum size of stack */ { if (*top < max_size-1) { ++(*top); stack[*top] = item; } char pop(char stack[], /* input/output - the stack */ int *top) /* input/output - pointer to top of stack */ { char item; /* value popped off the stack */ if (*top >= 0) { item = stack[*top]; --(*top); } else { item = STACK_EMPTY; } return (item); } see the demo

CP104 Introduction to Programming Array Lecture 25 __ 5 Multidimensional Array Two dimensional array. syntax of declaration: datatype array_name[size1][size2]; Example int a[2][3]; declare a two dimensional array of 2 by 3 (= 6) data elements How to reference the data of two dimensional array? How the data of two dimensional array is stored?

CP104 Introduction to Programming Array Lecture 25 __ 6 Initialization int a[3][4] = {{1,2,3,4}, {5,6,7,8}, {9, 10, 11, 12}}; int a[3][4] = {1,2,3,4, 5,6,7,8, 9, 10, 11, 12}; Int a[3][4] = {{1}, {0,6}, {0, 0, 1}}; See example Array with several dimensions int a[2][3][4]; Declare a three dimensional array of 24 integer data item. See example

CP104 Introduction to Programming Array Lecture 25 __ 7 Function to Check Whether Tic-tac-toe Board Is Filled See demo

CP104 Introduction to Programming Array Lecture 25 __ 8 Case Study: Analysis of Sales Data Problem: input sales transaction data Output: Sales Analysis Output

CP104 Introduction to Programming Array Lecture 25 __ 9 Sales Analysis Main Function

CP104 Introduction to Programming Array Lecture 25 __ 10 Function scan_table and Helper Function initialize

CP104 Introduction to Programming Array Lecture 25 __ 11 Function scan_table and Helper Function initialize (cont’d)

CP104 Introduction to Programming Array Lecture 25 __ 12 Function scan_table and Helper Function initialize (cont’d)

CP104 Introduction to Programming Array Lecture 25 __ 13 Function display_table and Helper Function display_quarter

CP104 Introduction to Programming Array Lecture 25 __ 14 Function display_table and Helper Function display_quarter (cont’d)