Unit - 4 CTPS
Unit - 4 CTPS
Unit - 4 CTPS
Length
We can obtain the length of a string literal by typing a “. length” after the
string.
Figure shows how we can obtain the length of the string literal “popcorn”.
The expression “popcorn”. length produces the number 7 since the
length of the string literal “popcorn” is 7.
In other words, the code “popcorn”.length is interchangeable with the
value produced by the code, the number 7.
Concatenation
String concatenation is another common string processing operation.
String concatenation is an operation that takes two strings and splices
them to form a third string as output.
String concatenation is usually expressed, oddly enough, as a plus symbol
(+).
Although we usually think of the plus symbol as referring to the
mathematical addition of two numbers, the plus symbol is also employed
to concatenate two strings.
The two strings “pop” and “corn” can be concatenated to produce the
string “popcorn”.
Naming
Variables are bound to data through a name binding operation using
the left-arrow symbol (←).
On the left of this symbol must be a variable name and a value must
occur on the right of the arrow.
In this sequence of actions we tell the computer to (1) bind the name x
to the string literal “pop”, (2) bind the name y to the string literal
“corn” and (3) bind the name z to the string “popcorn”, a string that is
produced by concatenating the strings referred to by the variables x
and y.
Substring
The substring function allows us to obtain part of a string if we know
the indices of the first and last characters that we want to extract from
the string.
The function substring is applied to the x variable, which is bound to
the string “computational thinking”.
We are telling the computer to give us the sequence of characters
starting from the character at index 3 and ending with the character at
index 5.
Searching
The indexOf function searches a string literal for a character and
returns the index of the first occurrence of the character.
In this example, the value produced by this code is the number 3 since
that first lowercase c occurs at index 3 in the string literal “popcorn”.
In other words, the code “popcorn”.indexOf(“o”) is interchangeable
with the number 3, the data the code produces.
4.3 Abstraction
An abstraction is anything that allows us to concentrate on important
characteristics while deemphasizing less important, perhaps distracting,
details.
For example, you are using abstraction if you tell someone that you just
saw a red Corvette. In this case the color and model of the car were
considered to be the most important, while details of the model year,
engine displacement, wheel dimensions, and so forth are omitted.
Store the word SENTENCE in an array called word. Let’s consider from the
first character of the text and the first character of word. To check the next
character increase the pointer ‘i’ to 2.
We must compare the chr just read with word[2]. If chr=’E’ the matching
process can continue. If i=9 a match is found.
When a mismatch is found reset the pointer of word array.
The steps to be taken when a mismatch occurs is
If word-match then
(a) Increment match count by 1.
(b) Set pre to most recent character read,
(c) Reset pointer i to first of word array
Algorithm Description:
1. Establish the word and wordlength wlength of the search-word.
2. Initialise the match-count nmatches, set preceding character and set
pointer for word array i to 1.
3. While not at end-of-line do
(a) while not end-of-line do
(a.1) read next character;
(a.2) if current text character chr matches ith character in word then
(2.a) extend partial match i by 1.
(2.b) if a word-pattern match then
(b.1) read next character post
(b.2) if preceding and following character not alphabetic then
(2.a) update match count nmatches
(b.3) re-initialize pointer to word array i
(b.4) save following character post as preceding character
else
(2’.a) save current text character as preceding character for match
(2’.b) reset word array pointer i to first position
(b) read past end of line
4. Return word match count nmatches
Problem:
Given a set of n numbers design an algorithm that adds these numbers and
returns the resultant sum. Assume n is greater than or equal to zero.
Algorithm Development:
Write the numbers down one under the other and start adding up the right-hand
column, For eg, Consider the addition of 421, 583 and 714
421
583
714
…8
For eg S=421+583+714
S=a+b+c
General equation
Assign s=0
S=S+a1
S=S+ a2
.
.
.
In General- S=S+ai+1
For n>2
In the above code, a, b loses its relevance, Hence
The numbers that are not crossed out are prime numbers
It is named as “Seive of Eratosthenes” named after the Greek
mathematician Eratosthenes.
Storage problem arises when the list goes larger
To avoid this we use √x
For eg when x=1000 – we have to cross only multiples of 31 because √1000
≈ 31.
Raising a number to a large powerProblem
Given some integer x, compute the value of xn where n is a positive integer
greater than 1
Algorithm Description:
Evaluating the expression
p=xn , where x and n are given is a straight forward task
A simple method of evaluation is accumulating product p by x for n iterations.
p := 1
for I := 1 to n do
p := p * x
However, sometimes the above method strive for higher efficiencies in
evaluating the power of some integers.
Consider the evaluation of x10. In our stepwise approach we have
Notice that x is increasing by one power at each step.
For eg
x4=x2*x2
Requires only two multiplications.
Multiplication = addition of powers