Docsity

programming and all languages, Schemes and Mind Maps of Programming Languages

Toggle sidebar
bg1
Assignment Brief and Guidance:
Task 1:Define basic algorithms to carry out an operation and outline the process of
programming an application (LO1)
Searching on an array/list is to find a given element on the array and return whether it is found
or not and return its position if found. Linear search and binary search are two popular searching
algorithms on arrays.
1.1 Define what an algorithm is and outline the characteristics of a good algorithm. Develop
algorithms for linear search and binary search using Pseudo code (P1).
1.2 Describe the steps involve in the process of writing and executing a program. Take an array
of 10 or more elements and dry run the above two algorithms. Show the outputs at the end of
each iteration and the final output (M1).
1.3 Define what Big-O notation is and explain its role in evaluating efficiencies of algorithms. Write
the Python program code for the above two algorithms and critically evaluate their efficiencies
using Big-O notation (D1
Task 2: Explain the characteristics of procedural, object-orientated and event-driven
programming, conduct an analysis of a suitable Integrated Development Environment
(LO2).
2.1 Define what is meant by a Programming Paradigm. Explain the main characteristics of
Procedural, Object oriented and Even-driven paradigms and the relationships among them (P2).
2.2 Analyze the features of an Integrated Development Environment (IDE) and explain how those
features help in application development (M2).
2.3 Write small snippets of code as example for the above three programming paradigms using
a suitable programming language(s) and critically evaluate their structure and the characteristics
(D2).
Ayubo Drive is the transport arm of Ayubo Leisure (Pvt) Ltd, an emerging travel & tour company
in Sri Lanka. It owns a fleet of vehicles ranging from cars, SUVs to vans.
The vehicles that it owns are hired or rented with or without a driver. The tariffs are based on the
vehicle type. Some of the vehicle types that it operates are, small car, sedan car, SVUs, Jeep (WD),
7-seater van and Commuter van. New vehicle types are to be added in the future.
Vehicle rent hire options are described below.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41

Partial preview of the text

Download programming and all languages and more Schemes and Mind Maps Programming Languages in PDF only on Docsity!

Assignment Brief and Guidance: Task 1:Define basic algorithms to carry out an operation and outline the process of programming an application (LO1) Searching on an array/list is to find a given element on the array and return whether it is found or not and return its position if found. Linear search and binary search are two popular searching algorithms on arrays. 1.1 Define what an algorithm is and outline the characteristics of a good algorithm. Develop algorithms for linear search and binary search using Pseudo code (P1). 1.2 Describe the steps involve in the process of writing and executing a program. Take an array of 10 or more elements and dry run the above two algorithms. Show the outputs at the end of each iteration and the final output (M1). 1.3 Define what Big-O notation is and explain its role in evaluating efficiencies of algorithms. Write the Python program code for the above two algorithms and critically evaluate their efficiencies using Big-O notation (D Task 2: Explain the characteristics of procedural, object-orientated and event-driven programming, conduct an analysis of a suitable Integrated Development Environment (LO2). 2.1 Define what is meant by a Programming Paradigm. Explain the main characteristics of Procedural, Object oriented and Even-driven paradigms and the relationships among them (P2). 2.2 Analyze the features of an Integrated Development Environment (IDE) and explain how those features help in application development (M2). 2.3 Write small snippets of code as example for the above three programming paradigms using a suitable programming language(s) and critically evaluate their structure and the characteristics (D2). Ayubo Drive is the transport arm of Ayubo Leisure (Pvt) Ltd, an emerging travel & tour company in Sri Lanka. It owns a fleet of vehicles ranging from cars, SUVs to vans. The vehicles that it owns are hired or rented with or without a driver. The tariffs are based on the vehicle type. Some of the vehicle types that it operates are, small car, sedan car, SVUs, Jeep (WD), 7 - seater van and Commuter van. New vehicle types are to be added in the future. Vehicle rent hire options are described below.

  1. Rent (With or without driver) – For each type of vehicle rates are given per day, per week and per month. Rate for a driver also given per day. Depending on the rent period the total rent amount needs to be calculated. For example: if a vehicle is rented for 10 days with a driver, total amount to be calculated as follows: Total rent = 1_week_car_rent + 1_day_rent x 3 + 1_day_driver_rent x 10
  2. Hire (with driver only) – These are based on packages such as airport drop, airport pickup, 100km per day package, 200km per day package etc. Standard rates are defined for a vehicle type for a package type that is applicable for that type of vehicle. For each package maximum km limit and maximum number of hours are also defined. Extra km rate is also defined which is applicable if they run beyond the allocated km limit for the tour. For day tours if they exceed max hour limit, a waiting charge is applicable for extra hours. Driver overnight rate and vehicle night park rate also defined which is applicable for each night when the vehicle is hired for 2 or more days. Task 3: Implement basic algorithms in code using an IDE (LO3). 3 3.1 Design suable algorithms for vehicle tariff calculation for rents and hires (P3). Ideally 3 functions should be developed for this purpose as follows: Function 1: Rent calculation. Return the total rent_value when rented_date, return_date, with_driver parameters are sent in. with_driver parameter is set to true or false depending whether the vehicle is rented with or without driver. Function 2: Day tour - hire calculation. Calculate total hire_value when start_time, end_time, start_km_reading, end_km_reading parameters are sent in. Should return base_hire_charge, waiting_charge and extra_km_charge as output paramters. Function 3: Long tour - hire calculation. Calculate total hire_value when start_date, end_date, start_km_reading, end_km_reading parameters are sent in. Should return base_hire_charge, overnight_stay_charge and extra_km_charge as output parameters.

In computer programming, algorithms are often created as functions. These functions serve as small programs that can be referenced by a larger program. For example, an image viewing application may include a library of functions that each use a custom algorithm to render different image file formats. An image editing program may contain algorithms designed to process image data. Examples of image processing algorithms include cropping, resizing, sharpening, blurring, red-eye reduction, and color enhancement. In many cases, there are multiple ways to perform a specific operation within a software program. Therefore, programmers usually seek to create the most efficient algorithms possible. By using highly-efficient algorithms, developers can ensure their programs run as fast as possible and use minimal system resources. Of course, not all algorithms are created perfectly the first time. Therefore, developers often improve existing algorithms and include them in future software updates. When you see a new version of a software program that has been "optimized" or has "faster performance," it most means the new version includes more efficient algorithms. Figure 1 Algorithm

1.1.2 Characters Of Algorithm

Running Time: A good algorithm should have shortest running time when compared with others. Space Utilization: Lesser the amount of space an algorithm uses, the better. Finiteness: An algorithm should terminate infinite number of steps and each step must finish in finite amount of time. Definiteness (No Ambiguity): Each step of algorithm should be clearly and precisely defined and there should not be any ambiguity. Inputs: An algorithm must have zero or more but must be finite number of inputs. Example of zero input algorithm. Print the ASCII code of each of the letter in the alphabet of the computer system. Output: An algorithm must have at-least one desirable outcome, i.e., output. Effectiveness: An algorithm should be effective. Effective means that each step should be referred as principle and should be executing in finite time.

1.1.3 Linear Search

In Linear Search the list is searched sequentially and the position is returned if the key element to be searched is available in the list, otherwise - 1 is returned. The search in Linear Search starts at the beginning of an array and move to the end, testing for a match at each item.

All the elements preceding the search element are traversed before the search element is traversed. i.e. if the element to be searched is in position 10, all elements form 1-9 are checked before 10. Figure 2 linear search Pseudocode for linear search Linear search (list, data) Begin Found=false For I=0 to length of list- 1 If list [I] =data then found=TRUE Break End if Next If found=TRUE then Print “data found” Else Print “data not found” End if End

1.1.4 Binary Search

A binary search algorithm is used to find the position of a specific value contained in a sorted array. Working with the principle of divide and conquer, this search algorithm can be quite fast, but the caveat is that the data has to be in a sorted form. It works by starting the search in the middle of the array and working going down the first lower or upper half of the sequence. If the

Mid = (low+ high)/ If (sorted list[mid]=search data) Position = mid+ Break Else if (search data>sorted list[mid]) Low =mid+ Else High=mid- 1 End if End do Return Position End Start List = input list of data Sdata = input search data Pos = binary_search (sorted List, Sdata) If Pos! = - 1 Then Output “Data Found at”, Pos Else Output “Data Not Found” Stop

1.2 Describe The Steps Involve In The Process Of Writing And Executing A

Program. Take An Array Of 10 Or More Elements And Dry Run The Above

Two Algorithms. Show The Outputs At The End Of Each Iteration And The

Final Output.

1.2.1 Programming Process

1. Problem Definition

This stage is the formal definition of the task. It includes the specification of inputs and outputs processing requirements, system constraints, and error handling methods. This step is very critical for the completion of a satisfactory program. It is impossible to solve a problem by using a computer, without a clear understanding and identification of the problem. Inadequate identification of problem leads to poor performance of the system. The programmer should invest a significant portion of his time in problem identification. If he does not spend enough time at this stage, he may find that his well-written program fails to solve the real problem. This step is the process of becoming familiar with the problem. It starts when the programmer is assigned a task. This step includes the reviewing of the design document that was prepared for the program, as well as any system wide information that would be helpful. The process ends when all the programmer’s questions have been resolved and the requirements of the program are understood. Example Step 1(problem) Sum of two numbers(n1+n2)

2. Design the Program

Programs use algorithms which are like equations that tell the computer what task to perform. The aim of the programmer is to create algorithms that are clear and simple. Algorithms are expressed first in logical hierarchical form known as modularization. Using modules or (a complete thought) the programmer creates a logical thought process for the computer to follow. After that the program is broken down in greater detail using pseudocode. Pseudocode uses terms like if, else, and, then to relate the programs rules to the computer. Two mini steps:

  • Determine program logic through top down approach and modularization, using a hierarchy chart
  • Design details using pseudocode and/or flowcharts, preferably involving control structure.

Figure 4 python code sum of two numbers

4. Test the Program

After the program is written it then enters the programming debugging and testing phase of the Program Development Life Cycle (PDLC). During this time the programmer will be looking for errors in both logic and syntax, as well as exploring other areas that may cause the program to either not work properly or to not run at all. This process is a lengthy and tedious one, oftentimes consisting of up to 50% of a program’s time in development (Morley 523). However, with a careful eye paid to program design and coding the amount of time spent debugging can be cut considerably. As stated, debugging will uncover errors in both logic and syntax. Syntax errors will prevent the program from executing. They can be such simple things as misspelled words or can involve breaking the syntax rules of the programming language used. On the other hand, logic errors will allow the program to run but will provide incorrect results. Errors of this kind may consist of merely using the wrong relational operator or larger, mistakes in writing formulas. Once the programmer locates the errors they are then fixed and the program is run again. This will happen multiple times, often called “execute, check, and correct” (526), until the program runs flawlessly. The program will then enter the testing phase. Testing the program comes in two phases, alpha and beta.

  • Alphatesting is the process of reading through the program in search of errors in logic. The second step is to run a diagnostic program to search for syntax or input errors.
  • Betatesting involves using the program in the real world to see if it contains any bugs or other deficiencies. Testing the python program Figure 5 testing the sum of two numbers

5. Document and Maintain

Documentation should be ongoing from the very beginning because it is needed for those involved with program now and future. Upon completion User Documentation for commercial use, Operator Documentation for people who run computer systems, and Programmer Documentation for programmers charged with maintenance.

Four mini steps: -

  • Write user documentation.
  • Write operator documentation.
  • Write programmer documentation.
  • Maintain the whole program. Every program requires certain processing time and memory to process the instructions and data. As the processing power and memory are the most precious resources of a computer, a program should be laid out in such a manner that it utilizes the least amount of memory and processing time Figure 6 program development life cycle

1.2.2 Dry Run

Dry run testing is a static test and should be performed by the developer to mitigate the effects of a failure of the product - meaning before the end user gets the product and discovers it doesn't do what it says it will. In dry run testing, no hardware is used, but it is assumed that the programmer who is testing the code is aware of what each line of code is supposed to do and gives him or her the opportunity to make corrections to the code before it becomes an issue for the actual software. Basically, a dry run test consists of programmers manually reading their code line by line to find errors and fix them. This is a simple Visual Basic code page; a full software application code page would look much more involved.

1.2.3 Dry Run For Linear Search

Data found Example: List [20,15,30,27,47,89,90,67,16,7,56] Search data [89] Table 1 linear search (data found) Place I Search data found Output 1 0 89 False - 2 1 89 False -

Data found List 1[10,5,25,32,65,41,120,56,77,78,80] Sorted the list 1[5,10,25,32,41,56,77,78,80] Search data= Table 3 binary search (data found) Steps Sorted List Search data Law high Mid Found Out put 1 5,10,25,32,41,56,77,78,80 25 0 9 - False - 2 5,10,25,32,41,56,77,78,80 25 0 9 - False - 3 5,10,25,32,41,56,77,78,80 25 0 3 4 False - 4 5,10,25,32,41,56,77,78,80 25 2 3 1 False - 5 5,10,25,32,41,56,77,78,80 25 2 3 2 True Data is fond Data not found Table 4 binary search (data not found) Steps List Search data Law high Mid Found Out put 1 5,10,25,32,41,56,77,78,80 28 0 - - False - 2 5,10,25,32,41,56,77,78,80 28 0 - - False 3 5,10,25,32,41,56,77,78,80 28 0 - - False 4 5,10,25,32,41,56,77,78,80 28 0 - - False 5 5,10,25,32,41,56,77,78,80 28 0 - - False 6 5,10,25,32,41,56,77,78,80 28 0 - - False

7 5,10,25,32,41,56,77,78,80 28 0 - - False 8 5,10,25,32,41,56,77,78,80 28 0 - - False 9 5,10,25,32,41,56,77,78,80 28 0 - - False 10 5,10,25,32,41,56,77,78,80 28 0 - - False Data not found

1.3 Define What Big-O Notation Is And Explain Its Role In Evaluating

Efficiencies Of Algorithms. Write The Python Program Code For The Above

Two Algorithms And Critically Evaluate Their Efficiencies Using Big-O

Notation

1.3.1 Big O Notation

Big O notation is a convenient way to describe how fast a function is growing. When studying the time complexity T( n) of an algorithm it’s rarely meaningful, or even possible, to compute an exact result. Typically, we are only interested in how fast T( n) is growing as a function of the input size n.

1.3.2 Efficiency Of Algorithms

  • Computational resources: time and space
  • Best, worst and average case performance How to compare algorithms: machine-independent measure of efficiency Growth rate Complexity measure O () How much time does it need How much memory (space) does it use Algorithm is a well-defined sequence of steps which leads to solving a certain problem. Steps should be: concrete

Basic operations Basic operations are operations which take constant time (at most time C for some constant C). In other words, time required to perform the operation does not grow with the size of the operands, or is bounded by a constant. Basic operations contd. If we are not sure how operations are implemented, we have to exercise judgement: can something be in principle implemented as a constant time operation? For example, adding two 32-bit integers can be, but adding a list of 32-bit integers cannot: it is going to take longer for a longer list.

1.3.3 Python Program For Two Algorithms

linear search (data found) def linear search (list, search): position=-1: for I in range (0, Len(list)-1): if list[I]==search: positioni+ break if position=! =-1: print (“data found”) print (“position, position”) else: print (“data not found”) k= [10,20,58,45,32,6,54,95,68,7,5,36,21] search data=

linear search (k, search data) Figure 7 python code for linear search data found (big o notation) linear search (data not found) Figure 8 python code for linear search data not found (big o notation) python code for binary search def binary_search (item_list, item): first = 0 last = Len(item_list)- 1 found = False while (first<=last and not found): mid = (first + last)// if item_list[mid] == item: found = True else: if item < item_list[mid]: last = mid - 1 else: first = mid + 1 return found print (binary_search ([1,2,3,5,8], 8)) data found Figure 9 python code for binary search data found (big o notation)

print (“data found”) print (“position, position”) else: print (“data not found”) k= [10,20,58,45,32,6,54,95,68,7,5,36,21] search data= linear search (k, search data) Every loop iteration here does two conditional branches: One to check whether we’re already at the element we’re looking for and the other to check whether we’re at the end of the array. Fortunately, both are easy to predict: By predicting that we’re not at the end of the array and that we haven’t found the element yet, we get exactly one branch misprediction per search, which is hard to improve upon. We can, however, reduce the total number of branches. First, by unrolling the loop, like this:

Linear Search With SSE

Using a SIMD instruction set like SSE2 we can further reduce the number of instructions per array element. First, we can load 4 array elements in one go into a 128-bit register. Then we can do 4 comparisons in parallel (with PCMPGTD) if we have prepared another register with 4 copies of key. We can then get the results of those comparisons into a general-purpose register (with PMOVMSKB) which we can then compare to the result we would expect if none of the 4 array elements match our key yet, in which case we increment our counter and repeat the loop. If the result indicated that one of the keys does match, we can use the BSF instruction, which counts the number of trailing zero bits in a register, to determine the index of the matching element without needing another loop. Using GCC’s SSE2 built-ins, the basic loop looks like this: This loop can of course be unrolled, as well. Note that after the sentinel we require that there be at least 3 more dummy array elements so that the load doesn’t result in a segmentation fault. If the array is properly aligned to 16 bytes that will always be the case. We are now at one conditional branch per 4 array elements, but we can go even lower. Using the PACKSSDW and PACKSSWB instructions we can pack the results of 16 key comparisons into one vector which we can then move into a general-purpose register like above and branch on it. We’ll require even more dummy memory after the sentinel. Here’s what it looks like: Binary search def binary_search (item_list, item):

first = 0 last = Len(item_list)- 1 found = False while (first<=last and not found): mid = (first + last)// if item_list[mid] == item: found = True else: if item < item_list[mid]: last = mid - 1 else: first = mid + 1 return found print (binary_search ([1,2,3,5,8], 8)) This will work for n < MAX_INT/2. For larger n, middle must be calculated as min + ((max - min) >> 1), which is one subtraction per iteration more. Each iteration here also does two unconditional branches, one of which is essentially unpredictable. There’s no way for the CPU to know in which of the two halves of the search space the element you’re looking for will be, which means that we’ll get on average one branch misprediction per two iterations. There is a way around this unconditional branch, however: It can be turned into two conditional move instructions. I haven’t found a way to tell GCC to do this, so I had to do it using inline assembly (for x86 and AMD64): Figure 11 Linear search with SSE On the horizontal axis we have the array size and on the vertical axis we have the runtime for one search in seconds. As you can see the curves for the two binary searches with conditional