cprogramming (3)

Upload: margarette-manalo

Post on 03-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 cprogramming (3)

    1/38

    JimsC Programming Problems

    The First 101

    gcc ansi -WallA Varied Collection of Problems for Beginning Students

    Version 3.0

    Last Updated December !" 00#

  • 8/12/2019 cprogramming (3)

    2/38

    Programming Style Guide or !1"101 and !1"10#

    $ersion 1"0

    1" %ll iles &ithin 'rograms should contain the standard header" This is a comment based banner bo( &ith a

    'erimeter o asteris)s" *nside this bo( &ill be the 'rogrammers name and the title o the 'rogram" *t shoul

    also contain an entry that estimates that amount o time you s'ent to create it"

    /***************************************************************/

    /* Programmer: Herschel Burke Gilbert */

    /* */

    /* Program 1: Hello World */

    /* */

    /* Approximate completion time: 15 minutes */

    /***************************************************************/

    #" +our 'rogram should use unctions &hen reasonable to do so"

    ," +our unctions unless that is there sole 'ur'ose should not contain in'ut or out'ut statements"

    ." +our 'rogram should use our !1"101/!1"10# standard main tem'late as ollo&s

    int main( int argc, char *argv[] ) {

    return 0 ;

    }

    otice that the irst 2 is on the same line as the int main3

    4" +our indentation strategy must be reasonable and consistent" o 5agged edges

    6eclarators shall be indented #-. s'aces"

    7(ectuble statements shall be indented the same as declarators and they should all line u'"

    The body o a loo' shall be indented"The body o 8then9 and the body o the 8else9 shall be indented"

    :" +our comments 'rom't and out'ut should not contain any s'elling errors"

    ;" +our code should not a''ear cram'ed" Ty'ically o'erators &ill be surrounded by blan)s" +ou should use a

    single blan) line to set o logical chun)s o consecuti" 6o not 'ut a blan) bet&een the unction name and it arguments"

    !" +ou should not o

  • 8/12/2019 cprogramming (3)

    3/38

    1#" +ou should stri

  • 8/12/2019 cprogramming (3)

    4/38

    ssh (secure shell)

    So, you are at home or you are in the dorm room or at the library or Barnes and Noble. You have your laptop or

    computer with you and you are filled with passion to get some 91.101 work done. You need to get to a terminal

    window. If it were me, I would then type:

    % ssh [email protected]

    You would insert your own username in place of canning.

    Youll be prompted for your password. After typing your password in, you will be working on mercury. You will

    be home free.

    scp (secure copy)

    So, you are at home and you have created working program, say p1.c, on mercury, but now you wish to print th

    file out on your home printer. You will need to copy p1.c from mercury to your local machine. One way for you

    do this is to pull the file from mercury and have it placed on you machine.

    So, while you have a terminal window on your machine,

    %scp [email protected]:~canning/101/p1.c p1.c

    Of course, this is an example. You will need to use your username and the correct pathname of the file.

    You will be prompted for you password.

    Now, a copy of the file will be sitting on your local machine.

    Perhaps you have a C compiler and a UNIX environment with emacs on your local machine. You do your work

    that environment, but now you need to get your program onto mercury so that you can electronically submit it.

    %scp p1.c [email protected]:~canning/101/p1.c

    You will be prompted for your password.

    You should try these out as soon as possible so that you will have mastered them before you need them in a crisis.

    mailto:[email protected]:[email protected]:~canning/101/p1.cmailto:[email protected]:[email protected]:~canning/101/p1.c
  • 8/12/2019 cprogramming (3)

    5/38

    I hope you enjoy these problems. In the past, many students have enjoyed them, some have not.

    Which group will you be in? I am rooting for you to be in the first group. Once you start to conque

    them, they can all tumble over. I am trying to make you as strong as possible with the time we hav

    together. ou cannot learn to play the clarinet unless you practice. ou cannot learn to speak a

    foreign language unless you practice. ou cannot learn to solve problems in ! within a "#I$

    environment unless you practice. %lease dedicate a portion of each day to solve one or more of

    these problems. &ive yourself a chance to feel great. 'tart solving as many of these problems asyou can. (o not procrastinate. It is not hard once you get oriented. It does take time. ou can do

    this. It will be worth it.

    Problem $% &ello 'orld

    Write a C 'rogram that 'rints out the messageHello World"

    Problem % ()e Value *

    Write a C 'rogram that 'rints out the

  • 8/12/2019 cprogramming (3)

    6/38

    Problem % 1ne &ori2ontal Line of Asteriss

    Asing the emacs editor create a ile called testdata8that has a single integer

  • 8/12/2019 cprogramming (3)

    7/38

    Problem $!% Argc

    Write a C 'rogram that 'rints the number o command line arguments" This is the number that is stored in the

    argument argc" 6o not lose 'oints because your code has a ragged indent or a miss'elled &ord" For e(am'le

    bashN "/a"out &ould 'rint out 1bashN "/a"out 411 Kel Dtt =ie is good &ould 'rint out ;

    bashN "/a"out Jem and Scout &ould 'rint out .

    Problem $,% Using t)e s6rt +unction

    Write a C 'rogram that as)s the user to enter a loating 'oint number rom the )eyboard and then 'rints out thesMuare root o that number" +ou should call thes"rtunction" +ou can bro&se %''endi( 6 to &hich header ile yo

    should include" The te(tboo) is your best riend" @ead it" +ou should )no& that thes"rtunction deinition is not

    located in libc"a" *t is located in libm"a and thus you &ill need to use the lm o'tion &hen you try to build thee(ecutable image

  • 8/12/2019 cprogramming (3)

    8/38

    to use the ormat code &ithscanf" The unction%rintf on the other hand use or both double and loat

    ty'es"

    Problem 0% Area of a Circle

    *n grade school you learned that the area o a circle is 'i L r# " Write a C 'rogram that in'uts a single loating 'oinnumber re'resenting the

  • 8/12/2019 cprogramming (3)

    9/38

    Program ,% Sum of a Bunc)

    Create a ile and call it testdata)" The ile should contain an un)no&n number o integer

  • 8/12/2019 cprogramming (3)

    10/38

    Program 3$% Bubble Sort

    % ile contains 10 integers" Write a 'rogram to read these numbers and then sort them using a standard bubble sort

    +our 'rogram should out'ut the numbers sorting into ascending order" +ou should reali?e that the bubble sort is on

    Drder #com'arison based sorting strategy" The name o the ile is to be 'assed to your 'rogram as argH# L .I >

    and 4 # 1> > is ,,"

    +our C 'rogram &ill call a unction called inner that has the ollo&ing interace

    loat inner H loat uO loat

  • 8/12/2019 cprogramming (3)

    11/38

    Program 3!% Simulating Call B4 ;eference

    Write a 'rogram that contains both a main unction and a unction called s&a'" +our main 'rogram should read in

    t&o integer

  • 8/12/2019 cprogramming (3)

    12/38

    Problem 3% Blan ;emo8al

    % te(t ile contains a bunch o characters" There are no tab characters &ithin the ile" Write a 'rogram that re'laces

    t&o or more consecuti;ecursi8e 5uclid ?et)od@

    % ile contains an un)no&n number o 'airs o 'ositi

  • 8/12/2019 cprogramming (3)

    13/38

    Program !% 9on recursi8e +actorial Use a function

    Dne &ay to e('ress the deinition o the actorial o a number is

    X is ( H-1I ( H-#I ( H-,I ( H-.I ( 3 ( , ( # ( 1

    Kathematicians dene 0X to be 1"

    For e(am'le ,X is , ( # ( 1 &hich is eMual to :"

    Write a C 'rogram that com'utes and out'uts to standard out'ut the actorial o a number" +our unction main

    should read in either the

  • 8/12/2019 cprogramming (3)

    14/38

    Program !% ;elati8el4 Prime to 3,$

    Write a 'rogram that &ill com'ute and 'rint out all 'ositi

  • 8/12/2019 cprogramming (3)

    15/38

    Program ,$%

  • 8/12/2019 cprogramming (3)

    16/38

    Problem ,3% Close 5noug) for All Practical Purposes

    This %roblem was ta/en from the irst nnual 2ortheast 3egional High 4chool -rogramming 5ontest

    5ham%ionshi%. The contest was held at Western 2ew ngland 5ollege on 7a 1*, 186.

    )his program is not that difficult. *or those of you who are math phobic I believe you can handle this. +emember the

    tetbook is your best friend. It is filled with the answers you seek. ou are !olumbo, -llery ueen, /gatha !risti, and

    'herlock 0olmes all wrapped into one problem solving machine. !1mon, don1t tell me you never heard of !olumbo. ou ca

    do it. If you need to discuss this problem feel free to stop into my office. With help, you can become independent.

    The sMuare root o a number % can be com'uted by successi!!"

    ote +ou should chec) out the unctionfabsin %''endi( 6" %re you getting a&ay rom distractions Go to the

    library" ring your mathematics boo)" 6o all the 'roblems in the irst our cha'ters"

    Program ,!% +ibonacci Se6uence > 1, #1 ,. 44 3

    The irst term is 1"The second term is 1"

    7ach term thereater is the sum o the 're

  • 8/12/2019 cprogramming (3)

    17/38

    The Fibonacci seMuence is 1 1 # , 4 > 1, #1 ,. 44 3

    The irst term is 1"

    The second term is 1"

    7ach term thereater is the sum o the 're

  • 8/12/2019 cprogramming (3)

    18/38

    Thereore there are essentially 4 dierent triangle ty'es scalene isosocles eMuilateral scalene right and isoscele

    right" +our out'ut should be the most rele

  • 8/12/2019 cprogramming (3)

    19/38

    Program *$% 9umber Pu22le

    Write a C 'rogram that &ill read in t&o lines o data" The irst data line contains an integer and the second data

    line contains dierent 'ositi

  • 8/12/2019 cprogramming (3)

    20/38

    Program *3% Enig)t ?o8es

    This is a challenging %roblem for 1.101 students. It was ta/en from a %rogramming com%etition held a number of

    ears ago. 9ou should first understand the bac/trac/ing solution to the :night;s Tour.

    There are a''earing some !"0"

    For a gi!

    "0"

    ,#

    ,1"0,.

    4:"!"";>#"

    14

    - * - * -

    * - - - *

    - - K - -

    * - - - *

    - * - * -

  • 8/12/2019 cprogramming (3)

    21/38

    Program *!% S6uare Deal

    *n'ut or this 'rogram &ill be t&o 'ositi

  • 8/12/2019 cprogramming (3)

    22/38

    Program *,% Cube Cra-ling Ta/en from a -rogramming 5om%etition at W-I

    Eonor is ready" Eer ne& shi' Punisher is ca'able o interstellar light and she &ants to test its 'erormance" *n

    sim'le terms interstellar light is accom'lished by cra&ling around the aces o an imaginary ,-dimensional cube

    o edge length #=" ot that = is an integer 'arameter based on the 'o&er o a shi's engine" =ocations are

    re'resented as H( y ?I &here ( y and ? are all non-negati

  • 8/12/2019 cprogramming (3)

    23/38

    +ou can assume that no mo

  • 8/12/2019 cprogramming (3)

    24/38

    Program *%Word Reversal (medium difficulty)

    Write a 'rogram that &ill read in strings rom standard in'ut" 7ach string contains no more than :0 characters" The

    'rogram &ill out'ut each string &ith the &ords re

  • 8/12/2019 cprogramming (3)

    25/38

    Program $% Partition Problem

    Write a 'rogram that 'rom'ts the user or a number n" This number re'resents the number o 'ositi9 log 9 8ersion @

    *m'lement the di

  • 8/12/2019 cprogramming (3)

    26/38

    *n this 'roblem * am not as)ing you to create r 'ointers to integers" * am as)ing you to create s'ace to hold r L c

    integers all o &hich are consecuti

  • 8/12/2019 cprogramming (3)

    27/38

  • 8/12/2019 cprogramming (3)

    28/38

    %.

    #

    0

    -1

    The inal state is 7

  • 8/12/2019 cprogramming (3)

    29/38

    Problem #% +un 'it) Subtraction

    The sMuare 'attern sho&n belo& &as obtained by 'utting the numbers ,; #> 1 and #4 at the corners o a sMuare

    y 5oining the mid'oints o its sides a smaller sMuare &as dra&n inside the irst sMuare" 7ach corner o this ne&

    sMuare &as allocated a number by inding the dierence bet&een the t&o numbers at the ends o the line H eg" ,;-

    #>! U #>-1#; I" This ne& sMuare &as then ta)en as the starting 'oint and the 'rocess &as re'eated until thenumbers in the corners &ere the same in this case :" The sign o the dierences is to be ignored"

    +our 'rogram should read in the numbers at the corners o the initial outermost sMuare" The in'ut &ill come romthe standard in'ut" They &ill be 'ro

    ,;

    1

    1#

    #.!

    1#

    #;

    ,

    , 1>

    !

    14

    ! 14

    0

    :

    :

    0

    :

    :

    :

    :

  • 8/12/2019 cprogramming (3)

    30/38

    Problem 0% Building t)e +ourier ?atri:

    The construction o the n( nFourier Katri( &as discussed in class" The 'ur'ose o this assignment i or you to

    in'ut a number nrom the command line &ith nbeing a 'o&er o t&o and then ha

  • 8/12/2019 cprogramming (3)

    31/38

    +ou should create an array o structs"

    +ou should create

  • 8/12/2019 cprogramming (3)

    32/38

    Program !% ;e8erse t)e Last n Bits

    From time to time in the course o human e

  • 8/12/2019 cprogramming (3)

    33/38

    Program ,% Alligators and Ducs

    Su''ose there are n duc)s loating on the 'ond in a circle" The 'ond is also home or an alligator &ith a ondnessor duc)s" eginning at the 1st'osition the alligator counts around the circle and eats e

  • 8/12/2019 cprogramming (3)

    34/38

    +our 5ob is to 'ass in a hardco'y solution to %c)ermans Function or small 'ositi0!1!:,

    #0!:

  • 8/12/2019 cprogramming (3)

    35/38

    7('ected Dut'ut

    Konday

    Tuesday

    Thursday

    Wednesday

    ote +ou should ta)e into account lea' years" % lea' year is deined as any year such that it is di

  • 8/12/2019 cprogramming (3)

    36/38

    because it &ould not mo"!

    Then your 'rogram should out'ut

    The a

  • 8/12/2019 cprogramming (3)

    37/38

  • 8/12/2019 cprogramming (3)

    38/38

    +our 'rogram should irst 'rint out the &ords in sorted order" Dne 'er line"

    Then your 'rogram should 'rint out the &ords in their natural stored order"

    Program $00% =ra)am Scan for t)e Con8e: &ull

    Program $0$% Simple 5ncr4ption

    +our 'rogram is to o'en and read a te(tile character by character