t00160030120134075[l] pert 1- algorithm & programming and introduction to c programming

46

Upload: ilma-arifiany

Post on 17-Dec-2015

23 views

Category:

Documents


5 download

DESCRIPTION

Algorithm and Programming - Introduction to C

TRANSCRIPT

  • Algorithm & Programming and Introduction to C Programming Subject: T0016 ALGORITHM AND PROGRAMMINGYear: 2013

  • TextbooksMain TextbookPaul J. Dietel, Harvey M. Deitel,. 2010. C : How to Program. PEAPH. New Jersey. ISBN:978-0-13-705966-9

    Additional TextbookJeri R. Hanly , Elliot B. Koffman. 2009. Problem Solving and Program Design in C. ADWES. Boston, Massachusetts, USA. ISBN:978-0321535429 *T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • Course DescriptionThis course comprises algorithm definition, basic principles of programming with C, how to make a program using C programming language, problem solving in C, and learning about many functions and features in C which can be used.

    By completing this course, students will have basic knowledge related with C and able to develop program using C programming language.

    This course is prerequisite for Data Structure course.*T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Learning OutcomesLO1 : Explain kind of algorithms in problem solving LO2 : Apply syntax and functions in C language in problem solving LO3 : Construct a program using C language in problem solving LO4 : Design a program with file processing using C language in problem solving LO5 : Choose the best sorting and searching algorithm in problem solvingT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Learning OutcomesAt the end of this session, student will be able to:Define algorithm theory and design (LO1)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Algorithm DefinitionAlgorithm is a procedure for solving a problem in terms of the actions to be executed, and the order in which these actions are to be executed

    Derived from the word algoris and ritmis. Introduced by Al-Khowarizmi.

    In the programming domain, algorithm define as method that consist of structured steps in problem solving using computer. T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Simple Algorithm ExampleRise and Shine AlgorithmGet out of bedTake off pajamasTake a showerGet dressedEat breakfastCarpool to work

    T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Problem DefinitionModel DevelopmentAlgorithm DesignWriting codePROBLEMPROCESS SOLUTIONAlgorithmSource CodeExecutable CodeWriting CodeCOMPILESyntax ErrExecutable code: => RunOutput ErrDocumentationAlgorithm Development StepsT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Representing AlgorithmHow to develop an algorithm?We can use:WritingStructure English and Pseudo-code.DrawingFlow ChartT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Pseudo-codeAn artificial and informal language that helps you develop algorithmsPseudo-code is similar to everyday English, convenient, and user friendlyKeywords are used to describe control structureExample:if, else, print, set, add, while, etc.T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Pseudo-codeBasic Computer Operation:Input OutputComputeStoring value to an identifier (Store)CompareRepetition (Loop)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Pseudo-code ExampleExample : Algorithm using a calculatorStartSet the calculator ONEmpty any valuesDoInput pricePush plus button (+)while all prices have been inputprint total price turn OFF calculatorEndT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Pseudo-code ExampleExample : Algorithm Class AverageStartSet total to zeroSet grade counter to oneWhile grade counter is less than or equal to tenInput the next gradeAdd the grade into the total Add one to the grade counterSet the class average to the total divided by tenPrint the class average.EndT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Flow ChartT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Start d = b^2 4ac d < 0Print messageImaginary x1=(-b+sqrt(d))/2a x2 =(-b-sqrt(d))/2aPrint: x1, x2StopYTFlow Chart Example Input a,b,cT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Good Algorithm PracticeHaving the right logical flow to solve the problem Producing the correct output in a time efficient manner

    Written using unambiguous structured language

    Easy implementation into real programming language

    All steps and operations are clearly defined and endedT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Structure Theorem Structure theorem which makes the computer programming possible using only three control structure, which are: Sequence SelectionRepetition T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *1. Sequence Sequence is series of consecutive commands/statements

    Commonly programming language has sequence of statements flowing from top of the program to its endT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *2. Selection Selection control structure is structure that allow us to choose from several options of statement/command

    The first statement will be executed if the condition is satisfied, if not then the else statement will be executed (if the other exist)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *3. Repetition A number of statements/commands can be repeated several times using Repetition structure control

    Statements/commands will be repeated while the looping condition is satisfied(may use DOWHILE ENDDO)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *History of CC evolved from two previous languages, BCPL and B.BCPL was developed in 1967 by Martin RichardsIn 1970, Ken Thompson used B to create early versions of the UNIX operating system at Bell LaboratoriesC language was evolved form B by Dennis Ritchie at Bell Laboratories and was originally implemented on DEC PDP-11 computer in 1972The publication in 1978 of Kernighan and Ritchies book, The C Programming Language1983 X3J11 technical committee was created to make a standard of the language1989 Standard was approved1999 The standard was updatedC99 is a revised standard for the C programming languageT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Why Using CFlexibilityClose to low level machine language yet easy to understand

    PortabilityUsed form micro computer to super computer

    A Well Known Programming LanguageIt is used in many forms of implementations such as O/S, scientific application, business application, etc.

    Supported With a Large Number of LibrariesT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *C Standard LibraryWhen programming in C, youll typically use the following building blocks:

    C Standard Library FunctionsExample:- : Mathematical Functions- : Input and Output- : Utility Functions- : String Functions- : Time and Date FunctionsFunctions you create yourselfFunctions other people have created and made available to youT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *C StructureC language is a structural programming languageIt consists of functionsThere is no separation between function and procedure (if you are from Pascal language background)Each C program has one main function called mainProgram will be started from the first line of the main functionC language is case sensitiveEvery statement should be ended with a semi-colon (;)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *C StructureT0016 - Algorithm and Programmingmain(){statements;}

    main(){statements;return(0);}void main(){statements;}

    int main(){statements;return(0);}1.2.3.4.

    T0016 - Algorithm and Programming

  • *CommentsUsed for readability of the programNot accounted as a command/statement by the compilerUsing /* and */ Using // at the beginning of line for one line commentExample:T0016 - Algorithm and Programming/*--------------------------My First Program --------------------------*/#includevoid main(){printf (Hello, BINUSIAN\n);}// This program will simply print out a message

    T0016 - Algorithm and Programming

  • *Escape Sequences\abell, alert, system beep\bback space\thorizontal tab\nnew line, line feed\vvertical tab\rcarriage return\single quote\double quote\\backslash\xddhexadecimal notation\dddoctal notationT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *CharacterC program is written using ASCII character subset:Capital letters AZLower Case azDigit 09Special characters !, &, +, \, _, etc.

    ASCIIAmerican Standards Committee for Information Interchangehttp://www.asciitable.com/T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *IdentifierThe naming mechanism for various element in a program such as: variable, function, constant, etc.Started with a letter or underscore_It is case sensitiveMaximum length is vary for every compilerExample: Turbo 2.0 (DOS), max 32 charactersNever use reserved word/keyword(such as: for, while, if, main)Example:name, x1, _total, cubic()wrong: 1time, intT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *KeywordsKeywords/reserved words are words that have special meaning to the C compiler.Example:

    Keywords added in C99_Bool _Complex _Imaginary inline restrict

    T0016 - Algorithm and Programming

    Keywordsautobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhile

    T0016 - Algorithm and Programming

  • *VariableIdentifier for storing data/informationEach variable has its name, address (L-value), type, size and data (R-value)Data or variable value can be modified at run timeDeclaration format: ; = ;Example:int a, b, c, total;float salary, bonus;int num_students = 20;T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *VariableVariable Declaration:Variable can be declared at every statement blockBlock statement or compound statement is statement exists between { and } signExample:T0016 - Algorithm and Programmingint x;int y;int z;

    or:int x, y, z;

    or:

    int x; int y; int z;

    T0016 - Algorithm and Programming

  • *Data TypeIn C, there are 5 data types and 4 modifiersData types:Character charInteger intFloating point floatDouble floating point doubleVoid voidModifiers:signedunsignedlongshortT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Data TypeData type and its range on TURBO C 2.0 (DOS)T0016 - Algorithm and Programming

    DATA TYPESYNTAXMEMORYRANGEcharacterunsigned char1 byte0 to 255char1 byte-128 to 127integerunsigned int2 byte0 to 65535int2 byte-32768 to 32767short int1 byte-128 to 127unsigned long4 byte0 to 4294967295long4 byte-2147483648 to 2147483647floatfloat4 byte3.4E-38 to 3.4E+38double8 byte1.7E-308 to 1.7E+308long double16 byte3.4E-4932 to 1.1E+4932

    T0016 - Algorithm and Programming

  • *Data TypeBeside used in function identifier type as no return value, keyword void also used as data type in variable.

    Void data type: is data type that can be transform into any data type (will be discussed later in pointer)

    T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *ConstantConstant / symbolic constant does not have address (only value) and its value can not be changed at run time.Constant type: Integer constant -5 Floating-point constant 3.14 Character constant 'C' '1' '$' Escape sequence \n \t \'' String constant ''BiNus'' Symbolic constant #define PHI 3.14 const float PHI=3.14; 'His a character constant ''H'is a string constant 1is a integer constant '1is a character constant const float Pi= 3.1415926; Pi is a symbolic constant

    T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *Sizeofsizeof is an operator to find out size of a data type in C language

    Syntax: sizeof expression

    Example :sizeof(int) = 4 => Dev-V (Windows)sizeof(int) = 2 => Turbo C ver 2.0 (DOS)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *SuffixC provides suffix for floating point constant: F or f for float data type L or l for long double data type Default double data type

    Example : 3.14 (double) 3.14f (float) 3.14L (long double)

    T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *SuffixC provides suffix for a constant integer:U or u for unsigned integerL or l for long integerUL or ul or LU or lu for unsigned long integerDefault integer

    Example : 174 (integer) 174u (unsigned integer) 174L (long integer) 174ul (unsigned long integer)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *SuffixSome compilers will give warning for differ in data type, as can be seen from the following example Visual C++:Example :float x;x = 3.14; warning: truncation from 'const double' to 'float

    How to deal with the issue? You may use casting or suffixfloat x;x = (float)3.14;// castingx = 3.14f;// or suffix

    T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *SummaryAlgorithm is a procedure for solving a problem in terms of the actions to be executedAlgorithm development steps consists of: problem definition, model development, algorithm design, writing code, and documentationWe can use writing (Structure English and Pseudo-code) or drawing (Flow Chart) to represent algorithmBasic Computer Operation: input, output, compute, store, compare, and repetition (loop)Structure theorem are sequence, selection, and repetitionT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *SummaryC is used because: flexibility, portability, well known programming language, supported with a large number of librariesC language is a structural programming languageC language consists of functionsC program has one main function called mainC language is case sensitiveEvery statement in C language should be ended with a semi-colon (;)T0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *ReferencesPaul J. Dietel,Harvey M. Deitel,. 2010. C : how to program. PEAPH. New Jersey. ISBN:978-0-13-705966-9 Chapter 1 & 2Writing Your First C Program: http://aelinik.free.fr/c/ch02.htmData Types and Names in C: http://aelinik.free.fr/c/ch04.htmT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *ReferencesPaul J. Dietel,Harvey M. Deitel,. 2010. C : how to program. PEAPH. New Jersey. ISBN:978-0-13-705966-9 Chapter 3Programming in C: http:// www.cs.cf.ac.uk/Dave/C/C Language Tutorial: http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/Pseudocode Examples: http://www.unf.edu/~broggio/cop2221/2221pseu.htmComputer & Internet Help : Understanding Flowchart Symbols: http://www.youtube.com/watch?v=xLoL7tlJYwsT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

  • *

    ENDT0016 - Algorithm and Programming

    T0016 - Algorithm and Programming

    *******