390 likes | 501 Vues
Lecture 3 By Zahoor Ahmad Khan zahoor_ahmad_khan@hotmail.com Source of this lecture: ---- (Material is taken from World Wide Web, recommended books, online tutorials)-----. Introduction to Computer Programming. What is a Computer Program?.
E N D
Lecture 3 By Zahoor Ahmad Khan zahoor_ahmad_khan@hotmail.com Source of this lecture: ---- (Material is taken from World Wide Web, recommended books, online tutorials)----- Introduction to Computer Programming
What is a Computer Program? • A computer program is generally designed to solve a problem • A problem exists when what you have is not the same as what you want • What you have is called DATA • What you want is called INFORMATION • A Computer Program manipulates data to produce information
What does a Computer Programmer do? • A computer needs very simple step-by-step instructions to function • A person can usually solve problems in quick and complex ways • A programmer creates solutions to problems • A programmer then breaks the solution down into its simplest steps • These steps are then translated into computer code • Creating solutions to problems, and then creating simple step-by-step instructions for a computer based on the solutions is the job of a Computer Programmer
How are Computer Programs Created? • The solution is translated into computer code using a text editor and a specific Computer Language • The computer code (known as “Source Code”) is Compiled. This creates a machine language file with instructions the computer can understand (called an “Object File”) • The Object File is then Linked and an Executable File is created, which can be run by the computer
Computer Languages • Machine Language (true computer code) • Symbolic Languages (Assembly) • Middle level languages (C) • High Level Languages (Java, C#) • Natural Languages
Example Print Command for • Machine Language is 0101010001011010100001 • Symbolic Languages is “PR” • High Level Languages “PRINT” • Natural Languages
How Computer languages are differentiated? • They are differentiated on the basis of number of lines/number of Constructs (smallest unit of an instruction to a computer) of code for a problem… • Like C language has 3 line code for a problem • Assembly language has 10 lines of code for the same problem • Machine language has 30 lines of code for the same problem. • Natural Language or high level languages have/may have less then 3 lines of code for same problem
How People Solve Problems • A Problem exists when what we have (Data) is not the same as what we want (information) • People create a solution (called an Algorithm) which manipulates Data into Information • People do this quickly and often in a complex way
How Computers Solve Problems • Computers also use Algorithms to solve problems, and change data into information • Computers can only perform one simple step at a time • Complex “Human” Algorithms must be broken down into simple step-by-step instructions BEFORE they can be translated into computer code
Algorithms (source : Wikipedia) • In mathematics, computing, linguistics and related disciplines, an algorithm is a procedure (a finite set of well-defined instructions) for accomplishing some task which, given an initial state, will terminate in a defined end-state. In simple words an algorithm is a step-by-step procedure for calculations. • Algorithms are essential to the way computers process information, because a computer program is essentially an algorithm that tells the computer what specific steps to perform (in what specific order) in order to carry out a specified task, such as calculating employees’ paychecks or printing students’ report cards. Thus, an algorithm can be considered to be any sequence of operations which can be performed by a Computer.
Problem Solving • Problem Solving is the ability to understand what you have, what you want, and creating a set of instructions to change what you have into what you want • Good Problem Solving Skills are based on knowledge, experience and logic • Good Programmers NEVER make assumptions
Expressing the Algorithms • A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily • Following ways/standards are used for expressing algorithms: • NATURAL LANGUAGE • PSEUDOCODE • FLOWCHARTS • PROGRAMMING LANGUAGE
Expressing The Algorithms • Algorithms can be expressed in many kinds of notation, including natural languages, pseudo code, flowcharts, and programming languages. Natural language expressions of algorithms tend to be ambiguous, and are rarely used for complex or technical algorithms. Pseudo code and flowcharts are structured ways to express algorithms that avoid many of the ambiguities common in natural language statements, while remaining independent of a particular implementation language. Programming languages are primarily intended for expressing algorithms in a form that can be executed by a complete, but are often used as a way to define or document algorithms.
Natural Language • "...prose to describe an algorithm, ignoring the implementation details. At this level we do not need to mention how the machine manages its tape or head“ • In the philosophy of language, a natural language (or ordinary language) is a language that is spoken, written, or signed (visually or tactilely) by humans for general-purpose communication, as distinguished from such constructs as computer-programming languages or the "languages" used in the study of formal logic, especially mathematical logic.
Pseudo Code • “Pseudo” means “pretend” or “false” • Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like computer code • Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does
Pseudocode (wikipedia) • Pseudo code (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. The programming language is augmented with natural language descriptions of the details, where convenient.
Flowcharts • A Flowchart is a Visual Representation of an algorithm • A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data • Flowcharts aid in breaking down a problem into simple steps
Selection • Selection is the decision-making construct • It is used to make yes/no or true/false decisions logically • Selection can be thought of as “if something is true take this action, otherwise take that action”
The Three Computer Programming Constructs • Any problem, regardless of how complex, can be broken down into three basic CONSTRUCTS • SEQUENCE • SELECTION • ITERATION
Sequence • Sequence is the most basic of the constructs • It is simply performing one step after another • Each step is followed in a specific sequence, hence the name • Sequence can be thought of as “do this, then do this, then do this”
Iteration • Iteration comes from the word “reiterate”, which means to repeat • Iteration is a looping construct • Iteration is a combination of decision and sequence and can repeat steps • Iteration can be thought of as “while something is true, do this, otherwise stop”
Visualizing The Constructs • By using Flowchart symbols, we can visualize the three basic computer constructs • Every algorithm ever created is made up of only these three constructs
Basic Flowchart Symbols Process Input/Output Terminator Decision Connector Data Flow
The Basic Flowchart Symbols • The terminator shows where a specific piece of an algorithm beings and ends • A process is a basic action or actions on data • A decision is a logical test with two possible data paths resulting from one
The Basic Flowchart Symbols • Input/Output gets data or displays information to the user of the algorithm • A connector connects two or more data flows into one • A data flow shows the direction of data’s movement through the algorithm
Selection F T
Iteration F T
Example : • You have problem when staying at home. Your lamp doesn’t work and you need the light. Express the algorithm to solve the problems !
Algorithms : • Lamp doesn’t work • Is lamp plugged in ? • If No, plug in lamp and finish • If yes, Check the bulb if it is burned out ? • If yes, replace bulb • Buy new lamp
Example : One of the simplest algorithms is to find the largest number in an (unsorted) list of numbers. The solution necessarily requires looking at every number in the list, but only once at each. From this follows a simple algorithm, which can be stated in a high-level description English prose, as:
Natural language: Assume the first item is largest. Look at each of the remaining items in the list and if it is larger than the largest item so far, make a note of it. The last noted item is the largest in the list when the process is complete.
(Quasi-) Formal description: Written in prose but much closer to the high-level language of a computer program, the following is the more formal coding of the algorithm in pseudo code : Algorithm LargestNumber Input: A non-empty list of numbers L. Output: The largest number in the list L. largest ← L0 for eachitemin the list L≥1, do if the item > largest, then largest ← the item returnlargest • "←" is a loose shorthand for "changes to". For instance, "largest ← item" means that the value of largest changes to the value of item. • "return" terminates the algorithm and outputs the value that follows.
Home work! • Make an algorithms in high level language, pseudo code and flowchart for • Computing factorial N (N!) ! • Check if a number is even or odd • Compute first 10 numbers of Fibonacci sequence, i-e 1,1,2,3,5,8,13,21…… • Check if a number is a prime number i-e 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, ...