Computer Programming CS102_Introduction

download Computer Programming CS102_Introduction

of 48

Transcript of Computer Programming CS102_Introduction

  • 8/18/2019 Computer Programming CS102_Introduction

    1/48

    Computer Programming

    (CS-102)Week 1

  • 8/18/2019 Computer Programming CS102_Introduction

    2/48

    Topics

    • Data and Data types, Variables, Loops, Arrays,Functions, Switch, Casting, Pointers,Overloading, Object-Orientation: Structures,Classes (Encapsulation, Data Hiding,Inheritance, Polymorphism, Composition)

  • 8/18/2019 Computer Programming CS102_Introduction

    3/48

    Course & Reference Books

    • C++ How To Program (Eight Edition), Dietel• Object-Oriented Programming in C++ (Third

    Edition or higher), Robert Lafore

  • 8/18/2019 Computer Programming CS102_Introduction

    4/48

    C++ IDEs

    • Eclipse• Netbeans

    • Dev C++• Microsoft Visual Studio 2005/2008/2010

  • 8/18/2019 Computer Programming CS102_Introduction

    5/48

    Computer Programming Review

    • What is computer programming?

    Computer programming is the art of writinginstructions that makes a computer performa certain task.

  • 8/18/2019 Computer Programming CS102_Introduction

    6/48

    Computer Programming Languages

    • What is the natural language of a computer?

    A computer naturally understands a machinecode or binary code i.e. a string of 0s and 1s.

  • 8/18/2019 Computer Programming CS102_Introduction

    7/48

    Classification of ProgrammingLanguages

    • Low-level Languages• High-level Languages

  • 8/18/2019 Computer Programming CS102_Introduction

    8/48

    Who is a good programmer?

    You can tell if you are a good programmer…

    1) You can understand your own code 12months later2) Others can make changes to your code.

  • 8/18/2019 Computer Programming CS102_Introduction

    9/48

    A Program• A program is a very detailed list of steps which must be followed to

    accomplish a certain task

    Example of a daily routine:1. Wear clothes according to particular weather2. Buy some tickets3. Go to a nearby bus stop4. Wait for the correct bus5. Get on6. Pay the driver

    7. Sit down where empty seats are available8. Wait until the bus arrived in the area you wished to go to9. Alert the driver as your bus stop comes10. Wait for the bus to stop11. Finally get off

  • 8/18/2019 Computer Programming CS102_Introduction

    10/48

    Flowchart ofthe example

    • Can you identifynumber ofloops?

    • Terminator• Process• Connector• Decision

  • 8/18/2019 Computer Programming CS102_Introduction

    11/48

    Programming a Robotic Arm

  • 8/18/2019 Computer Programming CS102_Introduction

    12/48

    Programming Techniques

    • The evolution of programmingtechniques is

    – to make programming languages moreexpressive – to develop complex systems more easily

  • 8/18/2019 Computer Programming CS102_Introduction

    13/48

    Programming Techniques (Contd..)

    • Unstructured Programming• Procedural Programming

    • Modular & Structural Programming• Abstract Data Type• Object-Oriented Programming

  • 8/18/2019 Computer Programming CS102_Introduction

    14/48

    Unstructured Programming

    • Usually, people start learning programming by writingsmall and simple programs consisting only of one mainprogram. Here ``main program'' stands for a sequence ofcommands or statements which modify data which is

    global throughout the whole program

    • Drawbacks:This programming technique can only be used in a verysmall program. For example, if the same statementsequence is needed at different locations within theprogram, the sequence must be copied. If an error occursAnd needs to be modified then every copy needs to bemodified

  • 8/18/2019 Computer Programming CS102_Introduction

    15/48

    Procedural Programming

    • With procedural programming, you are able tocombine sequences of calling statements intoone single place.

    • A procedure call is used to invoke theprocedure. After the sequence is processed,flow of control proceeds right after theposition where the call was made .

  • 8/18/2019 Computer Programming CS102_Introduction

    16/48

    Procedures

    • With parameters and sub-procedures(procedures of procedures) , programs cannow be written more structured and error free

    • For example, if a procedure is correct, everytime it is used it produces correct results

    • Consequently, in cases of errors you cannarrow your search to those places which arenot proven to be correct

  • 8/18/2019 Computer Programming CS102_Introduction

    17/48

    Procedure Program view

    Main Program

    Data

    Procedure 1 Procedure 2 Procedure 3

  • 8/18/2019 Computer Programming CS102_Introduction

    18/48

    Modular Programming

    • Modular programming is subdividing yourprogram into separate subprograms such asfunctions and subroutines.

    • With modular programming, procedures of acommon functionality are grouped togetherinto separate modules.

    • A program therefore no longer consists of onlyone single part. It is now divided into several

    smaller parts which interact throughprocedure calls and which form the wholeprogram

  • 8/18/2019 Computer Programming CS102_Introduction

    19/48

    Modular Programming

    Main Program(Also a module)

    Data

    Data Data 1

    Module 2+

    Data Data 2

    Module 1+

    Data Data 1

    Procedure 1 Procedure 2 Procedure 3

  • 8/18/2019 Computer Programming CS102_Introduction

    20/48

    Modular Programming

    • Each module can have its own data. Thisallows each module to manage an internalstate which is modified by calls to proceduresof this module.

    • Each module has its own specialfunctionalities that supports theimplementation of the whole program

  • 8/18/2019 Computer Programming CS102_Introduction

    21/48

    Structural Programming

    • How many basic structures for programming?• Three Types of Structures in a structured

    program

    • Statement sequence(s1,s2,…,sn)

    • Branch (if-then-else)

    • Loop (for,do, and while loops)

  • 8/18/2019 Computer Programming CS102_Introduction

    22/48

    Data and Data Types

    • C++ is a strongly typed language, in otherwords every data item in your program has atype associated with it

    • Your C++ compiler will make extensive checksto ensure that, as far as possible, you use theright data type in any given context

    • There are two broad classifications of numericconstants that you can use in C++1) Integer literals 2) Floating Point literals

  • 8/18/2019 Computer Programming CS102_Introduction

    23/48

    Everyday Decimal Numbers

    • Lets consider an example

    324 can be written as 3x10^2+2x10^1+4x10^0

    which is 3x10x10+2x10+4

    We call it decimal notation because it is builtaround powers of 10Representing numbers in this way is very handyfor people. However, your PC is rather less handy,Being built mainly of switches that are either on or

    off

  • 8/18/2019 Computer Programming CS102_Introduction

    24/48

    Binary Numbers

    • Representing numbers using base 2 is calledthe binary system of counting. Digits can onlybe 0 or 1, which is ideal when you only have

    on/off switches to represent them. In an exactanalogy to our system of counting in base 10,the binary number ’1101’ breaks down likethis:

    1x2^3+1x2^2+0x2^1+1x2^0 = (13) to base 10

  • 8/18/2019 Computer Programming CS102_Introduction

    25/48

    Computer Programming

    (CS-102)Week 1 Lecture 2

  • 8/18/2019 Computer Programming CS102_Introduction

    26/48

    Integer Variable Types

    • Char (1 byte)• Short ( 2 bytes)

    • Int (4 bytes)• Long (4 or 8 bytes)

  • 8/18/2019 Computer Programming CS102_Introduction

    27/48

  • 8/18/2019 Computer Programming CS102_Introduction

    28/48

    Floating Point Data Types

    • Float Precision 7 digits Range 1.2 x 10^-38 to 3.4 x10^38

    • Double Precision 15 digits Range 2.2x10^-308 to1.8x10^308

    • Long double Extended precision• Write a program that will compute area of a circle. The

    program should prompt for the radius of the circlefrom the user and then display the result

    • Create a program that converts inches to feet-and-inches for example, an input of 77 inches shouldproduce an output of 6 feet and 5 inches

  • 8/18/2019 Computer Programming CS102_Introduction

    29/48

    First Program Review

    // My First Program#include using namespace std;int main (){

    cout

  • 8/18/2019 Computer Programming CS102_Introduction

    30/48

    Introduction to C++

    The previous program is the typical programthat programmer apprentices write for thefirst time, and its result is the printing onscreen of the "Hello World!" sentence. It isone of the simplest programs that can bewritten in C++, but it already contains the

    fundamental components that every C++program has. We are going to look line by lineat the code we have just written:

  • 8/18/2019 Computer Programming CS102_Introduction

    31/48

    Commenting

    // my first program in C++This is a commentline. All lines beginning with two slash signs(//) are considered comments and do not haveany effect on the behavior of the program.The programmer can use them to includeshort explanations or observations within the

    source code itself. In this case, the line is abrief description of what our program is.

  • 8/18/2019 Computer Programming CS102_Introduction

    32/48

    Directives

    #include Lines beginning with a hashsign (#) are directives for the preprocessor. Theyare not regular code lines with expressions butindications for the compiler's preprocessor. In thiscase the directive #include tells thepreprocessor to include the iostream standardfile. This specific file (iostream) includes thedeclarations of the basic standard input-output

    library in C++, and it is included because itsfunctionality is going to be used later in theprogram.

  • 8/18/2019 Computer Programming CS102_Introduction

    33/48

    Namespace std

    using namespace std;All the elements of the standard C++ library aredeclared within what is called a namespace, the

    namespace with the name std . So in order toaccess its functionality we declare with thisexpression that we will be using these entities.This line is very frequent in C++ programs that

    use the standard library, and in fact it will beincluded in most of the source codes included inall of programs

  • 8/18/2019 Computer Programming CS102_Introduction

    34/48

    Main()

    • int main ()This line corresponds to the beginning of the definition of themain function. The main function is the point by where all C++ programsstart their execution, independently of its location within the source code.It does not matter whether there are other functions with other namesdefined before or after it - the instructions contained within this function'sdefinition will always be the first ones to be executed in any C++ program.

    For that same reason, it is essential that all C++ programs havea main function.

    The word main is followed in the code by a pair of parentheses (()). That isbecause it is a function declaration: In C++, what differentiates a functiondeclaration from other types of expressions are these parentheses thatfollow its name. Optionally, these parentheses may enclose a list ofparameters within them.

    Right after these parentheses we can find the body of the main functionenclosed in braces ({}). What is contained within these braces is what thefunction does when it is executed.

  • 8/18/2019 Computer Programming CS102_Introduction

    35/48

    Coutcout

  • 8/18/2019 Computer Programming CS102_Introduction

    36/48

    Return to main function

    return 0;The return statement causes the main function tofinish. return may be followed by a return code(in our example is followed by the return codewith a value of zero). A return code of 0 forthe main function is generally interpreted as the

    program worked as expected without any errorsduring its execution. This is the most usual way toend a C++ console program.

  • 8/18/2019 Computer Programming CS102_Introduction

    37/48

    Compilation and Linking

    • C++ Source Code• Compiler compiles it produce the object code

    •The linker links the object file to produce theexecutable program

  • 8/18/2019 Computer Programming CS102_Introduction

    38/48

    Data Types

    • What is declaration?• General Syntax of data type in C++

  • 8/18/2019 Computer Programming CS102_Introduction

    39/48

    Data Types

    • Purpose of variables?• Storage of Information• Different types of information that we may

    have to store

    • Integer Data, Floating Point Data, Character(s),true/false

    • Collection of all of the above• Different storage in computers

  • 8/18/2019 Computer Programming CS102_Introduction

    40/48

    Names of Variables

    • Keywords cannot be used as integer names• Variable names should• Start with letter(s)• They can contain digits except for the first

    letter

    • They can have underscore (only)• Good naming convention

  • 8/18/2019 Computer Programming CS102_Introduction

    41/48

    Character values

    1. #include 2. using namespace std;

    3. int main(int argc,char* argv[])4. {5. char a;6. a='14';7. //a=14; //uncomment and see what happens8. cout

  • 8/18/2019 Computer Programming CS102_Introduction

    42/48

    Operating with variables1. / operating with variables2. #include 3. using namespace std;4. int main ()5. {6. // declaring variables:7. int a, b;8.

    9. // process

    10. int result; :11. a = 5;12. b = 2;13. a = a + 1;14. result = a - b;

    15. // print out the result:16. cout

  • 8/18/2019 Computer Programming CS102_Introduction

    43/48

    Scope of variables

    All the variables that we intend to use in aprogram must have been declared with its typespecifier in an earlier point in the code, like wedid in the previous code at the beginning of thebody of the function main when we declaredthat a , b , and result were of type int .

    A variable can be either of global or local scope. A

    global variable is a variable declared in the mainbody of the source code, outside all functions,while a local variable is one declared within thebody of a function or a block.

  • 8/18/2019 Computer Programming CS102_Introduction

    44/48

    Scope of Variable

  • 8/18/2019 Computer Programming CS102_Introduction

    45/48

    Global and Local Variables

    Global variables can be referred from anywhere in thecode, even inside functions, whenever it is after itsdeclaration.

    The scope of local variables is limited to the blockenclosed in braces ({}) where they are declared. Forexample, if they are declared at the beginning of thebody of a function (like in function main ) their scope isbetween its declaration point and the end of that

    function. In the example above, this means that ifanother function existed in addition to main , the localvariables declared in main could not be accessed fromthe other function and vice versa.

  • 8/18/2019 Computer Programming CS102_Introduction

    46/48

    Keyword: const

    • You can define any kind of ‘variable’ as const,and the compiler will check that you do notattempt to alter the value of such a variable

  • 8/18/2019 Computer Programming CS102_Introduction

    47/48

    Classwork

    • Write a simple program that prompts the user toprovide an three inputs in yards, feets and inches andexpress the length in inches

    Formula to calculate length in inches:

    Inches_per_foot= 12

    Feet_per_yard =3

    inches+inches_per_foot*(feet+feet_per_yard*yards)

  • 8/18/2019 Computer Programming CS102_Introduction

    48/48

    The output should look like this