Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.

Similar presentations


Presentation on theme: "Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010."— Presentation transcript:

1 Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010

2 Software Development Phases Problem Analysis and Specification Design Coding Testing, Execution, and Debugging Maintenance

3 Software Development Model One of the earliest strategies for development software is known as the Waterfall Model

4 Waterfall Model

5 Realistic Waterfall Model

6 Software Development Phases Problem Analysis and Specification Design Coding Testing, Execution, and Debugging Maintenance

7 Testing, Execution, and Debugging Errors happen all the time!!! There are three different points at which errors can be introduced: 1. Syntax errors 2. Run-time errors 3. Logic errors

8 The "V" Life Cycle Model

9 Two major types of testing Black box testing: Outputs produced for various inputs Checked for correctness Do not consider structure of program component itself. (so basically, it just test a lot of different inputs and match with the expected outputs )

10 Two major types of testing White box testing: examine code’s internal structure (Test for every loop, if statement, functions…) Test data is carefully selected

11 What is Algorithm Analysis? Algorithm: A clearly specified finite set of instructions a computer follows to solve a problem. Algorithm analysis: a process of determining the amount of time, resource, etc. required when executing an algorithm.

12 Big O Notation Big O notation is used to capture the most dominant term in a function, and to represent the growth rate. Also called asymptotic upper bound. Ex: 100n 3 + 30000n =>O(n 3 ) 100n 3 + 2n 5 + 30000n =>O(n 5 )

13 Sequential Search A sequential search steps through the data sequentially until an match is found. A sequential search is useful when the array is not sorted. A sequential search is linear O(n) (i.e. proportional to the size of input) Unsuccessful search --- n times Successful search (worst) ---n times Successful search (average) --- n/2 times

14 Binary Search If the array has been sorted, we can use binary search, which is performed from the middle of the array rather than the end. We keep track of low_end and high_end, which delimit the portion of the array in which an item, if present, must reside. If low_end is larger than high_end, we know the item is not present.

15 Binary Search 3-ways comparisons int binarySearch(vector a[], int x){ int low = 0; int high = a.size() – 1; int mid; while(low < high) { mid = (low + high) / 2; if(a[mid] < x) low = mid + 1; else if( a[mid] > x) high = mid - 1; else return mid; } return NOT_FOUND; // NOT_FOUND = -1 }//binary search using three-ways comparisons

16 The Max. Contiguous Subsequence Given (possibly negative) integers A1, A2,.., An, find (and identify the sequence corresponding to) the max. value of sum of Ak where k = i -> j. The max. contiguous sequence sum is zero if all the integer are negative. {-2, 11, -4, 13, -5, 2} =>20 {1, -3, 4, -2, -1, 6} => 7

17 O(N) Algorithm template int maxSubsequenceSum(int a[]){ int n = a.size(); int thisSum = 0, maxSum = 0; int i=0; for( int j = 0; j < n; j++){ thisSum += a[j]; if( thisSum > maxSum){ maxSum = thisSum; seqStart = i; seqEnd = j; }else if( thisSum < 0) { i = j + 1; thisSum = 0; } return maxSum; }//figure 6.8

18 Introduction to Stacks This type of last-in-first-out processing occurs in a wide variety of applications This last-in-first-out (LIFO) data structure is called a Stack Adding an item to a stack is referred to as pushing that item onto the stack Removing an item from the stack is referred to as popping the stack

19 Designing and Building a Stack class The basic functions are: Constructor: construct an empty stack Empty(): Examines whether the stack is empty or not Push(): Add a value at the top of the stack Top(): Read the value at the top of the stack Pop(): Remove the value at the top of the stack Display(): Displays all the elements in the stack

20 Select position 0 as bottom of the stack A better approach is to let position 0 be the bottom of the stack Thus our design will include An array to hold the stack elements An integer to indicate the top of the stack

21 Implementing Linked Stack Operations Constructor Simply assign null pointer to myTop Empty Check for myTop == null Push Insertion at beginning of list Top Return data to which myTop points

22 Implementing Linked Stack Operations Pop Delete first node in the linked list ptr = myTop; myTop = myTop->next; delete ptr; Output Traverse the list for (ptr = myTop; ptr != 0; ptr = ptr->next) out data << endl;

23 Functions related to Stack Constructor: vector L; Empty(): L.size() == 0? Push(): L.push_back(value); Top(): L.back(); Pop(): L.pop_back(); Display(): Write your own

24 Introduction to Queues A queue is a waiting line It’s in daily life: A line of persons waiting to check out at a supermarket A line of persons waiting to purchase a ticket for a film A line of planes waiting to take off at an airport A line of vehicles at a toll booth

25 Introduction to Queues Difference between Stack and Queues: Stack exhibits last-in-first-out (LIFO) Queue exhibits first-in-first-out (FIFO)

26 Queue ADT Basic operations Construct a queue Check if empty Enqueue (add element to back) Front (retrieve value of element from front) Dequeue (remove element from front)

27 Designing and Building a Queue Class Array-Based Consider an array in which to store a queue Note additional variables needed myFront, myBack Picture a queue object like this

28 Circular Queue Problems We quickly "walk off the end" of the array Possible solutions Shift array elements Use a circular queue Note that both empty and full queue gives myBack == myFront

29 Circular Queue Using a static array QUEUE_CAPACITY specified Enqueue increments myBack using mod operator, checks for full queue Dequeue increments myFront using mod operator, checks for empty queue

30 Circular Example Both Front and Back wraparound as needed. bcd FrontBack bcd FrontBack ef efg

31 Queue Full Situation If an item were stored in the last position, and an Enqueure() occurred myBack would be incremented by 1, giving it the same value as myFront However, myFront == myBack indicates the queue is empty Thus, we cannot distinguish between empty and full We may avoid this situation by maintaining one empty position, so that myFront will never equal to myBack unless the queue is empty

32 Algorithm for Enqueue(value) 1. Set newBack == (myBack+1)%Queue_capacity 2. If newBack == myFront Signal “Queue Full” otherwise: Set Array[myBack] == value Set myBack == newBack

33 Algorithm for Dequeue() If queue is empty signal “Queue Empty” Otherwise Set myFront=(myFront+1)%Queue_capacity

34 Enqueue and Front Enqueue (add element to back) This is the same with push(), therefore: L.push_back(value); Front (retrieve value of element from front) L.begin();

35 Dequeue Dequeue (remove element from front) L.erase( L.begin() ); L.begin() is an iterator, in erase(), you cannot just gives the location

36 Queue + Stack SCROLL: a queue-stack hybrid model Thus, you may have functions in both— Push(value)=Enqueue(value) Pop(); Top() --- top of stack Dequeue(); Front()

37 Consider Every Day Lists Groceries to be purchased Job to-do list List of assignments for a course Dean's list Can you name some others??

38 LIST ADT Collection of data elements A sequence with a finite number of data items, all of the same type Basic operations Construction---create a empty list Empty---check if the list is empty Insert---insert an item Delete---remove a item Traverse---go through the list or part of it

39 Static Array based Insert: we need to do Check the capacity Need to move array elements to make room for the new element

40 Static Array based Algorithm for insert: If size is equal to capacity: display error due to limit space If pos size: display error due to error position Else: for i in rangeing from size to pos+1: array[i]=array[i-1] //C++ starts index from 0 array[pos]=item size++

41 Static Array based The efficiency of this insert algorithm obviously depends on the number of array elements that must be shifted to make room for the new item In worst case, the new item must be inserted at the beginning of the list O(n) The best case occurs when the new item is inserted at the end of it O(1) Two important special kinds of lists for which are stacks and queues

42 Static Array based Delete: also requires shifting array elements For example, to delete the second item in: 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

43 Static Array based Algorithm for delete: If size is zero: issue a error message If pos =size: issue an error message Otherwise: for index i ranging from pos to size-2: array[i]=array[i+1] size--

44 Linked List Linked list nodes contain Data part – stores an element of the list Next part – stores link/pointer to next element (when no next element, null value)

45 Traverse We begin by initializing an auxiliary variable ptr to point to the first node: Initialize a variable ptr to point to first node Process data where ptr points

46 Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list

47 Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

48 Deletion For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

49 Deletion Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation : Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted. predptr ptr To free space

50 Array-Based Implementation of Linked Lists Given a list with names Implementation would look like this

51 Implementation Details For Insertion, there are also two cases to consider: insertion after some element in the list and insertion at the beginning of the list For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list


Download ppt "Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010."

Similar presentations


Ads by Google