CE 101 Algorithms and Programming I PDF

Summary

This document is an introduction to algorithms and programming, covering mathematical, comparison, and logical operations, along with several examples.

Full Transcript

CE 101 Algorithms and Programming I Introduction to Programming Content Introduction Operations Mathematical (Arithmetic) Operations Comparison Operations Logical Operations Introduction Business Machine,...

CE 101 Algorithms and Programming I Introduction to Programming Content Introduction Operations Mathematical (Arithmetic) Operations Comparison Operations Logical Operations Introduction Business Machine, equipment, etc. Raw Material Product Human Computer Program Data Result Human Operation flow in business and in computer Introduction Whole Program Computer Main Program Hardware Software Sub- Sub- Program 1 Program n System Application Sub- … Program 2 Introduction Programming Errors Syntax Errors Logic Errors Operations Computer operations are divided into three groups as Mathematical (Arithmetic) operations Relational (Comparison) operations Logical operations Mathematical (Arithmetic) Operations Addition, subtraction, multiplication, division, exponential, logarithm etc. Mathematical expressions written/coded in computer language have order of precedence. Operator priority is very important. Mathematical (Arithmetic) Operations a b a^b a/b a*b a+b a-b -2 6 64 -0.33333 -12 4 -8 -1 7 -1 -0.14286 -7 6 -8 0 8 0 0 0 8 -8 1 7 1 0.142857 7 8 -6 2 6 64 0.33333 12 8 -4 Mathematical (Arithmetic) Operations Order of precedence of mathematical operators Order Operation Programming Language 1 Negativity of numbers -… 2 Parenthesis (…) 3 Mathematical functions cos, sin, ln, log, exp, … 4 Exponentiation a^b, pow, … 5 Multiplication and division a*b and a/b 6 Addition and subtraction a+b and a-b Mathematical (Arithmetic) Operations Example: Y=A*B/C Left to right Example: Y=A+B-C+D Examples 1. If x=5+7%2, then what is x? 2. If x=5+(7%2), then what is x? 3. If x=(5+7)%2, then what is x? Parentheses can be used to avoid operator precedence rule. If there are multiple operators at the same priority level, grouping tells which should be evaluated first (left to right or right to left). Enclosing all sub-statements in parentheses improves the readability of the code. From mathematical expression to algorithm coding: Example Find the results of the following expressions for a=4, b=6, c=8, and d=10. c*d/(a*d)+b+c*d/a c*d/a/d+b+c*d/a c*d/a*d+(b+c)*/a Answer: 28, 226, 235 respectively. From algorithm coding to mathematical meaning: Find the results of the following expressions for a=1, b=2, c=3, d=4, and e=-2. Answer: 20, 23, 2.04, 6.06 respectively. Find the results of the following expressions for a=-1, b=7, c=4, d=2, and e=6. Answer: -14, 33, -18.72 respectively. Relational Operators Example: 1. Start 2. Enter A 3. Enter B 4. If A>B, then output «A is greater than B.» 5. If AB, then print «First one comes alphabetically later.» 5. If A25 AND Salary=MinimumWage, then print Name Example In a class, the names of those students who have a grade above 65 in the "Computer" course and get a grade above 65 in any of the "Turkish Language" or "Foreign Language" courses are requested. Example In a class, the names of those students who have a grade above 65 in the "Computer" course and get a grade above 65 in any of the "Turkish Language" or "Foreign Language" courses are requested. If Computer>65 AND (TL>65 OR FL>65), print Name C=50, TL=50, FL=50?? C=95, TL=50, FL=60?? C=70, TL=70, FL=60??

Use Quizgecko on...
Browser
Browser