Introduction
•What is Programming?
–Programs = instruction to instruct machine to carry out specific task, or to solve specific problems.
–Algorithm = a step-by-step procedures that will accomplish a desired task
–Program structure = the action and the order of execution of an algorithm
–Data structure = the format and the of the data in computer memory
–Programming = the activity of communicating algorithms
–Programming process is analogues
–Programming process:
•Identifying programming task or problem
•Formulating algorithm for its solutions
•Translating the algorithm into programming language
•Testing and debugging the program
Memory Concepts
•Memory concept is a concept of storing data(input) and information (output) into the computer memory
int int1, int2, sum;scanf("%d", &int1);scanf("%d", &int2); sum = int1 + int2;
•Variable names such as int1, int2 and sum actually correspond to locations in the computer's memory
Program Structures
•Sequential execution = statement in a program are execute one after the other in the order in which they are written
•Examples of basic C program structures:
–Conditional
–Looping and Iteration
–Recursion
–Pointers
–Advanced Data Types
–File Manipulation
Program StructuresConditional
•Logical operation in C:
•These operators are used in conjunction with the following statement:
–If
–?
–switch
•The if statement has the same function as other languages.
•The ?(ternary condition) operator is a more efficient form for expressing simple if statement
•It allows multiple choice of a selection of items at one level of a conditional
•It is a far neater way of writing multiple if statement
•The break is needed if we want to terminate the switch after execution of one choice
Program StructuresLooping and Iteration
•C mechanism for controlling looping and iteration:
–The for statement
–The while statement
–The do-while statement
Program StructuresRecursion
•Recursion = function that call itself either directly or indirectly through another function
•A recursive function is called to solve a problem
•Two types of function:
–Function is called with a base case
•The function is simply returns a result
–Function is called with a complex problem
•The function divides the problems into two conceptual pieces:
–A piece that the functions knows how to do
–A piece that the function does not know how to do
Program StructuresPointers
•C uses pointers a lot, because:
–It is the only way to express some computations
–It produces compact and efficient code
–It provides a very powerful tool.
•C uses pointers explicitly with:
–Arrays
–Structures
–Functions
•Pointers = a variables which contains the address in the memory of another variable
•Pointer to any variable type is an address in memory – which is an integer address. Pointer is not an integer
•Two types of pointer:
–Unary or monadic operator & gives the ‘address of a variable’
–Indirection or dereference operator * gives the ‘content of an object pointed to by a pointer’
•Declaring a pointer to a variable:
–int *pointer
Program StructuresAdvanced Data Types
•Structures
–Structures in C are similar to records in Pascal
•Defining New Data Type
–typedef can also be used with structures.
–C allows array of structures:
•Unions
–Is a variable which may hold (at different times) objects of different sizes and type.
–C uses the union statement to create unions
•Coercion or Type-Casting
–C allows coercion.
–Coercion means that forcing one variable of one type to be another type
–Coercion in C can be done by using the cast operator ()
•Enumerated Types
–Enumerated types contain a list of constant that can be addressed in integer values
•Static Variables
–A static variable is a local to particular function
–It is only initialized once (on the first call to function)
–To define a static variable simply prefix the variable declaration with the static keyword
Program StructuresFile Manipulation
•All text file functions and types in C come from the stdio library
•Commands for read and write into a file:
•Random Access Files
–Fixed in length
–Can be accessed directly without searching through other records
–Appropriate for database system that required rapid access to specific data
Programming Style
Names:
•The names of variables and functions should be chosen with care so as to identify their meanings clearly and succinctly
•Guidelines in choosing name:
–Names should be meaningful and should suggest clearly the purpose of the function, variable and the like
–Keep the name simple for variables used on;y briefly and locally
–Use common prefixes or suffixes to associate names of the same general category
–Develop proper conventions to name your variables and functions
–Avoid deliberate misspellings and meaningless suffixes to obtain different names
–Avoid choosing cute names whose meaning has little or nothing to do with the problem
–Avoid choosing names that are close to each other in spelling or otherwise easy to confuse
–Be careful in the use of latter l, O or 0
Documentation and Format
•A good habit is to prepare documentation as the program is being written.
•Guidelines for documentation style:
•Place a prologue at the beginning of each function
•When each variable, constant, or type is declared, explain what it is and how it is used
•Introduce each significant section of the program with a comment stating briefly its purpose at action
•Indicate the end of each significant section
•Avoids comment that parrot what the code does
•Explain any statement that employs a trick or whose meaning is unclear
•The code itself should explain how the program works. The documentation should explain why it works and what it does
•Whenever a program is modified, be sure that the documentation is correspondingly modified
Refinement and Modularity
•One of the most important parts of the refinement process is deciding exactly what the task of each function is, specifying precisely what its input will be and what result it will produce
•Action of the function:
–Preconditions
•Indicates the beginning
–Postconditions
•Indicates what finishes
Program Tracing and Debugging
•Debugger is use to keep track of function calls, changes of variables and so on.
•Scaffolding technique is a snapshop that can help programmer converge quickly on the particular location where an error is occurring.
•Scaffolding is an excellence in tracing pointers errors
•Scaffolding can also help novice programmer to test for correct code
•Tracing and debugging a program is a skill to be masters.hence, put more effort to debug your own program as the only way to improve the skill is through practice
No comments:
Post a Comment