Download presentation
Presentation is loading. Please wait.
Published byRosamund Ethel Hardy Modified over 9 years ago
1
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? 2 10 14 6 4 16 12 8 3 5 7 1 15 13 11 9 First one be executed
2
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
3
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?
4
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
5
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?
6
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
7
CP104 Introduction to Programming Array Lecture 25 __ 7 Function to Check Whether Tic-tac-toe Board Is Filled See demo
8
CP104 Introduction to Programming Array Lecture 25 __ 8 Case Study: Analysis of Sales Data Problem: input sales transaction data Output: Sales Analysis Output
9
CP104 Introduction to Programming Array Lecture 25 __ 9 Sales Analysis Main Function
10
CP104 Introduction to Programming Array Lecture 25 __ 10 Function scan_table and Helper Function initialize
11
CP104 Introduction to Programming Array Lecture 25 __ 11 Function scan_table and Helper Function initialize (cont’d)
12
CP104 Introduction to Programming Array Lecture 25 __ 12 Function scan_table and Helper Function initialize (cont’d)
13
CP104 Introduction to Programming Array Lecture 25 __ 13 Function display_table and Helper Function display_quarter
14
CP104 Introduction to Programming Array Lecture 25 __ 14 Function display_table and Helper Function display_quarter (cont’d)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.