Lecture 2 - Foundations of Programming and Problem Solving
Document Details
Uploaded by InviolableForesight3570
Tags
Related
Summary
These lecture notes cover the fundamentals of programming, including what a computer program is, what a program can do, the steps in developing a program, algorithms, different data types, and operators. Topics like variables, constants, and naming conventions are also discussed.
Full Transcript
Introduction to Programming Foundations of Programming & Problem Solving WHAT IS A COMPUTER PROGRAM? A program is an organized list of instructions that when executed causes the computer to behave in a pre-determined manner. It is a step-by-step list of inst...
Introduction to Programming Foundations of Programming & Problem Solving WHAT IS A COMPUTER PROGRAM? A program is an organized list of instructions that when executed causes the computer to behave in a pre-determined manner. It is a step-by-step list of instructions written in a particular computer programming language. Without programs computers would be useless. 2 WHAT CAN A PROGRAM DO? A program can only instruct a computer to: Read input Calculate Store data Compare and branch Iterate or loop Write output 3 MAIN STEPS IN DEVELOPING A PROGRAM Define the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into a specific programming language Run the program on the computer Document and maintain the program. 4 MAIN STEPS IN DEVELOPING A PROGRAM Define the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into a specific programming language Run the program on the computer Document and maintain the program. 5 ALGORITHMS A computer is only a tool that can do what it is specifically told to do. We can direct the computer to do what we want by specifying our needs in a discrete step by step manner An algorithm must be developed, which is a step by step procedure to solve a problem. 6 ALGORITHMS An algorithm must meet the following requirements: Use operations from only a given set of basic operations. Produce the problem solution or answer, in a finite number of such operations. 7 ALGORITHMS There are two types of Algorithms: Flowcharts Pseudo Code 8 IDENTIFIERS This data must be present in the memory (RAM) of the computer for operations to be done on it. The computer accesses this data via memory addresses. Identifiers are required to keep track of these memory addresses (data containers). Identifiers allow us humans to give these ‘data containers’ more meaningful names. IDENTIFIER TYPES Variables Constants WHAT ARE VARIABLES? Variables have the same meaning as variables in algebra. That is, they represent some unknown, or variable, value. x=a+b z + 2 = 3(y - 5) Are identifiers whose value can be changed within a program NAMING VARIABLES Variables may be given representations containing multiple characters. But there are rules for these representations. Variable names – May only consist of letters, digits, and underscores – May be as long as you like, but only the first 31 characters are significant – May not begin with a digit – May not be a reserved word (keyword) NAMING CONVENTIONS Programmers generally agree on the following conventions for naming variables. – Begin variable names with lowercase letters – Use meaningful identifiers – Separate “words” within identifiers with underscores or mixed upper and lower case. – Examples: surfaceArea surface_Area surface_area – Be consistent! NAMING CONVENTIONS Programmers generally agree on the following conventions for naming variables. – Begin variable names with lowercase letters or an underscore character – Use meaningful identifiers – Separate “words” within identifiers with underscores or mixed upper and lower case. Examples: surfaceArea surface_Area surface_area NAMING CONVENTIONS continu… A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A- z, 0-9, and _ ) Variable names are case- sensitive (age and Age are two different variables) Be Consistent. CONSTANTS Use all uppercase for symbolic constants. The value cannot be changed. Symbolic constants are not variables, but make the program easier to read. Examples: PI = 3.14159 AGE = 52 WHICH ARE LEGAL IDENTIFIERS? 1. AREA 6. lucky*** 2. 3D 7. area_under_pe 3. Last-Chance ri 8. num45 4. x_yt3 9. #values 10.Pi 5. num$ 11.%done BASIC DATA TYPES Real Integer Character String Boolean INTEGER TYPES Integers are numeric data items, which are either positive or negative including the following {0, 1, -1, 2, -2,...} REAL NUMBERS All "real" numbers in computers are finite approximations of the non- denumerable set of real numbers. These are two types of real numbers, Fixed Point and Floating Point Fixed Point Fixed point data items are numbers which have embedded decimal REAL NUMBERS Floating Point Floating point data items are numbers which are held as binary fractions by a computer. The numbers are expressed in a form where you have a mantissa and an Number exponent. Mantissa Exponen t 12.3 = 0.123 * 0.123 2 102 CHARACTER TYPES Character data may consist of any digit, letter of the alphabet or symbol. Character data is enclosed by a single quotation marks. Eg. ‘p’ BOOLEAN TYPE Boolean data items are used as status indicators and may contain only one of two possible values: True or False. ARITHMETIC OPERATORS Name Operator Example Addition + num1 + num2 Subtraction - initial - spent Multiplication * fathoms * 6 Division / sum / count Modulus % m%n DIVISION If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away. Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 3 DIVISION Division where at least one operand is a floating point number will produce a floating point answer. Examples : 17.0 / 5 = 3.4 4 / 3.2 = 1.25 35.2 / 9.1 = 3.86813 What happens? The integer operand is temporarily converted to a floating point, then the division is performed. DIVISION BY ZERO Division by zero is mathematically undefined. If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message. Non-fatal errors do not cause program termination, just produce incorrect results. MODULUS The expression m % n yields the integer remainder after m is divided by n. Modulus is an integer operation -- both operands MUST be integers. Examples : 17 % 5 = 2 6%3 = 0 9%2 = 1 5%8 = 5 USES FOR MODULUS Used to determine if an integer value is even or odd 5%2=1 odd 4%2=0 even If you take the modulus by 2 of an integer, a result of 1 means the number is odd and a result of 0 means the number is even ARITHMETIC OPERATORS RULES OF OPERATOR PRECEDENCE Operator(s) Precedence & Associativity Evaluated first. If nested (embedded), () innermost first. If on same level, left to right. * / % Evaluated second. If there are several, evaluated left to right Evaluated third. If there are several, evaluated + - left to right. = Evaluated last, right to left. Relational Operators < less than > greater than = greater than or equal to == is equal to (COMPARISON) != is not equal to Relational expressions evaluate to the integer values 1 (true) or 0 (false). All of these operators are called binary operators because they take two expressions as operands. PRACTICE WITH RELATIONAL EXPRESSIONS a = 1, b = 2, c = 3; Express Value Express Value ion ion a=c b=c ARITHMETIC EXPRESSIONS: TRUE OR FALSE Arithmetic expressions evaluate to numeric values. An arithmetic expression that has a value of zero is false. An arithmetic expression that has a value other than zero is true. PRACTICE WITH ARITHMETIC EXPRESSIONS a = 1, b = 2, c = 3 ; x = 3.0, y = 6.0 ; EXPRESSION NUMERIC TRUE/FALSE VALUE a+b 3 c-2*x -3.0 c-b-a 0 c-a 2 y%c 0.0 a/b 0 USING PARENTHESES Use parentheses to change the order in which an expression is evaluated. a + b * c Would multiply b * c first, then add a to the result. If you really want the sum of a and b to be multiplied by c, use parentheses to force the evaluation to be done in the order you want. (a + b) * c Also use parentheses to clarify a complex expression. PRACTICE WITH EVALUATING EXPRESSIONS Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expressions: 1. a+b-c+d 2. a*b/c 3. 1+a*b%c 4. a+d%b-c Logical Operators Logical operators are used for combining simple conditions to make complex conditions. AND- ( x > 5 AND y < 6 ) OR - ( z == 0 OR x > 10 ) NOT - (NOT (bob > 42) ) TRUTH TABLE FOR AND EXPRESSIO EXPRESSION RESULT N1 2 0 0 0 0 Nonzero 0 Nonzero 0 0 Nonzero Nonzero 1 TRUTH TABLE FOR OR EXPRESSIO EXPRESSION RESULT N1 2 0 0 0 0 Nonzero 1 Nonzero 0 1 Nonzero Nonzero 1 TRUTH TABLE FOR NOT EXPRESSION NOT EXPRESSION 0 1 Nonzero 0 OPERATOR PRECEDENCE AND ASSOCIATIVITY Precedence Associativity () left to right/inside-out * / % left to right + (addition) - (subtraction) left to right < >= left ot right == != left to right AND left to right OR left to right = += -= *= /= %= right to left , (comma) right to left SOME PRACTICE EXPRESSIONS a = 1, b = 0, c = 7 Expression Numeric Value True/False a b c a AND b a OR b !c !!c a AND ! b a=b OR b > c MORE PRACTICE Given a = 5, b = 7, c = 17 ; evaluate each expression as True or False. 1. c / b == 2 2. c % b