Lecture No. 1 November 1, 2013 Algorithm Algorithm is a process that a computer could carr out to complete a !ell de"ined tas# !ithin "inite time and resources$ %he ob&ective o" computer science is to solve problems b developing, anal'ing, and implementing algorithmic solutions$ Al()h!ari'imi Principle * All comple+ problems can be bro#en into simpler sub(problems$ * Solve a comple+ problem b brea#ing it do!n into smaller sub(problems and then solve them ,in a speci"ied order-, one at a time$ * .hen all the steps are solved, the original problem itsel" has also been solved$ * %his process is called Algorithm$ Different kinds of computing * Numerical / algorithms "or mathematical problems that arise in phsical sciences / 0or e+ample, integration, di""erentiation, solving a set o" linear e1uations$ * Non(numerical / everthing else / Sorting a list o" names, schedule the production process o" a "actor, account management, game plaing, control the movement o" a robot$ Computer Programming The process of planning a sequence of steps for a computer to follow. Computer Programming * Computer is a po!er"ul tool * It is not intelligent2 * In order to use computer to solve our problems, !e must tell it !hat !e !ant done and the order in !hich !e !ant it done$ * %hese instructions are called computer program$ * %his process is called computer programming$ * %he person giving these instructions is called a computer programmer$ Computer Programming * Anal'e the problem * 3evelop a se1uence o" instructions "or solving the problem$ * Communicate it to the computer$ Programming 4anguage A set o" rules, smbols, and special !ords used to construct a computer program$ 5A6A C7C88 69 Phases o" the so"t!are li"e ccle * :e1uirement 3e"inition * Analsis and design * Coding * %esting * Implementation * ;aintenance Problem Solving %echni1ues * As# 1uestions * 4oo# "or things that are similar * 3ivide and Con1uer * ;erging solutions As# <uestions * As# 1uestions until ou have developed a clear understanding o" the problem$ * .ho, .hat, .h, .here, .hen$ / .hat do I have to !or# !ith= ,m data or input- / >o! much data is there= / .hat should m output loo# li#e= / >o! man times is the process going to be repeated= / .hat are the e+ceptions to the main course= / .hat special error condition might come up= 4oo# "or things that are "amiliar * 3o not reinvent the !heel2 * 3ra! Analogies ;eans(?nds analsis * Starting point and ending state are #no!n$ * @ou need to devise the trans"ormation "unction$ * ?nds are de"ined / ou need to anal'e our means o" getting bet!een them$ * 4ahore to Islamabad / .hat are the options= / Narro! do!n the options= / 0igure out the details= 3ivide and Con1uer * Same as the Al#h!ar'imi Principle$ * 9rea#up the large problem into smaller units and then solve them one at a time$ * 9uilding bloc# approach$ 3ivide and Con1uer Hard Problem Hard ub!problem "as# ub!problem "as# ub!problem "as# ub!problem "as# ub!problem ;erging Solution * Sometimes merging t!o independent solutions solves the problem more e""icientl= * Calculate Average / Count values / Sum 6alues / 3ivide sum b count * Alternative approach / calculate partial sum as ou count Problem Solving %echni1ues * .hat is the un#no!n= / .hat is re1uired= * .hat are the data= / .hat is given= * .hat is the condition= / 9 !hat condition the un#no!n is lin#ed to the data= Conversion "rom 0ahrenheit to Celsius * Autput / %emperature in Celsius ,C- * Inputs / %emperature in 0ahrenheit ,0- * Process 32) (F 9 5 C = Calculate and print the average grade o" 3 tests "or the entire class * Input / 3 test scores "or each student * output / Average o" 3 tests "or each student * Process 1$ Bet three scores 2$ Add them together 3$ 3ivide b three to get the average C$ Print the average D$ :epeat step 1 to C "or ne+t student E$ Stop i" there are no more students Algorithm ;a#e a 5am and 9utter Sand!ich * Autput / 5am and 9utter Sand!iches * Inputs / 5am, 9utter, 9read, )ni"e, Plate * Process 1$ Put t!o slices o" bread on the plate 2$ Fsing the #ni"e, spread butter on one side 3$ Fsing the #ni"e, spread &am on the other side C$ Put the t!o slices together, clean side out D$ :epeat "rom step 1 to C to prepare more sand!iches Algorithm A%; "or !ithdra!al * Autput / ;one, error messages * Inputs / Fser Identi"ication ,A%; card-, pass!ord, amount A%; "or !ithdra!al ( Process 1$ Bet the A%; card "or identi"ication and as# "or pass!ord 2$ Chec# pass!ord 3$ I" pass!ord is not valid, generate an error message and go to step number G$ C$ Bet the amount "rom the user D$ Chec# the current balance E$ I" amount is greater than current balance, generate an error message and go to step number G$ H$ Subtract the amount "rom the balance and give out the cash$ G$ :eturn the A%; card I$ Stop
A flowchart is a visual or graphical
representation of an algorithm.
The flowchart employs a series of blocks and
arrows, each of which represents a particular operation or step in the algorithm.
The arrows represent the sequence in which
the operations are implemented. 0lo! Charts 0lo!charts / ;ost Common Smbols Symbol Symbol Name Name Function Function Terminal Terminal Represents the beginning or end of a Represents the beginning or end of a program. program. Flow-line Flow-line Represents the flow of logic. Represents the flow of logic. Process Process Represents calculations or data Represents calculations or data manipulation. manipulation. Input/utput Input/utput Represents inputs or outputs of data Represents inputs or outputs of data and information. and information. !ecision !ecision Represents a comparison" #uestion" Represents a comparison" #uestion" or decision that determines or decision that determines alternati$e paths to be followed. alternati$e paths to be followed. $lowcharts % An "&le Find the solution of a quadratic equation Ax 2 !x"#$, given A, ! and ". ST%RT INP&T %" '" ( (alculate R ) S*RT+' , --%(. % % % % /0 ) +-'1R./+,%. /, ) +-'-R./+,%. PRINT %" '" (" /0" /, 2N! 0lo! Charting ?+presses the "lo! o" processing in a structured pictorial "ormat$ Processing teps 'nput and (utput teps Decision $low of data )onnectors Terminator $low chart for )on*erting $ahrenheit into )elsius +et temp. in ,$- Print ,)- )alculate ) 32 (F 9 5 C = top .egin +et bread/ butter/ 0am/ knife/ and plate Put two slices of bread on the plate pread butter on one slice pread 1am on the other slice Put the two slices together/ stick# side in 2ant another3 4es top No $low chart for preparing a butter and 0am sandwich Add them together Di*ide the result b# three 5ore students3 4es top No $low chart for calculating a*erage of three scores +et three scores Print the a*erage +et Password 's Password )orrect3 +et amount 4es 's amount 6 .alance No +enerate "rror 5essage No +enerate "rror 5essage ubtract amount from balance 4es top 7eturn AT5 card A%; cash !ithdra!al process Dispense cash ST%RT ST%RT INP&T INP&T %" ' %" ' %dd % to ' %dd % to ' and store in ( and store in ( &TP&T &TP&T ( ( 2N! 2N! "omparison of Algorithm, flowchart and %seudo&code Step 03 'egin the calculations Step ,3 Input $alues for the $ariables % and '. Step 43 %dd the $ariables Step -3 utput the results Step 53 2nd the calculation. BEGIN Adder Input A and B C = A + B PRINT C END Adder Algorithm Algorithm $lowchart $lowchart Pseudo!code Pseudo!code