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

Exam1 Review

  • Upload
    zander

  • View
    38

  • Download
    0

Embed Size (px)

DESCRIPTION

Exam1 Review. Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010. Software Development Phases. Problem Analysis and Specification Design Coding Testing, Execution, and Debugging Maintenance. Software Development Model. - PowerPoint PPT Presentation

Citation preview

Page 1: Exam1 Review

Exam1 Review

Dr. Bernard Chen Ph.D.University of Central Arkansas

Spring 2010

Page 2: Exam1 Review

Software Development Phases

Problem Analysis and Specification Design Coding Testing, Execution, and Debugging Maintenance

Page 3: Exam1 Review

Software Development Model

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

Page 4: Exam1 Review

Waterfall Model

Page 5: Exam1 Review

Realistic Waterfall Model

Page 6: Exam1 Review

Software Development Phases

Problem Analysis and Specification Design Coding Testing, Execution, and

Debugging Maintenance

Page 7: Exam1 Review

Testing, Execution, and Debugging

Errors happen all the time!!!

There are three different points at which errors can be introduced:

1. Syntax errors2. Run-time errors3. Logic errors

Page 8: Exam1 Review

The "V" Life Cycle Model

Page 9: Exam1 Review

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 )

Page 10: Exam1 Review

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

Page 11: Exam1 Review

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.

Page 12: Exam1 Review

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: 100n3 + 30000n =>O(n3) 100n3 + 2n5+ 30000n =>O(n5)

Page 13: Exam1 Review

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

Page 14: Exam1 Review

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.

Page 15: Exam1 Review

Binary Search 3-ways comparisonsint binarySearch(vector<int> 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

Page 16: Exam1 Review

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

Page 17: Exam1 Review

O(N) Algorithmtemplate <class Comparable>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

Page 18: Exam1 Review

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

Page 19: Exam1 Review

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

Page 20: Exam1 Review

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

Page 21: Exam1 Review

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

Page 22: Exam1 Review

Implementing Linked Stack Operations

Pop Delete first node in the

linked listptr = myTop;myTop = myTop->next;delete ptr;

Output Traverse the listfor (ptr = myTop; ptr != 0; ptr = ptr->next) out << ptr->data << endl;

Page 23: Exam1 Review

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

Page 24: Exam1 Review

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

Page 25: Exam1 Review

Introduction to Queues

Difference between Stack and Queues:

Stack exhibits last-in-first-out (LIFO)

Queue exhibits first-in-first-out (FIFO)

Page 26: Exam1 Review

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)

Page 27: Exam1 Review

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 queueobject like this

Page 28: Exam1 Review

Circular Queue

Problems We quickly "walk off the end" of the array

Possible solutions Shift array elements Use a circular queue

Note that both emptyand full queuegives myBack == myFront

Page 29: Exam1 Review

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

Page 30: Exam1 Review

Circular Example

Both Front and Back wraparound as needed.

b c d

Front Back

b c d

FrontBack

e f

e fg

Page 31: Exam1 Review

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

Page 32: Exam1 Review

Algorithm for Enqueue(value) 1. Set newBack ==

(myBack+1)%Queue_capacity

2. If newBack == myFrontSignal “Queue Full”

otherwise:Set Array[myBack] == valueSet myBack == newBack

Page 33: Exam1 Review

Algorithm for Dequeue() If queue is empty

signal “Queue Empty”Otherwise

Set myFront=(myFront+1)%Queue_capacity

Page 34: Exam1 Review

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();

Page 35: Exam1 Review

Dequeue

Dequeue (remove element from front)

L.erase( L.begin() );

L.begin() is an iterator, in erase(), you cannot just gives the location

Page 36: Exam1 Review

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()

Page 37: Exam1 Review

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??

Page 38: Exam1 Review

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

Page 39: Exam1 Review

Static Array based

Insert: we need to do Check the capacity Need to move array elements to

make room for the new element

Page 40: Exam1 Review

Static Array based Algorithm for insert:

If size is equal to capacity: display error due to limit space

If pos < 0 or 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 0array[pos]=itemsize++

Page 41: Exam1 Review

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

Page 42: Exam1 Review

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

Page 43: Exam1 Review

Static Array based Algorithm for delete:

If size is zero:issue a error message

If pos <0 or pos >=size:issue an error message

Otherwise:for index i ranging from pos to size-2:

array[i]=array[i+1]size--

Page 44: Exam1 Review

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)

Page 45: Exam1 Review

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

Page 46: Exam1 Review

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

Page 47: Exam1 Review

Insertion

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

20newptr

predptr

Page 48: Exam1 Review

Deletion

For deletion, there are also two cases to consider: Deleting an element that has a

predecessor Delete the first element in the list

Page 49: Exam1 Review

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

Page 50: Exam1 Review

Array-Based Implementation of Linked Lists

Given a list with names

Implementation would look like this

Page 51: Exam1 Review

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