C++ in Short by Hassan

Embed Size (px)

Citation preview

  • 8/7/2019 C++ in Short by Hassan

    1/9

    COMPUTER PROGRAMING

    USING C++

    Lecture 1

    Programming: A program is a precise sequence of steps to solve a particular problem.Programming a computer simply means telling it what to do.

    Skills: 1. Analytical thinking 2.Critical thinking 3.Attention to details

    4. Creative synthesis

    Important points: * Computers are stupid * Comments liberally

    * Paying attention to details * be aware of Logical error

    A Question: What is the probability that she gets exactly three letter right i.e. three

    letters into their correct envelopes?

    Solution: No. of Permutation (arrangements) = 3! = 6

    Only one out of six arrangements is right. So probability =1/6

    Lecture 2

    Software:

    1.System Software: * System software controls the computers* It communicates with the computers hardware and controls different

    aspects of operations.

    1) Operating systems 2) Device drivers 3) Utilities

    OS: It manages other programs DD: It communicates between devices and computer.

    US: It performs specific tasks 1) Utility Compression 2) Disk defragment

    2.Application Software: A program or a group of programs designed for end users.e.g. GPS, Inventory control etc.

    Integrated Development Environment (IDE)

    Editor: In which we write the code

    Compilers and Interpreters: Translates source code to machine code. Compiler is

    efficient and stops on finding error. Interpreter reads line by line so it is slow and it

    executes before error.

    Debugger: To debug the program i.e. to correct logical errors.

  • 8/7/2019 C++ in Short by Hassan

    2/9

    Linker: It is a tool which checks our program and includes all those routines or

    functions which are used in our program while running.

    Loader: It loads program into memory and instructs the processor to start execution

    from first instruction.

    Lecture 3

    Write a program for printing Welcome

    #include Preprocessor directive Instruction to compiler to include contents of a

    system file.

    Library definition file for all input and output streams.

    Main() This is function which runs when the program is used.

    Not using main error voidmain(void)

    { } Curly brackets to group together pieces of program

    cout> Input stream to input data

  • 8/7/2019 C++ in Short by Hassan

    3/9

    Lecture 4

    Keywords of C main if else while do for

    Quadratic Equation Algebra y=ax2+bx+c In C y = a*x*x + b*x + c

    Imp Points *Integer division truncates fraction part*Liberal use of brackets *No expression on L.H.S of assignment operator

    Interesting ProblemInput: Four digit integer Output: All digits separate

    Assignment 1: Change the order of printing digits.

    Lecture 5

    DECISION MAKING if(condition)

    If Statement {

    If else statement Statement(s);

    If(condition) }{ statement(s); } Relational operators

    else { statement(s); } < >

  • 8/7/2019 C++ in Short by Hassan

    4/9

    Selection Statements if if else three types i. Nested if else

    ii. Dangling else problem iii. Conditional operator

    Conditional operator (? :) It is the C++ only ternary operator i.e. it takes three operands

    cout=60? Passed : Failed) ;

    Loop repetition structure e.g sum of first 100 integers starting from 1

    While Syntax while (logical expression) { statement(s); }

    Property *it executes zero or more times *It can become infinite loop

    Q: Calculate the sum of even numbers for a given upper limit of integers.

    Another way for even numbers 2*(number/2); remember integer division

    Write a program to find out factorial of a given non-negative integer

    Lecture 7

    Do while * While loop executes zero or more times *do while loop executes

    one or more times syntax do { statement(s); } while (condition);

    Example: Guessing game Relational operator while(trynum

  • 8/7/2019 C++ in Short by Hassan

    5/9

    SELF STUDY : POINTS EXPERIENCED

    #includeiostream.h

    char nm[15]; string variables or 15 characters

    const float p=3.14; To declare a constant value

    #define p 3.14 identifier constant The define directive

    The escape sequence \a alert \b back space \t \n

    \r carriage return = to move cursor to beginning of current line \\

    \ \ To print or on screen \f feed=to leave one blank page on printer.

    The setw manipulator

    #include cout

  • 8/7/2019 C++ in Short by Hassan

    6/9

    WRITE A PROGRAM IN C++ TO INITIALOZE A STRING AND THEN TO MOVE THE STRING

    TO LINE NO. 22 BY MOVING ONE CHARACTER AT A TIME. ALSO USE THE DELAY

    FUNCTION TO SLOW DOWN THE SPEED OF THE MOVING CHARACTER

    #inlcude

    #include#include

    main()

    {

    Char str[15]=Pakistan;

    Int I,l,c;

    clrscr();

    gotoxy(32,1);

    cout

  • 8/7/2019 C++ in Short by Hassan

    7/9

    & AMPERSAND

    #include

    #include

    Main()

    {clrscr();

    int a,b;

    int *x,*y;

    x=&a;

    y=&b;

    cout

  • 8/7/2019 C++ in Short by Hassan

    8/9

    yptr = &y;

    cout

  • 8/7/2019 C++ in Short by Hassan

    9/9

    ECHELON FOR CONVERSION

    for (l=0 ; l