CSC - A - L - P3 - CRM - Ipw - Caspa 2024
CSC - A - L - P3 - CRM - Ipw - Caspa 2024
A
PAIX – TRAVAIL – PATRIE
…………………..
…………………..
PRACTICAL
You are reminded of the necessity for good English and orderly presentation in your answers.
Analyse the problem as well as write algorithms in the answer booklet provided. Also record in your answer
booklet ant information requested or that you believe would make it easier to understand how you carried out
tasks or answered questions.
You are expected to print a single copy of relevant fragments of your work at different times. Please notify
the supervisor of any required printout that was not done!
When an imperative programming language is required to write program code, ether standard (ISO) Pascal or
the standard (ANSI) C programming language may be used.
If need be, supervisors will assist you in recording details of intermediate work carried out on the computer.
Where information is provided is provided as soft copy, notify the supervisor if it is not found in your
machine or has not been made available to you.
In this problem, array Ch1 represents the pattern while array Ch2 represents the data set. The goal is to find all
occurrences of Ch1 within Ch2. N and M represent the size of Ch1 and Ch2 respectively such that 𝑵 ≤
𝑴. 𝑇ℎ𝑒 𝑣𝑎𝑙𝑢𝑒𝑠 𝑜𝑓 𝑁 𝑎𝑛𝑑 𝑀 𝑎𝑟𝑒 𝑠𝑤𝑎𝑝𝑝𝑒𝑑 𝑖𝑓 𝑵 > 𝑴.
1. Swapping N and M if 𝑵 > 𝑴.
The following code snippet is written to exchange the values of variables N and M.
Temp ← N
M ← Temp
N←M
a) Assume N = 5 and M = 3. State the values of N and M after execution of the code snippet above.
(1 Mark)
b) Suggest how the code snippet above would need to be modified to successfully exchange the values of
variables N and M. (1 Mark)
i
N
Index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] …. [M-1]
Figure 2
Ch1 occurs in Ch2 if there exists a value of s as shown in figure 2 such that Ch1[i] = Ch2[i + s] for i starting from 0
to N-1(i traversing Ch1 as shown in figure 1). That is, there exist a value of s in figure 2 such that a substring of equal
length as Ch1 can be extracted from Ch2 which is identical to Ch1.
The number of occurrences of Ch1 in Ch2, NoOccur, is the number of positions s such that ch1[i] = ch2[i + s] (with i
traversing ch1). The variable s traverses the data set Ch2 and varies from 0 to a certain maximum value, MAX.
a) Determine the value of MAX as described above. (2 Marks)