CC4 Notes
CC4 Notes
FC of Line 1: A statement
Rule 1: for-loops =1
FC of Line 2: Three statements. Initialization + Condition +
The running time for a for loop is at most the running time of Change of State
the statements inside the for loop (including tests) times the = Initialization is always 1
number of iterations. Condition is the difference of condition
and initialization plus the maximum number of comparison/s
Rule 2: Nested for-loops Change of state is one less than the
condition
Analyze these inside out. The total running time of a = 1 + ((n-1)+2) + (((n-1)+2) -1)
statement inside a group of nested for is the running time of = 1 + (n+1) + n
the statement multiplied by the product of the sizes of all the = 2n + 2
for loops. FC of Line 3: Open curly brace is not treated as a statement,
so no frequency count.
Rule 3: Consecutive Statements =0
FC of Line 4: One statement but repeated by the number of
These just add, which means that the maximum is the one times the condition is true.
that counts. =1*n
=n
Rule 4: if-else Statements FC of Line 5: Close curly brace is executed, so it is considered
as a statement.
For the fragment: =1
if(cond) FC of Line 6: A statement
S1; =1
else S2;
TFC = sum of all the FC from Line 1 to Line 6
the running time of an if/else statement is never more than
= 1 + (2n+ 2) + 0 + n + 1 + 1
the running time of the test plus the larger of the running
times of S1 and S2. TFC = 3n + 5
Big-O = the term (variable) with the highest degree
Problem: Determine the Total Frequency Count (TFC) and the Big-O = O(n)
corresponding Big-O notation for the code segment below.
Line 1 x = 500;
Line 2 for (i = 1; i <= n; i++) Cascading Loops: Compute the TFC and Big-O
Line 3 {
Line 4 x = x + i; Analysis: Cascading loops are loops outside a loop.
Line 5 }
Each loop is independent of the other. The strategy in
Line 6 System.out.println(x);
solving this problem is to compute the FC of each loop
and thereafter add them all up to arrive at the TFC. The 1) = k+1
term (variable) with the highest degree will be used for } =1
determining the Big-O. =1
Solution: FCc = 4k + 7
Program Segment Frequency Count Note: at worst-running time complexity, n, m and k can
Solution reach positive infinity, so (n=m=k)
for(a = 1; a <= n; a++) = 1 + (n-1+2) + ((n-
1+2)-1) = 2n+2 TFC = FCa + FCb + FCc
{ = (4n + 3) + (4m – 1) + (4k + 7)
statement1; = 1 * ((n-1+2)- = (4n + 3) + (4n – 1) + (4n + 7)
1) =n TFC = 12n + 9
statement2; = 1 * ((n-1+2)-
1) =n Big-O = O(n)
} =
1 =1
Evaluation of Expression
FCa = 4n + 3
Expression is made up of operands, operators and delimiters.
for(b = 1; b < m; b++) = 1 + (m-1+1) + ((m- Example:
1+1)-1) = 2m
{ X = 8 / 4 * 3 -5 * 6 + (7 - 2 * 9 % 2 ^ 3) + 8 * 9; //using
statement3; = 1 * ((m-1+1)-1) constants as operands and arithmetic operators.
= m-1 Y = A / B * C - D * E + (F - G * H % I ^ C) + A * H; // using
statement4; = 1 * ((m-1+1)-1) variables as operands and arithmetic operators
= m-1
} = Operands can be any legal variable names or constants in
1 =1 programming languages.
This is how our math teachers taught us and it is prone 6. Go back to step 4 while X is not # else go to step 7
to mistakes!
7. If STACK is not empty, POP and print operators in the
MDAS or PEMDAS Rule IS NOT A RULE!!! This is one OUTPUT while not #.
reason for the erroneous computations. The proper
IN-Stack Priority (ISP) – The priority of the operator as an
rule should be The Precedence and Associativity Rule
element of the stack.
of Operators, which include Arithmetic, Relational and
IN-Coming Priority (ICP) – The priority of the operator as
Logical operators.
current token.
Furthermore, creating a program to evaluate infix will
SYMBOL ISP (Y) ICP (X)
prove to be very challenging and ambiguous.
) -- -- closed
So, Infix form should be transformed to either Prefix or
parenthesis
Postfix for a 1-D array implementation to be effective
^ 4 4 raised to
(consistently achieving the right output)
operator in this topic (not the bitwise XOR operator)
Prefix has a higher TFC than Postfix. *, /, % 2 2
multiplication, division, modulo
The compiler accepts expressions and produce correct +, - 1 1 addition,
result by reworking the expressions into postfix form. subtraction
Conversion from Infix to Postfix using STACKS ( 0 5 open
parenthesis
Algorithm converting infix form to postfix using stacks. # -1 -1 nothing
1. Create a table with 3 columns. Column1 for TOKEN (X), follows
Column2 for STACK(Y), Column3 OUTPUT
Note: ISP and ICP values can be adjusted to include {
other operators such as relational and logical while STAK[top] != ‘#’
operators following the Precedence and Associativity {
Rule of operators. POP(Y);
print(Y);
Sample Code snippet:
}
void POSTFIX(expression E) }
token X ,Y;
xample:
% #+(-% 84/3*56*-729* 2 3 * 30 - 7 18 8 % - + 72 +
2 #+(-% 84/3*56*-729*2 6 30 - 7 18 8 % - + 72 +
3 #+(-%^ 84/3*56*-729*23 6 30 - 7 2 - + 72 +
) #+ 84/3*56*-729*23^%- -24 7 2 - + 72 +
8 #+ 84/3*56*-729*23^%-+8 -24 5 + 72 +
E=84/3*56*-729*23^%-+89*+
Simulation
84/3*56*-729*23^%-+89*+
23*56*-729*23^%-+89*+
2 3 * 30 - 7 2 9 * 2 3 ^ % - + 8 9 * +
2 3 * 30 - 7 18 2 3 ^ % - + 8 9 * +