progs of cs201

download progs of cs201

of 77

Transcript of progs of cs201

  • 8/10/2019 progs of cs201

    1/77

  • 8/10/2019 progs of cs201

    2/77

    #include

    main(){

    short ! y! ;

    '' *+;

    y &%;

    $%;

    ;

    cout

  • 8/10/2019 progs of cs201

    3/77

    this pro0ram store dou1le data type

    and

    si e of dou1le data type dou1le is 5 1yte (+ 1its 3indo3s operatin0 system)

    -

    #include

    main(){

    dou1le ! y! ;

    &'$ +*.*+ +;

    y &'6%5 .&' &;

    &' &' .&' &; y ;

    cout

  • 8/10/2019 progs of cs201

    4/77

    9peration:nput

    - this pro0ram take di0it inte0er form user and sho3 the di0it on the screensperatly i.e. if user enter &' &! it display &! !'!& speratly -

    #include

    main(){

    int user:nput! di0it;

    coutuser:nput;

    --$st one

    di0it user:nput $%;

    cout

  • 8/10/2019 progs of cs201

    5/77

    -- th

    user:nput user:nput-$%;

    di0it user:nput $%;

    cout

  • 8/10/2019 progs of cs201

    6/77

    }

    0essin08ame- /his is a 0ame! 3here user has to 0ess the hidden kep

    and total try should less then *;

    -

    #include

    main(){

    int tryCum ;

    char character;

    do{

    cout > character;

    if(character 7 7){ cout

  • 8/10/2019 progs of cs201

    7/77

    0radeEcheck

    -

    pro0ram 0ets a 0rade from user and provide results accordin0ly

    -

    #include

    main(){

    char 0rade;

    cout> 0rade;

    --start of s3itch

    s3itch(0rade) {

    case 7F7=

    case 7a7=

    cout

  • 8/10/2019 progs of cs201

    8/77

    cout 0rade;

    s3itch(0rade){

    case 7F7=

    case 7a7=

    cout

  • 8/10/2019 progs of cs201

    9/77

    cout

  • 8/10/2019 progs of cs201

    10/77

    cout> a0e$;

    cout> a0e&;

    cout> a0e';

    cout> a0e ;

    cout> a0e*; cout> a0e+;

    cout> a0eJ;

    cout> a0e5;

    cout> a0e6;

    cout> a0e$%;

    int totalF0es;

    2oat avara0eF0e;

    totalF0es a0e$ a0e& ;-- a0e' a0e a0e* a0e+ a0eJ a0e5 a0e6 a0e$%;

    avara0eF0e totalF0es-&;

  • 8/10/2019 progs of cs201

    11/77

    cout

  • 8/10/2019 progs of cs201

    12/77

    @ec %6?ircleFrea,unction

    dou1le circleFrea( dou1le radius ){

    --value of pi is '.$ $*6&+

    return ( '.$ $*6&+ radius radius);

    }

    #include

    main(){

    dou1le radius$! radius&! rin0Frea;

    cout > radius$;

    cout > radius&;

    rin0Frea circleFrea (radius$) circleFrea (radius&);

    cout

  • 8/10/2019 progs of cs201

    13/77

    int GvenAdd(int ); --function decleration! also kno3n as funtion prototype orsi0nature

    #include

    main(){

    int num1er;

    cout > num1er;

    if(GvenAdd(num1er)){

    cout

  • 8/10/2019 progs of cs201

    14/77

    }

    function?alcul o3

    dou1le return o3erCum(dou1le a! int 1){

    dou1le result $.%;

    int i;

    for( i $; i< 1; i ){

    result a;

    }

    return result;

    }

    #include

    main(){

    dou1le num1er;

    int po3er;

    cout > num1er;

    cout > po3er;

    --Co3! 3e 3ill pass num1er! and po3er varia1le to function return o3erCum

    cout

  • 8/10/2019 progs of cs201

    15/77

  • 8/10/2019 progs of cs201

    16/77

    main(){

    int input! result;

    cout > input;

    --callin0 sMuare function

    result make9Muare(input);

    cout

  • 8/10/2019 progs of cs201

    17/77

    @ec $%callHyLeOerence-

    /his pro0ram chan0e value of from function 1co it is callin0 1y reference

    "P" 3ith com1ination of varia1le name could used for this propes! like P

    and in function " " could used like! mean 3hatever pointin0 to

    -

    void sMuare( dou1le ) {

    ;

    -- cout

  • 8/10/2019 progs of cs201

    18/77

    @ec $$array#include

    main(){

    int cQ$%%R;

    int ! i %;

    do{

    cout

  • 8/10/2019 progs of cs201

    19/77

    for(i $; i ;

    int found %;

    for(i $; i

  • 8/10/2019 progs of cs201

    20/77

    random num1er 0enerator

    -

    #include

    #include

    main(){

    int a! i ;

    int Q$%%R;

    for(i %; i < $%; i ){

    QiR rand(); cout

  • 8/10/2019 progs of cs201

    21/77

  • 8/10/2019 progs of cs201

    22/77

    int i;

    for(i %; i < array9i e; i )

    numQiR i;

    }

    studentEarray

    #include

    void student,unc(intQR! intQR);

    main(){

    int studentQ&R! numQ&R! i;

    for( i %; i < '; i ){

    cout > studentQiR;

    cout > numQiR; }

    student,unc(student! num);

    }

    void student,unc(int oneQR! int t3oQR){

    int i ! total!;

    for(i %; i < '; i ){

    total oneQiR;

  • 8/10/2019 progs of cs201

    23/77

    --cout

  • 8/10/2019 progs of cs201

    24/77

  • 8/10/2019 progs of cs201

    25/77

    readNatri (a);

    --display matri

    displayNatri (a);

    cout

  • 8/10/2019 progs of cs201

    26/77

    void displayNatri (int arrQRQma ?olsR){

    int ro3! cols;

    for(ro3 %; ro3 < ma Lo3; ro3 ){

    for(cols %; cols

  • 8/10/2019 progs of cs201

    27/77

    void 0et:nput(dou1le salQRQ&R! int numGmps);

    void calCet9al(dou1le salQRQ&R! int numGmps);

    void KndTnluckies(dou1le salQRQ&R! int numGmps! int luckyQR);

    void printTnlucky(int lucky! int numGmps);

    void mark:fTnlucky(dou1le salQRQ&R! int numGmps! int luckyQR! int upperHound! intempC1r);

    main(){

    const int array9i e $%%;

    dou1le salQarray9i eRQ&R;

    int luckyQarray9i eR {%};

    int numGmps;

    -- Lead the actual num1er of employees in the company

    cout > numGmps;

    cout

  • 8/10/2019 progs of cs201

    28/77

    cout

  • 8/10/2019 progs of cs201

    29/77

    }else if(salQiRQ%R > $%%%% PP salQiRQ%R < &%%%%){

    --te t deductation is $%

    salQiRQ$R salQiRQ%R (.$% salQiRQ%R);

    }else if(salQiRQ%R > &%%%%){

    --te t deductation is $*

    salQiRQ$R salQiRQ%R (.$* salQiRQ%R);

    }else{

    --Co need to do any thin0

    }

    }

    }

    void KndTnluckies(dou1le salQRQ&R! int numGmps! int luckyQR){

    for(int i %; i < numGmps; i ){

    if(salQiRQ%R > % PP salQiRQ%R< *%%%){

    --Co need to do anythin0 in here

    ;

    }else if(salQiRQ%R > *%%$ PP salQiRQ%R < $%%%%){

    mark:fTnlucky(sal! numGmps! lucky! *%%$! i);

    }else if(salQiRQ%R > $%%%% PP salQiRQ%R < &%%%%){

  • 8/10/2019 progs of cs201

    30/77

    mark:fTnlucky(sal! numGmps! lucky! $%%%$! i);

    }else if(salQiRQ%R < &%%%$){

    mark:fTnlucky (sal! numGmps! lucky! &%%%$! i);

    }

    }

    }

    void mark:fTnlucky(dou1le salQRQ&R! int numGmps! int luckyQR! int upperHound!

    int empC1r){

    for(int i %; i < numGmps; i ){

    -

    9ee the if condition 1elo3! it 3ill mark the employee unlucky even if

    an employee in the hi0her ta 1racket is 0ettin0 the same amount of net

    salary as that of a person in lo3er ta 1racket.

    -

    if(salQiRQ%R < upperHound PP salQiRQ$R > salQempC1rRQ$R ){

    luckyQempC1rR $; --employee marked as unlucky

    1reak;

    }

    }

    }

  • 8/10/2019 progs of cs201

    31/77

    void printTnluckies(int salQRQ&R! int luckyQR! int numGmps){

    for(int i %; i < numGmps; i ){

    if(luckyQiR $){

    cout

  • 8/10/2019 progs of cs201

    32/77

    main(){

    int QR {$%! 6! 5! J! +! *! ! '! &! $};

    int i! U! tmp! s3aps;

    for(i %; i

  • 8/10/2019 progs of cs201

    33/77

  • 8/10/2019 progs of cs201

    34/77

    3hile( sptr S 7B%7){

    if(islo3er( sptr)){

    sptr toupper( sptr);

    cout

  • 8/10/2019 progs of cs201

    35/77

    cout y;

    ptr P ;

    yptr Py;

    if( ptr > yptr){

    cout

  • 8/10/2019 progs of cs201

    36/77

    -- "B%" is neccessary to 3rite in the end of strin0! else array continusaly print0ar1a0e on screen

    #include

    main(){

    char nameQ&%R;

    char name/3oQ$%R "Fmir"; --here array autometicaly assi0ne "B%" in the end

    nameQ%R 7a7;

    nameQ$R 717;

    nameQ&R 7c7;

    nameQ'R 7Bn7;

    for(int i %; i

  • 8/10/2019 progs of cs201

    37/77

    char strFQ5%R "F test for";

    char strHQ5%R;

    char ptrF; --a pointer to type character

    char ptrH; --a pointer to type character

    ptrF strF; --point ptrF at strin0 F

    ptrH strH; --point ptrH at strin0 H

    3hile( ptrF S 7B%7){

    ptrH ptrF ; --copin0 character 1y character

    }

    ptrH 7B%7;

    cout

  • 8/10/2019 progs of cs201

    38/77

    cout

  • 8/10/2019 progs of cs201

    39/77

  • 8/10/2019 progs of cs201

    40/77

  • 8/10/2019 progs of cs201

    41/77

    #include

    #include

    main(){

    char KleCameQR "output.t t";

    char t t/oWriteQR "Welcome to Iirtual Tniversity";

    ofstream output,ile;

    output,ile.open(KleCame! ios==out);

    if(Soutput,ile){

    cout

  • 8/10/2019 progs of cs201

    42/77

    KleWrite

    #include

    #include

    main(){

    char KleCameQR "output.t t";

    char t t/oWriteQR "Welcome to Iirtual TniversityBn";

    ofstream output,ile;

    output,ile.open(KleCame! ios==app);

    -

    Fppend rather than truncate an e istin0 Kle. Gach insertion

    (output) 3ill 1e 3ritten to the end of the Kle

    -

    if(Soutput,ile){

    cout

  • 8/10/2019 progs of cs201

    43/77

  • 8/10/2019 progs of cs201

    44/77

    system("pause");

    }

    >> ate mode

    Autput.t t

    Welcome to Iirtual Tniversity

    KleWrite

    #include

    #include

    main(){

    char KleCameQR "output.t t";

    char t t/oWriteQR "Welcome to Iirtual Tniversity";

    ofstream output,ile;

    output,ile.open(KleCame! ios==ate);

    -

    ate rather than truncate an e istin0 Kle. Gach insertion

    (output) 3ill 1e 3ritten to the end of the Kle

    -

    if(Soutput,ile){

    cout

  • 8/10/2019 progs of cs201

    45/77

    }

    output,ile > 0et func

    Autput.t t

    Welcome to Iirtual Tniversity

    KleWrite

    #include

    #include

    main(){

    char KleCameQR "output.t t";

    char t t/oWriteQR "Welcome to Iirtual Tniversity";

    ofstream output,ile;

    output,ile.open(KleCame! ios==ate);

    -

    ate rather than truncate an e istin0 Kle. Gach insertion

    (output) 3ill 1e 3ritten to the end of the Kle

    -

    if(Soutput,ile){

  • 8/10/2019 progs of cs201

    46/77

  • 8/10/2019 progs of cs201

    47/77

    #include

    #include

    main(){

    ifstream in,ile; --Xandle for the input Kle

    char input,ileCameQR "test.t t";

    const int NF E?XFLE/AELGF4 $'; --ma imum character to read in onr linrdirecotry

    char complete@ine/e tQNF E?XFLE/AELGF4R; -- to 1e used in 0et@ine function

    in,ile.open(input,ileCame); --openin0 the Kle

    --checkin0 that Kle is successfuly opened or not

    if(Sin,ile){

    cout

  • 8/10/2019 progs of cs201

    48/77

    >> strtok

    9alin.t t

    Famir $&%%%

    Fmara $*%%%

    Fdnan $'%%%

    Ff al $$*%%

    9alout.t t

    /he total salary *$*%%

    strtoken

    -

    /his pro0ram reads name and salary from a t t Kle

    ?alculate the salaries and 3rite the total in an output Kle

    -

    #include

    #include

    #include

    #include

    main(){

    ifstream in,ile; --Xandle for the input Kle

    char input,ileCameQR "salin.t t"; --Kle name! this Kle is in the currentdirectory

    ofstream out,ile; --handle for the output Kle

    char output,ileCameQR "salout.t t"; --Kle name! this Kle is in the currentdirecotry

  • 8/10/2019 progs of cs201

    49/77

    const int NF E?XFLE/AELGF4 $%%; --ma imum character to read in oneline

    char complete@ine/e tQNF E?XFLE/AELGF4R; --used in 0et@ine function

    char token tr;

    int salary! total9alary;

    salary %;

    total9alary %;

    in,ile.open(input,ileCame); --Apenin0 the input Kle

    out,ile.open(output,ileCame); --Apenin0 the output Kle

    --?heckin0 that Kle is successfully opened or not

    if(Sin,ile){

    cout

  • 8/10/2019 progs of cs201

    50/77

    in,ile.0etline(complete@ine/e t! NF E?XFLE/AELGF4);

    token tr strtok(complete@ine/e t! " "); --Krst token is name

    token tr strtok(CT@@! " ");

    salary atoi(token tr);

    total9alary salary;

    }

    --3ritin0 the total into the output Kle

    out,ile

  • 8/10/2019 progs of cs201

    51/77

    #include

    #include

    main(){

    ofstream out("test.t t");

    if(Sout){

    cout

  • 8/10/2019 progs of cs201

    52/77

    int i;

    2oat f;

    ifstream in("test.t t");

    if(Sin){

    cout > f;

    in >> ch;

    -

    Cote that 3hite space are 1ein0 i0nored! you can turn this of usin0unsetf(ios==skip3s);

    -

    cout

  • 8/10/2019 progs of cs201

    53/77

    B& Kle copy?opied.t t

    Xello 3orld

    ,ile.t t

    Xello 3orld

    copy,ile

    - ?ode snippet to copy the Kle Ythis,ileZ to the Kle Ythat,ileZ -

    #include

    #include

    main(){

    ifstream from,ile("Kle.t t");

    if(Sfrom,ile){

    cout

  • 8/10/2019 progs of cs201

    54/77

    cout

  • 8/10/2019 progs of cs201

    55/77

    -

    /his is a sample pro0ram to determine the len0th of a Kle. /he pro0ram

    accepts the name of the Kle as a command line ar0ument.

    -

    #include

    #include

    #include

    ifstream in,ile;

    ofstream out,ile;

    main(int ar0c! char ar0v){ in,ile.open("test.t t");

    if(Sin,ile){

    cout

  • 8/10/2019 progs of cs201

    56/77

    -

    out,ile.open("test.t t"! ios==app);

    if(Sout,ile){

    cout

  • 8/10/2019 progs of cs201

    57/77

    -

    #include

    #include

    main(){

    lon0 pos;

    ofstream outKle;

    outKle.open("test.t t");

    -- outKle.3rite("hello 3orld Bn /his is a sample. hello 3orld"! );

    outKle

  • 8/10/2019 progs of cs201

    58/77

    -

    Xere! a loop 3ill 1e used to process the 3hole Kle. We 3ill see that it is

    much faster due to 1ein0 capa1le of reducin0 the num1er of calls to readin0

    and 3ritin0 functions. :nstead of $%%%% 0etc () calls! 3e are makin0 only

    one read () function call. /he performance is also made in physical reduced

    disk access (read and 3rite). :mportant part of the pro0ram code is 0iven

    1elo3=

    -

    #include

    #includemain(){

    int i %;

    int U %;

    char strQ$%%%%R;

    ifstream K; --open the input Kle

    ofstream fo; --open the output Kle

    K.open("test.t t"! ios==in); --open the input Kle

    fo.open("out,ile.t t"! ios==out); --open the output Kle

    K.seek0(%! ios==end); --0o the end of input Kle

    K.tell0(); --0et the position;

    K.seek0(%! ios==1e0); --0o to the start of input Kle

    for(i %; i

  • 8/10/2019 progs of cs201

    59/77

  • 8/10/2019 progs of cs201

    60/77

  • 8/10/2019 progs of cs201

    61/77

    $

    &

    '

    *

    +

    J

    5

    6

    *%*$

    *&

    *'

    *

    **

    *+

    *J

    *5

    *6

    +%

    +$

    +&

    +'

    +

    +*

    ++

  • 8/10/2019 progs of cs201

    62/77

    +J

    +5

    +6

    J%

    J$

    J&

    J'

    J

    J*

    J+JJ

    J5

    J6

    5%

    5$

    5&

    5'

    5

    5*

    5+

    5J

    55

    56

    6%

    6$

    6&

  • 8/10/2019 progs of cs201

    63/77

    6'

    6

    6*

    6+

    6J

    65

    66

    si eof

    #include

    #include

    main(){

    int i;

    char c 7a7;

    lon0 l;

    dou1le d;

    2oat f;

    --si eof() function tell the si e fo diOernt type of varia1les

    cout

  • 8/10/2019 progs of cs201

    64/77

    --fo.3rite("Bn"! i); --to 3rite 1inary

    fo

  • 8/10/2019 progs of cs201

    65/77

    seek0 0et

    -

    /his is a sample pro0ram to demonstrate the use of open()! close()! seek0()! 0et()

    functions and streams. :t e pects a Kle named my ,ile.t t in the current directory

    havin0 some data strin0s inside it.

    -

    #include

    #include

    #include

    -- 4ecalrein0 the stream o1Uects

    ifstream in,ile;

    ofstream scrn! prnt;

    main(){

    char in?har; in,ile.open("my ,ile.t t"! ios==in); --open the Kle for input

    if(Sin,ile){

    cout

  • 8/10/2019 progs of cs201

    66/77

    scrn.close(); --close the output stream

    in,ile.seek0(%! ios==1e0); --8o to the 1e0innin0 of the Kle

    prnt.open("@ /$"! ios==out); --Fttach the output stream 3ith the @ /$ port

    3hile(in,ile.0et(in?har)){ --read the 3hole Kle one character at a time

    prnt

  • 8/10/2019 progs of cs201

    67/77

  • 8/10/2019 progs of cs201

    68/77

    B$ strcture

    9truct

    -

    9imple pro0ram sho3in0 the initiali ation of structure.;

    -

    #include

    --4eclarin0 student structure

    struct student{

    char nameQ+ R;

    char courseQ$&5R;

    int a0e;

    int year;

    };

    main(){

    --initiali in0 the structure

    student s$ {"Fli"! "?s&%$ :ntorduction to pro0rammin0"! &&! &%%&};

    cout

  • 8/10/2019 progs of cs201

    69/77

    }

    B& arro3 access

    arro3 access

    -

    /his pro0ram sho3s the access of structure data mem1ers 3ith pointer to structure

    -

    #include

    struct student{

    char nameQ'%R;

    char courseQ+%R;

    int a0e;

    int year;

    };

    main(){

    student s$ {"9yed 9huUaat Fli"! "?s&%$"! &%! &%%&};

    student s tr;

    s tr Ps$;

    cout

  • 8/10/2019 progs of cs201

    70/77

    cout

  • 8/10/2019 progs of cs201

    71/77

    char courseQ$&5R;

    int a0e;

    int year;

    };

    student s$ {"Fli"! "?s&%$"! & ! $66%};

    --usin0 si eof() operator detemine the si e

    cout

  • 8/10/2019 progs of cs201

    72/77

    main(){

    --declarin0 student structure

    struct student{

    char nameQ'%R;

    char courseQ$*R;

    int a0e;

    2oat 8 F;

    };

    int noAf9tudents '; --total no of students student studentsQnoAf9tudentsR; --array of student structure

    int totalF0e! inde ! avera0eF0e;

    2oat total8 F! ma 8 F! avera0e8 F;

    --initiali in0 the structure! 0ettin0 the input from user

    for(int i %; i < noAf9tudents; i ){

    cout

  • 8/10/2019 progs of cs201

    73/77

    cout > studentsQiR.8 F;

    }

    ma 8 F %;

    --calculatin0 the total a0e! total 8 F and ma 8 F

    for(int U %; U < noAf9tudents; U ){

    totalF0e totalF0e studentsQUR.a0e; total8 F total8 F studentsQUR.8 F;

    --determinin0 the ma 8 F and storin0 its inde

    if(studentsQUR.8 F>ma 8 F){

    ma 8 F studentsQUR.8 F;

    inde U;

    }

    }

    --calculatin0 the avera0e a0e

    avera0eF0e totalF0e-noAf9tudents;

    cout

  • 8/10/2019 progs of cs201

    74/77

    --4eterminin0 the 0rade of the class

    if(avera0e8 F > ){

    cout

  • 8/10/2019 progs of cs201

    75/77

    main(){

    int num1er;

    cout > num1er;

    if(num1er P % 5) --5 is 3ritten in de adecimal form

    cout

  • 8/10/2019 progs of cs201

    76/77

    cin >> pass3ord;

    passptr pass3ord;

    --no3 enctryptin0 the pass3ord 1y usin0 ` 3ith '

    3hile( passptr S 7B%7){

    passptr ( passptr ` ');

    passptr;

    }

    cout

  • 8/10/2019 progs of cs201

    77/77

    int num1er! result;

    cout > num1er;

    result num1er