Вы находитесь на странице: 1из 9

Computing Basics

Structures What: a collection of related variables, which may or may not be of the same data type, usually concerning a certain entity (e.g. mailing address, library book, student etc.) used to group data types together Notes: Dot operator (.) is of higher precedence than the * operator

Array What: collection of data of the same data type 2-d Arrays: variable[row][column] String: character array terminated by a NULL character Typecasting: typedef char name[50] name tableNames[10] tableNames names (10 rows of 49 columns) [Compiled by and Copyright. http://crustedtwits.blogspot.com] Pointers What: A variable that stores the address of a variable; it points to a variable When to use: to allocate memory dynamically, using pointers to keep track of what you ve allocated Reference: A placeholder for the address of a variable. You cannot create a reference that is not assigned to anything. Pass by reference/pointer: only address of parameter is passed in, able to alter parameter (%Variable Parameter) Pass by Value: a copy of the value is made/separate memory cell is made (inefficient, waste space), address is not passed, unable to alter the parameter passed in (%Value Parameter) Notes: *-used to declare or dereference a pointer &-used to denote address of a variable

%Parameters What: used to communicate information between calling function and function being called (value-returning function or void functions). A parameter MUST BE a variable. Variable Parameters: uses the memory cell of the corresponding global variable in the calling statement Local variables: declarations declared within a block only accessible within the block they are defined in. They are created when function is called and disappears when function finishes. They are used when one function needs it but statements outside that function do not.

Random Number Generator What: rand function generates pseudo-random numbers [Compiled by and Copyright. http://crustedtwits.blogspot.com] Recursive Functions What: body of subprogram calls itself When to use: Original task can be broken down/reduced to simpler version of itself (repeating) making problem easier to handle, solve and code Terminal case: stops recursive calls at when a criteria is met; ensure successive recursive calls do not continue indefinitely Trace diagram: traces each step (each time function is called) of the execution of a recursive function

Structure Charts What: depicts the overall top down structure of a program that includes an analysis of any data flow between main body and procedures Symbols: y y Down arrow: IMPORT value imported by a function via value parameter Down & Up arrow together: IMPORT AND EXPORT global variable updated based on old value via variable parameter

Up arrow: EXPORT global variable receives a new value not based on old value via variable parameter

Backus-Naur Form (BNF) What: formal notation used to describe the grammar of a language = defines the structure of another language Why use this: a standard language to ensure no disagreement or ambiguity to what is allowed and what is not allowed Symbols: y y y ::= means is defined by | meta symbol meaning or <digit>::= 1|2|3 (note: brackets encloses a non-terminal symbol while 1,2,3 are terminal symbols)

[Compiled by and Copyright. http://crustedtwits.blogspot.com] Syntax Diagram What: drawings that illustrate valid language syntax Advantages over BNF: Syntax diagrams provide reader with better understanding of the rules of the language through visual representation and offer shorter descriptions than BNF rules. Symbols: y y y Non-terminal is boxed up Terminals are circled Multiple alternative paths: ----> [] ---> |--> [] ---| |--> [] ---| Zero or more repetition --------------> |<--- [] ---| One or more repetition -----> [] ----> |<---------|

Writing a Program: 1. Understand the problem a. Define the problem clearly i. State objectives to be accomplished ii. Specify users of program b. Analyze the problem i. Identify input data, constant data and output data 2. Design and develop the solution by writing algos 3. Code/Implement the Algo using appropriate programming language and following its syntax rules 4. Testing and Debugging 5. Document the solution describe its purpose and process [Compiled by and Copyright. http://crustedtwits.blogspot.com] Basic Terminology Program: list of instructions the computer must follow in order to process data into information Structured Programming: a methodology for developing programs emphasizes use of modules in program design and implementation Modularity: solution to problem is a collection of well-defined separate modules. A module is a complete part-program with a specific purpose that is used from within the main program.  Cost/Time efficient: Modules can be shared between, coded and tested separately by a team of programmers  Benefits of smaller modules: Large project becomes easier to control and monitor; Easier to maintain and debug if a module is not working correctly  A library of modules can be stored for use in other programs  Larger memory space needed possible to over modularize  More files to be managed = higher risk of losing files  Cannot see all code or refer to all documentation Pseudo-code [logic modeling]: essentially English with some defined rules of structure and some keywords that make it appear a bit like programming code + Allows us to focus on the logical details of the program [internal structure and finality] without getting bogged down by syntax rules [generic; independent of programming language] It has no single form; its varying format may be confusing to share among many programmers hence the need for standardization

Algorithm: A step-by-step process or sequence of instructions that can be used to solve a problem in a finite amount of time Good algos: Easily understood by others (programmers) Solves the problem efficiently and cleanly Compiling: process of converting source files into object files or codes Compiler: translate the code into an intermediary form. Compiling produces an object file Linking: process that links all the object files needed by your program into an executable file *Iteration is required to search for compile-time errors, linking errors, run-time errors. [Compiled by and Copyright. http://crustedtwits.blogspot.com] Debug: detect, locate and remove all errors in a computer program Techniques: y Specifying a break point in the source code, causing program to execute up to that point to evaluate different portions of the code y Display procedure stack to list sequence of procedures (trace facility) y Marking portions of code out as comments to be excluded in the testing y Desk-checking: reading through the program to make sure it is free of errors and that the logic works: o freeze the algorithm at any point and taking a snap-shot of the state of all the variables using a trace table o trace table consists of rows which state the step in the algorithm and columns which specify the value of the variables at that step Interpreter: translate a program as it reads it, turning program instructions or code into actions Loops: execute the same section of program code over and over as long as the loop condition as specified by program is met with each iteration

Fundamental Algorithms
Sorting
Good performance factors: number of comparisons and number of exchanges Internal Sorting 1. Exchange Sort For each pass, the item at [0] is compared with subsequent item at [1], [2] [n]. For EACH comparison, if the subsequent < [0], the two entries are exchanged. Next pass, [1] compared until [n], passes end after [n-1] is compared. for (i=0;i<n-1;i++) for(j=i+1;j<n;j++) if (a[i]>a[j]) swap a[i] with a[j] 2. Selection Sort + Simple, good for large lists - Too inefficient, no better for lists that are almost sorted than for random lists For each pass, select the smallest element in the list and exchange with [0]. for (i=0;i<n-1;i++) smallestIndex = i for(j=i+1;j<n;j++) if (a[i]>a[j]) smallestIndex = j swap a[i] with a[smallestIndex] 3. Insertion Sort + Better than selection and bubble because less scanning is required low overhead - Too inefficient For each pass, from the back (using index j), shift elements up until correct location is found for the new item to be added for (i=0;i<n;i++) j=i while(j>0 and NewItem < a[j-1]) a[j] = a[j-1] //shifting elts j-a[j] = NewItem //location is found

4. Bubble Sort + Best for partially ordered lists; able to detect when a list is sorted and terminates process preventing unnecessary passes - NOT good for long lists as many, many swaps needed y Requires UP TO n-1 passes, can be less y Maintain a record of LastExchangeIndex, set to 0 before each pass, terminating the sort when this Index = 0 For each pass, compare adjacent elements and exchange their values if first elt > second elt. Largest elt ends up at the back. lastExchangeIndex=n-1 while (lastExchangeIndex>0) lastExchangeIndex=0 for(j=0;j<n;j++) if (a[j]>a[j+1]) swap and set lastExchangeIndex=j 5. Quick Sort + Best for loooooong list! It is the most efficient sort! y Pivot value = a[mid], separates the list into two sub lists, Low (contain values <= a[mid] ) and High (contain values > a[mid]). Shift a[mid] to the start of list first. y Set ScanUp = 1. This locates elements for sub list Low. y Set ScanDown. This locates elements for sub list High. y ScanUp moves up the list. When a[ScanUp]>pivot, stop. ScanDown moves down the list. When a[ScanDown] <= pivot, we have 2 elements in the wrong order swap a[ScanUp] and a[ScanDown] y Stop when ScanUp > ScanDown. Exchange pivot a[0] with a[ScanDown] y Recursive Phase: process the two sub lists separately using the same algo External Sorting Merging y Open F1 and F2 for input, F3 for output y Read the first element x from F1 and first element y from F2 y If x<y, write x to F3, read new x value y Else, write y to F3, read new y value y When end of either file is encountered, copy remaining elements from other file into F3 Merge Sort (sorting File) y Copy elements of File alternatively into F1 and F2 y Read x from F1 and y from F2. If x<y, copy x then y into File, read new values of x & y y Split elements of File alt-ly 2 by 2 into F1 and F2 y Sort by pairs into File again y Repeat until F1 & F2 contain one element block each. Compare and write to File

Searching
Sequential/Linear search Traverse list continuously, comparing each item with the target key, until key is found OR list is exhausted!!

Binary Search Improved, faster search technique for ordered lists: go to middle, compare with midpoint value, and move to lower or upper half of list and repeat.

Hashing Location of an item is determined by a function of the item Very good because only one location needs to be examined Collision Strategies Linear Probing Linear search of table begins at location where collision occurs and continues until an empty slot is found to store new item To FIND an element: Conduct hash function on item y If location is empty, we can immediately conclude item is not in list y If location contains item, search is immediately successful. y If location contains another value, conduct linear probing until item is found OR empty slot is reached = item no in list Chaining Uses a hash table that is an array of linked lists that store items It is not only a faster method, space is also dynamically allocated.

Abstract Data Type


What: A collection of related data items with an associated set of operations Implementation of ADT: Data structures to store data items and algorithms for the basic operations **Data Abstraction** = independence from implementation: Separating definition of a data type from its implementation ADT Array is Data A finite sequence of elements all of the same data type Operation Index operator [] allows direct access to specific element End ADT Array ADT Structure is Data A finite sequence of elements they may or may not be of same data type Elements are called members/fields Operation Access dot operator (.) allows direct access to each member End ADT Structure ADT List is Data A finite sequence of data items Operation Construction, Empty, Traverse, Insert, Delete End ADT List Linked Lists What: ordered collection of elements called NODES each of which consists: DATA part (stores element) and NEXT part (store the link/pointer indicating location of node containing next elt) + Store items without restrictions on location/space (dynamic allocation) + Access any item as long as (one) external link is maintained (first) + Insert and delete without shifting of elements NO direct access to elts! Slow processing! All values except first are accessed sequentially (an avg of n/2 accesses are required to access a node)

Вам также может понравиться