Exam1 Review

Preview:

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

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

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

Waterfall Model

Realistic Waterfall Model

Software Development Phases

Problem Analysis and Specification Design Coding Testing, Execution, and

Debugging Maintenance

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

The "V" Life Cycle Model

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 )

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

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.

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)

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

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.

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

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

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

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

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

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

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

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;

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

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

Introduction to Queues

Difference between Stack and Queues:

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

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

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)

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

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

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

Circular Example

Both Front and Back wraparound as needed.

b c d

Front Back

b c d

FrontBack

e f

e fg

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

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

(myBack+1)%Queue_capacity

2. If newBack == myFrontSignal “Queue Full”

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

Algorithm for Dequeue() If queue is empty

signal “Queue Empty”Otherwise

Set myFront=(myFront+1)%Queue_capacity

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

Dequeue

Dequeue (remove element from front)

L.erase( L.begin() );

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

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

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

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

Static Array based

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

make room for the new element

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

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

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

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

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)

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

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

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

Deletion

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

predecessor Delete the first element in the list

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

Array-Based Implementation of Linked Lists

Given a list with names

Implementation would look like this

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