Codes for Programming

download Codes for Programming

of 10

Transcript of Codes for Programming

  • 7/23/2019 Codes for Programming

    1/10

    C++ program to add two numbers.

    C++ programming code#include

    usingnamespacestd;

    intmain()

    {

    inta, b, c;

    couta >>b;

    c =a +b;

    cout

  • 7/23/2019 Codes for Programming

    2/10

    #include

    usingnamespacestd;

    /* Function arguments are of different data type */

    longadd(long, long);

    loatadd(loat, loat);

    intmain()

    {

    longa, b, %;

    loatc, d, &;

    couta >>b;

    % =add(a, b);

    cout

  • 7/23/2019 Codes for Programming

    3/10

    code of functions is same ecept data type, C++ provides a solution to this problem we can

    create a single function for different data types which reduces code si!e which is via templates.

    C++ programming code for function overloading

    #include

    usingnamespacestd;

    /* Number of arguments are different */

    oiddispla&(c$ar/0); print t$e string passed as argument

    oiddispla&(c$ar/0, c$ar/0);

    intmain()

    {

    c$arirst/0=" programming";

    c$arsecond/0="++ programming";

    displa&(irst);

    displa&(irst, second);

    return!;

    oiddispla&(c$ars/0)

    {

    cout

  • 7/23/2019 Codes for Programming

    4/10

    coutm >>n;

    cout

  • 7/23/2019 Codes for Programming

    5/10

    (oi#-(e)+(oi#si)+(oi#call)+intmain)

    //goto0)135+ cout$$67n7n7n7n7t7tAuthor: Arpit Agrawal7n7t7tEmail:[email protected]: Dice Roll Algorithm.7n7t7tProject ame: e!Roll.7n7t7t6+

    cout$$67n7n7t7t8oa#ing. . . . . . . 6+ leep)1+ cout$$67n7n7t7tPress r to roll or to uit the game 6$$en#l+ charch+ ch ; getch)+ m: i,)ch;;

  • 7/23/2019 Codes for Programming

    6/10

    si)+ &reaC+ #e,ault: cout$$6?6+

    =

    =

    (oi#one) cout $$ 6 !!!!!6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6 !!!!!6$$ en#l+ = (oi#two) cout $$ 6 !!!!!6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6 !!!!!6$$ en#l+ = (oi#three) cout $$ 6 !!!!!6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6 !!!!!6$$ en#l+ = (oi#,our) cout $$ 6 !!!!!6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6 !!!!!6$$ en#l+ = (oi#-(e) cout $$ 6 !!!!!6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+

    cout $$ 6F F6$$ en#l+ cout $$ 6 !!!!!6$$ en#l+ = (oi#si) cout $$ 6 !!!!!6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+ cout $$ 6F F6$$ en#l+

  • 7/23/2019 Codes for Programming

    7/10

    cout $$ 6 !!!!!6$$ en#l+ =

    c++beginnergamerandomdice

    shareimprove this question edited May 17 at 15:16

    RubberDuck

    21.5k236120

    asked ep 2! "13 at 13:#7

    $rpit $%ra&a'

    43115

    add acomme

    nt

    4 Answersactiveoldestvotes

    up

    vote13do&n voteaccepted

    (n'y ca'' sran#)once in an app'ication:

    sran# )time)?88+hou'd be )ust a*ter main)starts+

    ,his does not %enerate an even'y distributed the random numbers+

    n; ran#)+ n ; n B+

    ,his is because rand-. returns a number *rom /0R$DM$. or /032767. &hich is not

    e4act'y divisib'e by 6+ o you %et:

    : /54B2 oticethisis one more than the others. 2: /54B 1: /54B 4: /54B

    5: /54B B: /54B

    robab'y not an issue *or a simp'e app but &orth notin%+ ,he proper &ay to do this is:

    int#ieRoll) // !B e(enl0 #istri&ute#. staticintconstma ; RADGAH/B*B+

    intr ; ran#)+ while)r %; ma r ; ran#)+=

    returnrB+=

    robab'y best not to use %oto:

    m:i,)ch;;

  • 7/23/2019 Codes for Programming

    8/10

    ch ; getch)+gotom+

    re*er -a standard 'oop.:

    while)ch;;

  • 7/23/2019 Codes for Programming

    9/10

    this &as part o* main) makin% me think that you &ere missin% the *unction de*initions+;ou a'so haven"t indented the code insidethese 8number8 *unctions thou%h you have

    done it correct'y e'se&here+ ,ry to keep thin%s consistent+

    Overall

    (vera'' this 'ooks 'ike ? code and not at a'' ?@@+ t"s okay to keep it simp'e *or 'earnin%

    purposes but this cou'd use a 'ot o* &ork to make it 'ook 'ike an idea' %ame

    imp'ementation+

    * you &ant to take this a step *urther and reallymake it 'ook more 'ike ?@@ consider

    de*inin% your o&n c'asses such as a Iameand a Diec'ass+ ,he Iamec'ass &i'' de*ine

    the %ame ru'es &hi'e the Diec'ass &i'' represent one die and&i'' a''o& you to create

    dice -Dieob)ects.+ ,he Iamec'ass &i'' hand'e most o* the &ork and main)&i'' )ustneed to have a Iameob)ect that can ca'' a pu&licinter*ace *unction *or runnin% the

    %ame+ othin% e'se &ou'd need to be done in main)+

    shareimprove this ans&er edited $u% 13 "1# at 21:#7 ans&ered $u% 13 "1# at 1:0

    Aama'

    26.6k!!61!7

    add a comment

    up

    vote5do&n vote

    ;our main)is pretty messy and very hard to read+++ &on"t comment on the while'oop

    since someone e'se a'ready did -and a%ree &ith him. but sty'istica''y to make your

    code more readab'e you &ant to have proper indentation:intmain)

    //goto0)135+ cout$$67n7n7n7n7t7tAuthor: Arpit Agrawal7n7t7tEmail:[email protected]: Dice Roll Algorithm.7n7t7tProjectame: e!Roll.7n7t7t6+

    cout$$67n7n7t7t8oa#ing. . . . . . . 6+ leep)1+ cout$$67n7n7t7tPress r to roll or to uit the game 6$$en#l+ charch+ ch ; getch)+ m: i,)ch;;

  • 7/23/2019 Codes for Programming

    10/10

    is a mess+ ,he 3 'ines on'y %et e4ecuted i* ch;;ith a goto you have to ana'yCe

    the code to determine that you &ant 'oopin% behavior+ >hen someone %'ances at a while

    they automatica''y kno& it &i'' 'oop based on the condition inside the while+

    shareimprove this ans&er edited $u% 13 "1# at 20:12

    Aama'

    26.6k!!61!7

    ans&ered $u% 13 "1# at 20:0

    ressin%(n$'&ays

    34925

    add a comment

    up vote#do&n vote ,here"s no reason to use a case statement rep'ace it &ith:

    (oi#)* #rawJK) ; one3 two3 three3 ,our3 -(e3 si =+#rawJnK)+

    http://codereview.stackexchange.com/a/59943http://codereview.stackexchange.com/posts/59943/edithttp://codereview.stackexchange.com/posts/59943/revisionshttp://codereview.stackexchange.com/users/22222/jamalhttp://codereview.stackexchange.com/users/28411/pressingonalwayshttp://codereview.stackexchange.com/a/59943http://codereview.stackexchange.com/posts/59943/edithttp://codereview.stackexchange.com/posts/59943/revisionshttp://codereview.stackexchange.com/users/22222/jamalhttp://codereview.stackexchange.com/users/28411/pressingonalways