# Expert Answer:Help me do the quiz

Solved by verified expert:Should have some short answer, multiple choice and one program question.
01_02___ch01_l2.ppt

ch_02_l1.ppt

Don't use plagiarized sources. Get Your Custom Essay on
Expert Answer:Help me do the quiz
Just from \$10/Page

ch_02_l2.ppt

ch06_1_stacks.pptx

ch06_2_stackswithalgebraic.pptx

ppt1.zip

ppt1.zip

ppt3.zip

ppt4.zip

ppt1.zip

Unformatted Attachment Preview

Algorithm Efficiency
To design and implement algorithms, programmers
must have a basic understanding of what
constitutes good, efficient algorithms. In this
section we discuss and develop several principles
that are used to analyze algorithms.
Linear Loops
Logarithmic Loops
Nested Loops
Big-O Notation
Standard Measurement of Efficiency
1
Algorithm efficiency is generally defined as a
function of the numbers to be processed.
n(n + 1)
f ( n) =
2
The simplification of efficiency is known as
big-O notation.
O(n 2 )
2
n(n + 1)
f ( n) =
2
As n grows large, the n2 term will come to dominate, so
that all other terms can be neglected.
Further, the constants will depend on the precise details
of the implementation and the hardware it runs on, so
they should also be neglected.
Big O notation captures what remains: we write O(n2)
and say that the algorithm has order of n2 time
complexity.
3
4
Data Structures: A Pseudocode Approach with C, Second Edition
5
20 =
1
21 =
2
22 =
4
23 =
8
24 = 16
. . .
2n = x
6
20 =
1
log2 1
= 0
21 =
2
log2 2
= 1
22 =
4
log2 4
= 2
23 =
8
log2 8
= 3
24 = 16
. . .
2n = x
log216
= 4
log2x
= n
7
20 =
1
log2 1
= 0
21 =
2
log2 2
= 1
22 =
4
log2 4
= 2
23 =
8
log2 8
= 3
24 = 16
. . .
2n = x
log216
= 4
log2x
= n
8
20 =
1
log2 1
= 0
21 =
2
log2 2
= 1
22 =
4
log2 4
= 2
23 =
8
log2 8
= 3
24 = 16
. . .
2n = x
log216
= 4
log2x
= n
log240 = ?
9
20 =
1
log2 1
= 0
21 =
2
log2 2
= 1
22 =
4
log2 4
= 2
23 =
8
log2 8
= 3
24 = 16
. . .
2n = x
log216
= 4
log2x
= n
5 = log232
< log240 < log264 = 6 10 2. Give the BigO notation for each of the following pseudocode fragments: (A) a = n b = n + 1 11 2. Give the BigO notation for each of the following pseudocode fragments: (A) a = n b = n + 1 Answer: constant ( or O(1) ) 12 2. Give the BigO notation for each of the following pseudocode fragments: (B) a = n if( a >= 0 )
b = n + 1
else
b = (-1)*n + 1
end if
13
2. Give the BigO notation for each of the following pseudocode
fragments:
(B)
a = n
if( a >= 0 )
b = n + 1
else
b = (-1)*n + 1
end if
14
2. Give the BigO notation for each of the following pseudocode
fragments:
(C)
i = 1
loop( i <= n ) print( i ) i = i + 1 end loop 15 2. Give the BigO notation for each of the following pseudocode fragments: (C) i = 1 loop( i <= n ) print( i ) i = i + 1 end loop Answer: O(n) 16 2. Give the BigO notation for each of the following pseudocode fragments: (D) i = n loop( i > 0 )
print( i )
i = i – 1
end loop
j = 1
loop( j <= n ) print( j ) j = j + 2 end loop 17 2. Give the BigO notation for each of the following pseudocode fragments: (D) i = n loop( i > 0 )
print( i )
i = i – 1
end loop
j = 1
loop( j <= n ) print( j ) j = j + 2 end loop Answer: n + n/2 => O(n)
18
Calculate the run-time complexity of the following algorithm
segment:
i = 1
loop( i <= n ) j = 1 loop( j <= n ) k = 1 loop( k <= n ) print( i, j, k ) k = k + 1 end loop j = j + 1 end loop i = i + 1 end loop 19 i = 1 loop( i <= n ) j = 1 loop( j <= n ) k = 1 loop( k <= n ) print( i, j, k ) k = k + 1 end loop j = j + 1 end loop i = i + 1 end loop Answer: n · n · n => O(n3)
20
(E)
i = 1
loop( i <= n ) print( i ) i = i * 2 end loop 21 22 (E) i = 1 loop( i <= n ) print( i ) i = i * 2 end loop Answer: O(log2n) 23 (F) i = n loop( i > 0 )
print( i )
i = i / 2
end loop
j = 1
loop( j <= n ) print( j ) j = j + 2 end loop 24 (F) i = n loop( i > 0 )
print( i )
i = i / 2
end loop
j = 1
loop( j <= n ) print( j ) j = j + 2 end loop Answer: log2n + n => O(n)
25
Recursion is a repetitive process in which an algorithm
calls itself.
Each recursive call must either
– solve a part of the problem
or
– reduce the size of the problem
1
Example: A Simple Guessing Game
The computer generates a random number between 1 and 20.
The user is given up to 5 tries to guess the exact number.
After each guess, the program displays a hint, such as
“Your guess is low!” or “… high!”, or
a “sorry” message followed by the random number, or
a “congratulation” message.
2
algorithm guess( rand_num, tries )
This algorithm receives a random number between 1 and
20 and then prompts the user to enter his/her guess up to
5 times.
PRE:
rand_num,
tries ( number of guesses )
POST:
success / sorry / hint printed
RETURN: nothing
A Simple Guessing Game – Iterative Algorithm
Data Structures: A Pseudocode Approach with C, Second Edition
3
algorithm
guess ( rand_num, tries )
guessed = FALSE
loop( tries > 0 AND NOT guessed )
if( num is equal to rand_num )
guessed = TRUE
print_Congratulations()
else
if( tries is equal to 1 )
print_Sorry( rand_num )
else
if( num < rand_num ) print_Low() else print_High() end if end if end if tries = tries – 1 end loop return end guess A Simple Guessing Game - Iterative Algorithm Data Structures: A Pseudocode Approach with C, Second Edition 4 algorithm guess ( rand_num, tries ) if( tries > 0 )
if( num is equal to rand_num )
print_Congratulations()
else
if( tries is equal to 1 )
print_Sorry( rand_num )
else
if( num < rand_num ) print_Low() else print_High() end if guess( rand_num, tries – 1 ) end if end if end if return end guess A Simple Guessing Game - Recursive Algorithm Data Structures: A Pseudocode Approach with C, Second Edition 5 You should not use recursion if the answer to any of the following questions is no: 1. Is the algorithm or data structures naturally suited to recursion? 2. Is the recursive solution shorter and more understandable? 3. Does the recursive solution run in acceptable time and space limits? 6 main main funA funA funB funA ???… Recursion and Structure Charts Data Structures: A Pseudocode Approach with C, Second Edition 7 main main funA funA funB Recursion and Structure Charts Data Structures: A Pseudocode Approach with C, Second Edition 8 Data Structures: A Pseudocode Approach with C, Second Edition 1 Data Structures: A Pseudocode Approach with C, Second Edition 2 Data Structures: A Pseudocode Approach with C, Second Edition 3 (Continued) Data Structures: A Pseudocode Approach with C, Second Edition 4 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 5 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 Fib(0) 0 6 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 Fib(0) Fib(1) 0 1 9 10 11 7 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 Fib(2) 1 Fib(0) 0 + Fib(1) 1 8 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 Fib(3) 9 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 Fib(3) Fib(1) 1 10 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 Fib(3) Fib(1) + Fib(2) 1 11 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 Fib(3) Fib(1) Fib(2) + 1 Fib(0) 0 12 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 Fib(3) 2 Fib(1) Fib(2) + 1 1 Fib(0) 0 + Fib(1) 1 13 Fib(4) 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 14 Fib(4) Fib(2) + 1 Fib(0) Fib(1) + 0 1 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 15 Fib(4) 3 Fib(2) + Fib(3) 2 1 Fib(0) + Fib(1) Fib(1) Fib(2) 1 1 1 0 Fib(0) + Fib(1) 0 1 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 16 Fib(5) 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 17 Fib(5) Fib(3) 2 Fib(1) + Fib(2) 1 1 Fib(0) Fib(1) + 1 1 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 18 Fib(5) 5 + Fib(3) Fib(4) 2 Fib(1) + 3 Fib(2) Fib(2) 1 1 1 Fib(0) + 1 Fib(1) Fib(0) 1 1 Fib(3) + 2 + Fib(1) Fib(1) 1 1 + Fib(2) 1 Fib(0) Fib(1) + 1 1 0 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 11 19 n >= 0
algorithm
fib ( n )
if( n EQUAL 0 or n EQUAL 1)
return n
return fib( n – 1 ) + fib( n – 2 )
end fib
20
main
fib
21
main
fib
22
main
fib
fib
fib
23
You should not use recursion if the answer to
any of the following questions is no:
1. Is the algorithm or data structures naturally
suited to recursion?
2. Is the recursive solution shorter and more
understandable?
3. Does the recursive solution run in acceptable
time and space limits?
24
Fib(5)
5
+
Fib(3)
Fib(4)
2
Fib(1)
+
1
Fib(0)
1
3
Fib(2)
Fib(2)
1
1
+
Fib(1)
Fib(0)
1
1
+
Fib(3)
+
2
Fib(1)
Fib(1)
1
1
+
Fib(2)
1
Fib(0)
1
+
Fib(1)
1
25
You should not use recursion if the answer to
any of the following questions is no:
1. Is the algorithm or data structures naturally
suited to recursion?
2. Is the recursive solution shorter and more
understandable?
3. Does the recursive solution run in acceptable
time and space limits?
26
Data Structures: A Pseudocode Approach with C, Second Edition
27
Data Structures: A Pseudocode Approach with C, Second Edition
28
Data Structures: A Pseudocode Approach with C
1
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
2
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
3
top
base
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
4
top
base
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
5
top
base
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
6
top
base
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
7
top
base
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
8
top
base
Last In First Out
data structure.
Restricted linear list:
Insert/Delete are restricted to one end called top.
9
10
Basic Stack Operations
The stack concept is introduced and three basic stack
operations are discussed.
• Push
• Pop
• Stack Top
11
Memory
Stack
Global
Local
Variables variables
Heap
Parameters
Static
allocation
of memory
Executable
program
Dynamic
allocation
of memory
Data
12
main
funA
funB
funC
Computer Stack
13
main
funA
funB
funC
m
Computer Stack
14
main
funA
funB
funC
A
m
Computer Stack
15
main
funA
funB
funC
C
A
m
Computer Stack
16
main
funA
funB
funC
A
m
Computer Stack
17
main
funA
funB
funC
m
Computer Stack
18
main
funA
funB
funC
B
m
Computer Stack
19
main
funA
funB
funC
m
Computer Stack
20
main
funA
funB
funC
Computer Stack
21
main
funA
funB
funC
C
m
A
A
A
m
m
m
B
m
m
m
Computer Stack
22
Array
Stack Implementations
23
stack
10
0
20
30
1
2
3
4
5
6
7
8
9
Array Implementation of Stack
24
stack
10
0
20
30
1
2
3
4
5
6
7
8
9
top
Array Implementation of Stack
25
stack
10
0
20
30
1
2
3
4
5
6
7
8
9
top
Push 40
Array Implementation of Stack
26
stack
10
0
20
30
40
1
2
3
4
5
6
7
8
9
top
Array Implementation of Stack
27
stack
10
0
20
30
40
1
2
3
4
5
6
7
8
9
top
Push 25
Array Implementation of Stack
28
stack
10
0
20
30
40
1
2
3
4
5
6
7
8
9
top
Push 25
top
= top + 1
Array Implementation of Stack
29
stack
10
0
20
30
40
25
1
2
3
4
5
6
7
8
9
top
Push 25
top
= top + 1
stack[top] = dataIn
Array Implementation of Stack
30
stack
10
0
20
30
40
25
1
2
3
4
5
6
7
8
9
top
if ( top < last ) top = top + 1 stack[top] = dataIn else stack is in OVERFLOW state Array Implementation of Stack 31 stack 10 0 20 30 40 25 1 2 3 4 5 6 7 8 9 top Pop if ( top >= first )
dataOut = stack[top]
top
= top – 1
else
stack is in UNDERFLOW state
end if
Array Implementation of Stack
32
stack
10
0
20
30
40
1
2
3
4
5
6
7
8
9
top
Array Implementation of Stack
33
Data Structures: A Pseudocode Approach with C
34
Data Structures: A Pseudocode Approach with C
35
Data Structures: A Pseudocode Approach with C
36
Data Structures: A Pseudocode Approach with C
37
Data Structures: A Pseudocode Approach with C
38
Data Structures: A Pseudocode Approach with C
39
Data Structures: A Pseudocode Approach with C
40
Data Structures: A Pseudocode Approach with C
41
Data Structures: A Pseudocode Approach with C
42
Data Structures: A Pseudocode Approach with C
43
Data Structures: A Pseudocode Approach with C
44
algorithm push( stack, data )
Insert (push) one item into the stack
Pre : stack – pointer to the stack head structure (passed by value)
data – contains data to be inserted into the stack (passed by
value)
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
count
2
top
stack
9
8
end push
PUSH
45
algorithm push( stack, data )
Insert (push) one item into the stack
Pre : stack – pointer to the stack head structure (passed by value)
data – contains data to be inserted into the stack (passed by
value)
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
count
2
top
stack
success = false
9
8
return success
end push
PUSH
46
algorithm push( stack, data )
Insert (push) one item into the stack
Pre : stack – pointer to the stack head structure (passed by value)
data – contains data to be inserted into the stack (passed by
value)
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
count
2
top
stack
success = false
allocate( newPtr )
if( stack not full )
9
8
return success
end push
PUSH
47
algorithm push( stack, data )
Insert (push) one item into the stack
Pre : stack – pointer to the stack head structure (passed by value)
data – contains data to be inserted into the stack (passed by
value)
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
success = false
allocate( newPtr )
if( stack not full )
newPtr->data = data
count
2
top
stack
7
9
8
return success
end push
PUSH
48
algorithm push( stack, data )
Insert (push) one item into the stack
Pre : stack – pointer to the stack head structure (passed by value)
data – contains data to be inserted into the stack (passed by
value)
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
success = false
allocate( newPtr )
if( stack not full )
newPtr->data = data
newPtr->next = stack->top
count
2
top
stack
7
9
8
return success
end push
PUSH
49
algorithm push( stack, data )
Insert (push) one item into the stack
Pre : stack – pointer to the stack head structure (passed by value)
data – contains data to be inserted into the stack (passed by
value)
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
success = false
allocate( newPtr )
if( stack not full
newPtr->data =
newPtr->next =
stack->top
=
count
2
top
stack
7
)
data
stack->top
newPtr
9
8
return success
end push
PUSH
50
algorithm push( stack, data )
Insert (push) one item into the stack
Pre : stack – pointer to the stack head structure (passed by value)
data – contains data to be inserted into the stack (passed by
value)
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
success = false
allocate( newPtr )
if( stack not full
newPtr->data =
newPtr->next =
stack->top
=
stack->count =
success = true
end if
count
3
top
stack
7
)
data
stack->top
newPtr
stack->count + 1
9
8
return success
end push
PUSH
51
/* Insert one item into the stack
Pre : stack – pointer to the stack head structure
data – contains data to be inserted into the stack
Post: data – have been pushed into the stack
Return: 1 if successful, 0 if memory overflow
*/
int push( STACK *stack, int data )
{
NODE *newPtr;
int
success = 0;
newPtr = (NODE *)malloc(sizeof(NODE));
if( newPtr != NULL )
{
success = 1;
newPtr->data = data;
newPtr->next = stack->top;
stack->top
= newPtr;
stack->count++;
}
return success;
count
3
top
stack
7
9
8
}
PUSH
52
Stack Operations
1.
2.
3.
4.
5.
6.
7.
8.
Create stack
Push
Pop
Stack top
Empty stack
Full stack
Stack count
Destroy stack
53
Stack Operations
1.
2.
3.
4.
5.
6.
7.
8.
Create stack
Push
Pop
Stack top
Empty stack
Full stack
Stack count
Destroy stack
Basic stack operations (3):
54
Stack Operations
1.
2.
3.
4.
5.
6.
7.
8.
Create stack
Push
Pop
Stack top
Empty stack
Full stack
Stack count
Destroy stack
Basic Stack Operations
55
Stack Operations
1.
2.
3.
4.
5.
6.
7.
8.
Create stack
Push
Pop
Stack top
Empty stack
Full stack
Stack count
Destroy stack
Most often used stack operations (4):
56
Stack Operations
1.
2.
3.
4.
5.
6.
7.
8.
Create stack
Push
Pop
Stack top
Empty stack
Full stack
Stack count
Destroy stack
Most Often Used Stack Operations
57
Stack Operations
1.
2.
3.
4.
5.
6.
7.
8.
Create stack
Push
Pop
Stack top
Empty stack
Full stack
Stack count
Destroy stack
Why do we need “Empty stack”, “Full stack”, and
“Stack count”?
58
Data Structures: A Pseudocode Approach with C
59
A Simple Non-ADT Implementation of a Stack
Data Type: float
main
createStack
insertData
printData
pushStack
popStack
stack_demo_1_build_print_stack
60
Data Structures: A Pseudocode Approach with C
61
count
3
count
3
top
10
top
stack
stack
10
20
20
30
30
62
count
3
top
stack
10
20
30
63
count
3
top
stack
Veronica
John
Alexander
64
a + b
infix form
+ a b
prefix form
a b +
postfix form
1
Postponement
Reverse Polish Notation
1. Infix to postfix transformation
– Manual transformation
– Algorithmic transformation
2. Evaluating postfix expressions
2
Infix to Postfix
Manual Transformation
a + 5 * b
3
a + 5 * b
Infix to Postfix: Manual Transformation
4
a + 5 b *
Infix to Postfix: Manual Transformation
5
a
5 b * +
Infix to Postfix: Manual Transformation
6
a + 5 * b
a + 5 * b
a + 5 b *
a
5 b * +
Infix to Postfix: Manual Transformation
7
Infix to Postfix
Manual Transformation
a + b * c – d / e
8
a + b * c – d / e
a b c * + d e / –
Infix to Postfix: Manual Transformation
9
Postponement
Reverse Polish Notation
1. Infix to postfix transformation
– Manual transformation
– Algorithmic transformation
2. Evaluating postfix expressions
10
Infix to Postfix
Algorithmic Transformation
a + 5 * b
11
a + 5 * b
a
Infix to Postfix: Algorithmic Transformation (STACK)
12
+
a + 5 * b
push
a
Infix to Postfix: Algorithmic Transformation (STACK)
13
+
a + 5 * b
a 5
Infix to Postfix: Algorithmic Transformation (STACK)
14
*
+
a + 5 * b
push
a 5
Infix to Postfix: Algorithmic Transformation (STACK)
15
*
+
a + 5 * b
a 5 b
Infix to Postfix: Algorithmic Transformation (STACK)
16
*
+
a + 5 * b
a 5 b *
Infix to Postfix: Algorithmic Transformation (STACK)
17
+
a + 5 * b
pop
a 5 b *
Infix to Postfix: Algorithmic Transformation (STACK)
18
a + 5 * b
a 5 b * +
Infix to Postfix: Algorithmic Transformation (STACK)
19
(a – 2 *(b + c) – d * e) * f
push
(
Infix to Postfix: Algorithmic Transformation (STACK)
Exercise 7d, page 140
20
(a – 2 *(b + c) – d * e) * f
a
(
Infix to Postfix: Algorithmic Transformation (STACK)
Exercise 7d, page 140
21
(a – 2 *(b + c) – d * e) * f
push
a

(
Infix to Postfix: Algorithmic Transformation (STACK)
Exercise 7d, page 140
22
(a – 2 *(b + c) – d * e) * f
a 2

(
Infix to Postfix: Algorithmic Transformation (STACK)
Exercise 7d, page 140
23
(a – 2 *(b + c) – d * e) * f
push
a 2
*

(
Infix to Postfix: Algorithmic Transformation (STACK)
Exercise 7d, page 140
24
(a – 2 *(b + c) – d * e) * f
push
a 2
(
*

(
Infix to Postfix: Algorithmic Transformation (STACK)
Exerc …
attachment

## Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
\$26
The price is based on these factors:
Number of pages
Urgency
Basic features
• Free title page and bibliography
• Unlimited revisions
• Plagiarism-free guarantee
• Money-back guarantee
On-demand options
• Writer’s samples
• Part-by-part delivery
• Overnight delivery
• Copies of used sources
Paper format
• 275 words per page
• 12 pt Arial/Times New Roman
• Double line spacing
• Any citation style (APA, MLA, Chicago/Turabian, Harvard)

# Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

### Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

### Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

### Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.