You are on page 1of 38

Jims

C Programming Problems
The First 101
gcc ansi -Wall
A Varied Collection of Problems for Beginning Students
Version 3.0
Last Updated December !" 00#
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 should
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&s0
int main( int argc, char *argv[] ) {
return 0 ;
}
1otice that the irst 2 is on the same line as the int main 3
4" +our indentation strategy must be reasonable and consistent" 1o 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<e statements" =et your code breathe"
>" 6o not 'ut a blan) bet&een the unction name and it arguments"
!" +ou should not o<erca'itali?e- 'articularly your 'rom'ts to the user"
10" +our sentences should end &ith a 'ro'er 'unctuation mar)"
11" 7rrors messages ty'ically should not be re'orted in the unction that ound them" @ather- a status code
should be returned bac) to the caller"

1#" +ou should stri<e to &rite clear code that is not going out o its &ay to conuse the reader" *n general- direct
and 'lain &ill al&ays be 'reerred o<er indirect and cle<er"
1," * you o'en a ile- you should close it" This is normally done 5ust 'rior to returning bac) to A1*B"
1." 6o not intermingle your <ariable deinitions &ith e(ecutable statements" Catch u' your <ariable deinitions
at the to' o the body o the unction" Dnce you ha<e &ritten an e(ecutable statement you should not be
&riting additional <ariable deinitions" This can be <iolated in the case o a com'ound statement"
14" Eeader iles do not reser<e s'ace" They do not e<er contain unction deinitions" They do not e<er contain
<ariable deinitions" Ty'e deinitions do not reser<e s'ace and they are oten 'laced &ithin a header ile"
1:" 6o not &rite useless include statements" For e(am'le- do not use #include <stdlib.h> i the code
does not use anything declared &ithin stdlib.h"
1;" +our 'rogram should be com'iled &ith both the ansi and the Wall o'tions" 6oing so should not generate
any error or any &arning messages"
1>" Ase meaningul <ariable names" $ariables that are used as loo' indices are oten i- 5- and )" +ou should
continue &ith this con<ention unless there is a good reason to de<iate"
1!" +ou should see) to build and call unctions that are useul and unctional"
#0" +ou should not create a cascading unction that is only there to reduce the si?e o a body &ithout regard to
its 'ur'ose" Functions should ha<e a single 'ur'ose" The 'ur'ose should not be 8code which comes after
other code I just wrote"9
#1" Fee' your non-local <ariables to a minimum" Perha's they hel' &ith an abstract data ty'eG Perha's you
really need them or some s'ecial 'ur'oseG Cut i you use them- 'lease ha<e a good reason" Con<enience is
not oten a good reason"
##" Ase the ad5ecti<es static and e(tern a''ro'riately &hen building 'rograms that s'an multi'le iles" Some say
the use 8static9 unctions ham'er debugging in large systems"
#," * you need to ha<e global ty'es- global enumerated ty'es- and/or truly global <ariables- then batch them
into a ile called globals"h and/or globals"c Hor deinitionsI"
#." Ceore you 'ass in your code ta)e one last long loo) at it" *s is cleanG 6oes it a''ear sna''yG 6oes it ha<e
any ragged edges due to random indentationsG 6oes it contain any s'elling errorsG 6o your 'rom'ts ma)e
senseG *s your out'ut labeledG %re you being courteous to your readerG %re you being courteous to your
userG %re you being courteous to your graderG 6oes your code loo) rushedG *s it done &ith a 'rideG
#4" @e-read item #. abo<e" * thin) you should create a chec)list to remind yoursel o item #."
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 canning@mercury.cs.uml.edu <ret>
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 this
file out on your home printer. You will need to copy p1.c from mercury to your local machine. One way for you to
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 canning@mercury.cs.uml.edu:~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 in
that environment, but now you need to get your program onto mercury so that you can electronically submit it.
%scp p1.c canning@mercury.cs.uml.edu:~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.
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 conquer
them, they can all tumble over. I am trying to make you as strong as possible with the time we have
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 as
you 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 message Hello World"
Problem % ()e Value *
Write a C 'rogram that 'rints out the <alue :" * &ant you to use the Jd ormat code"
Problem 3% ()e C)aracter P
Write a C 'rogram that 'rints out the single character P" * &ant you to use the Jc ormat code"
Problem !% ()e scanf +unction
Write a C 'rogram that reads in an integer <alue rom the )eyboard <ia the scanf unction and then 'rints it bac)
onto the screen" +ou should )no& that scanf see)s an address e('ression" For e(am'le- &n is an address e('ression"
+ou should also )no& that scanf returns a <alue" *t is either the number o successul con<ersions or an error code
that is ty'ically 7DF" Please read about the scanf unction in %''endi( 6 o your te(tboo)"
Problem ,% Sum of (-o Values
Write a C 'rogram that reads t&o integer <alues rom the )eyboard <ia the scanf unction- then adds them together-
stores the result into a <ariable called sum- and then 'rints out the <alue o the <ariable sum"
Problem *% ()e fscanf +unction
Asing the emacs editor- create ile called testdata6 that has a single integer <alue stored into it" Write a C 'rogram
that o'ens a ile named testdata6, reads the single <alue stored in it using the fscanf unction- and then 'rints the
<alue bac) onto the screen" +ou should call the unction fclose beore you return to the Ani( o'erating system" @ead
about the unction fclose in %''endi( 6 o your te(tboo)" Kel Dtt hit 411 home runs"
Problem .% Bigger t)an $00/
Write a C 'rogram that reads a number rom the )eyboard <ia the scanf unction- and then 'rints the message The
number is bigger than 100 i it is" * the number is 100 or less than 100 then you should 'rint out the message0 The
number is not bigger than 100. Ka)e certain that your code loo)s good" 1o ragged indents"
Problem 0% 1ne &ori2ontal Line of Asteris3s
Asing the emacs editor- create a ile called testdata8 that has a single integer <alue stored into it" The integer &ill be
a number bet&een 1 and ,0 inclusi<e" Create a C 'rogram that uses the fscanf unction to read in this integer <alue
and then 'rints a hori?ontal line containing that many asteris)s HLI" Ase a or-loo' in your 'rogram" For e(am'le-
* the ile contained the integer > your 'rogram &ould 'rint out0 LLLLLLLL
* the ile contained the integer 14 your 'rogram &ould 'rint out0 LLLLLLLLLLLLLLL
Problem #% Using a +or Loop
Asing the emacs editor create a ile called testdata that contains 4 'ositi<e integer <alues" Write a C 'rogram that
reads and 'rints all i<e <alues" 7ach number should be read using the fscanf unction embedded inside a or-loo'"
+ou should not use an array" +ou should a or-loo'"
Problem $0% Sum of (-ent4
Write a C 'rogram that reads in #0 'ositi<e integer <alues rom a ile called testdata10 and com'utes their sum- and
then 'rints the <alue o the sum onto the screen" +our 'rogram should not use #0 distinct <ariables" +our 'rogram
should not use an array"
Problem $$% 56ual to 7ero/
Write a C 'rogram that reads a single integer number rom the )eyboard <ia the scanf unction- determines i the
number is eMual to ?ero or not- and then either 'rints out0 !The number is e"ual to #ero.$ or !The number is not
e"ual to #ero.$
Problem $% Positi8e" 9egati8e" or 7ero/
Write a C 'rogram that reads a single integer number rom the )eyboard <ia the scanf unction- determines i the
number is either- 'ositi<e- ?ero- or negati<e and then 'rints out a message such as0 8The number is %ositi&e"9 or
8The number is #ero"9 or 8The number is negati&e"9
Problem $3% ()e abs +unction
Write a C 'rogram that reads a single integer <alue rom the )eyboard" The number could be either a negati<e
number- a 'ositi<e number- or ?ero" +our 'rogram should call the absolute <alue unction abs and it then 'rints
out the absolute <alue o number entered" +ou can bro&se a''endi( 6 to determine &hich header ile you should
include" +our te(tboo) is your best riend" * do not &ant you to create your o&n absolute <alue unction" Ka)e sure
that your code loo)s sna''y" What &as the trouble in @i<er CityG
For e(am'le-
bashN "/a"out -: &ould cause : to be 'rinted"
bashN "/a"out . &ould cause . to be 'rinted"
Problem $!% Argc
Write a C 'rogram that 'rints the number o command line arguments" This is the number that is stored in the
argument0 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 1
bashN "/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 the
sMuare root o that number" +ou should call the s"rt unction" +ou can bro&se %''endi( 6 to &hich header ile you
should include" The te(tboo) is your best riend" @ead it" +ou should )no& that the s"rt unction 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 the
e(ecutable image <ia the gcc utility" +ou also need to include math"h"
Problem $*% Sine +unction
@ead an integer or loating 'oint <alue rom the command line" *t &ill come into your 'rogram as arg<O1P"
@emember that arg<O1Ps ty'e is a 'ointer to a character that begins a seMuence o characters that end in a null byte"
*n other &ords- it is &hat is agreed u'on as a string"
For e(am'le-
bashN "/a"out .",
Print out the trigonometric sine <alue o this number" +ou &ould do this by calling the 'ro'er unction that is ound
in %''endi( 6 o you te(tboo)"
Program $.% Count C)aracters
Write a 'rogram that counts the characters entered into the 'rogram rom the )eyboard until 7DF is reached" Ase
the getchar unction" +our 'rogram should out'ut the number o characters entered"
Program $0% Solid Bo: of Asteris3s
+our tas) is to &rite a C 'rogram that acce'ts t&o 'ositi<e integer <alues rom the )eyboard- say = and E" Coth =
and E &ill be less than #1" +our 'rogram then 'rints out a solid o bo( o asteris)s- &ith = hori?ontal stars and E
<ertical stars" The bo( is illed &ith asteris)s" For e(am'le-
* you &ere to ty'e in 4 and ,- you out'ut &ould be0
LLLLL
LLLLL
LLLLL
Problem $#% Area of a ;ectangle
*n grade school you learned that the area o a rectangle is its length times its height" Write a C 'rogram that in'uts
t&o loating 'oint numbers that re'resents the length and the height o a rectangle" +our 'rogram should out'ut the
area o the rectangle" * you use the ty'e double to declare your <ariables- you &ill need to use the Jl ormat code
&ith scanf" See the bac) co<er 'ages o your boo)" * you use the ty'e loat to declare your <ariables- you &ill need
to use the J ormat code &ith scanf" The unction %rintf - on the other hand- use J 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 'oint
number re'resenting the <alue o the radius r" +our 'rogram should out'ut the area o the circle &ith a radius r"
1o&- the only 'roblem is &hat <alue should you use or 'iG Emmm" Well- inside o math"h is a the ollo&ing line0
Qdeine KRP* ,"1.14!#:4,4>!;!,#,>.: /L 'i L/
so you need to include math"h and then use KRP* in an e('ression to calculate the area" * do not &ant you to ty'e in
this Qdeine" * &ant you to include math"h" Eo&e<er- &ith the ansi o'tion you &ill need to add another o'tion-
other&ise things &ont go too &ell"
Try to build this 'rogram using0 gcc ansi Wall '1;"c"
+ou should see that trouble e(ists" *t &ill not recogni?e KRP* e<en though it is inside math"h" To get 'ast this- you
need to build the 'rogram by either dro''ing the ansi o'tion or by adding the 6RG1ARSDA@C7 o'tion" * 'reer
you choose the latter0
gcc ansi Wall 6RG1ARSDA@C7 '1;"c
+ou do not need to build the 'rogram &ith the lm o'tion since you are not missing any deinitions rom libm"a"
+ou need to understand the 're<ious sentence" Ka)e certain that you do understand it"
Program $% Arg8
Write a C 'rogram that &ill 'rint out each command line argument on a se'arate line" Ase a or-loo'" The loo'
should ha<e an inde( <ariable i that ranges rom 0 to Hargc'1I" @emember the irst command line argument is
called arg<O0P and its ty'e is string so you can 'rint it out &ith a Js ormat code" +ou should also recall that the
character combination (n means ne&line"
Program % ;e8erse t)e Command Line
Write a C 'rogram that &ill 'rint out each command line argument on a se'arate line" Eo&e<er- you should 'rint
them out in re<erse order" That is- the last command line argument is 'rinted irst" 6o not ha<e an o-by-one error"
Ka)e sure that your code loo)s clean" @e<ie& the 101 Style Guide"
Program 3% Scanf ;eturns ')at/
Create a ile and call it testdata)*" The ile should contain an un)no&n number o integer <alues" Write a C
'rogram that uses a &hile loo' to read in each number and then 'rints the number bac) onto the screen" The
boolean e('ression o the &hile loo' must contain a call to the fscanf unction" +ou need to read your boo)" =oo) at
'age :," Study the code there" Ka)e certain you understand &hy it is this &ay"
Program !% 1ne Dimensional Arra4
Create a ile and call it testdata)+" The ile should contain e(actly 14 integer <alues" @ead these 14 numbers into a
one-dimensional array" Then 'rint the numbers out in the re<erse order rom &hich they &ere entered"
Ase an array" * do not &ant you to create 14 distinct <ariables" @ead your boo)" Cecome inde'endent"
Program ,% Sum of a Bunc)
Create a ile and call it testdata)," The ile should contain an un)no&n number o integer <alues" Write a 'rogram
that &ill read in these numbers until end o ile is reached and then out'uts the sum o the numbers" +ou should not
use an array to do this 'roblem"
Program *% fgetc and toupper
Write a 'rogram that reads in a te(t ile character by character using fgetc" The name o the te(t ile is testdata)6"
+our 'rogram should 'rint out an e(act co'y o the ile it read in e(ce't that all lo&ercase letters are con<erted to
u''ercase letters" +ou may use the unction tou%%er Hsee a''endi( 6I" +ou should also use the unction %utchar"
Program .% ;e8erse
Write a 'rogram that reads in 10 integer numbers rom the )eyboard using scan" +our 'rogram should then 'rint
out the numbers in re<erse order" +our 'rogram should store the 10 numbers into a one-dimensional array"
@emember- scan &ants you to 'ro<ide it an address e('ression" 6o you see that this 'roblem reMuires you to
8hold9 all the numbers at once beore you can generate the ans&erG This &as not the case &hen &e com'uted the
a<erage o some numbers" To com'ute the a<erage &e did not need to 8hold9 the numbers so an array is not needed
or &anted" Cut- here it is"
Program 0% Digit Sum
% ile contains a bunch o 'ositi<e integers" Write a 'rogram that reads in the numbers- one by one- and then out'uts
the sum o the indi<idual digits" For e(am'le- i the number :114 is read in- then your 'rogram &ould out'ut 1,"
+our main 'rogram must call a unction that recei<es an integer argument and returns the sum" This unction
should not 'rint out the ans&er- it should only com'ute and return the ans&er" The main unction &ill 'rint out the
ans&er" The name o the ile is gi<en on the command line as arg<O1P" +ou should )no& that arg<O1P is already a
string e('ression" *t does not need to be enclosed in double Muotes &hen you use o'en"
@emember- close the ile beore you e(it the 'rogram"
Program #% +ind t)e A8erage
Get into the emacs editor and create a ile named testdata#: that contains . integer <alues" Write a 'rogram that &ill
com'ute the a<erage HmeanI o the our numbers" @emember that the <alue o the a<erage in not necessarily an
integer <alue" %lso remember that truncation &ill occur i you di<ide an integer by an integer"
For e(am'le- i the numbers in the ile &ere0 1 # , . then the ans&er should be #"4 Hnot #I"
Program 30% Unfilled Bo:
+our tas) is to read a 'air o 'ositi<e integers- = and E- rom standard in'ut" Coth = and E &ill be less than #1"
+our 'rogram then 'rints out an em'ty bo( o asteris)s &ith = hori?ontal stars and E <ertical stars" The bo( is
em'ty" For e(am'le- i the in'ut data is : and . then the out'ut should be0

LLLLLL
L L
L L
LLLLLL
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 1
#
com'arison based sorting strategy" The name o the ile is to be 'assed to your 'rogram as arg<O1P" We
discussed bubble sort in class"
Program 3% <nner Product of (-o Vectors
Write a 'rogram that &ill in'ut t&o <ectors o inormation" 7ach <ector &ill contain e(actly > loating 'oint <alues"
+our 'rogram &ill enter these <alues in rom the standard in'ut" Dnce the <alues ha<e been read in- your 'rogram
should call a unction that &ill com'ute the inner 'roduct o these t&o <ectors" The unction should return the <alue
bac) to the main 'rogram" The main 'rogram &ill then 'rint out the <alue" +ou should not 'rint the <alue out inside
the inner 'roduct unction"
The inner 'roduct o t&o <ectors is a single number obtained by multi'lying corres'onding elements and then
adding u' their sums" For e(am'le- i <ector u S H4- 1- :- #I and <ector < S H1- #- ,- .I then the inner 'roduct is ,,
because
H4 L 1I S 4
H1 L #I S #
H: L ,I S 1>
H# L .I S >
and 4 T # T 1> T > is ,,"
+our C 'rogram &ill call a unction called inner that has the ollo&ing interace0
loat inner H loat uOP - loat <OP- int si?e I U
Program 33% Persistence of a 9umber
Kulti'lying the digits o an integer and continuing the 'rocess gi<es the sur'rising result that the seMuence o
'roducts al&ays arri<es at a single digit number" For e(am'le-
;14 ---- ,4 ---- 14 ---- 4
#; ---- 1. ---- .
.000 ---- 0
!
The number o times 'roducts need to be calculated to reach a single digit is called the 'ersistence number o that
integer" Thus- the 'ersistence number o ;14 is ,- the 'ersistence number o #; is #- the 'ersistence number o .000
is 1- and the 'ersistence number o ! is 0"

+ou are to &rite a 'rogram that &ill continually 'rom't the user to enter a 'ositi<e integer until 7DF has been
entered <ia the )eyboard" For each number entered your 'rogram should out'ut the 'ersistence o the number"
Please note that the correct s'elling o 'ersistence is '-e-r-s-i-s-t-e-n-c-e" The &ord does not contain the letter 8a9"
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 <alues rom the )eyboard and then call the unction s&a'" The s&a' unction &ill s&a' the original
<alues and then return bac) to main" The unction main &ill then out'ut the <alues contained in the original
<ariables" The <alues should be s&a''ed" 1o global <ariables are to be used" +ou must simulate call by reerence by
'assing the address o the <ariables to the unction s&a'" +ou must be able to do this"
Program 3,% Passing a (-o Dimensional Arra4
Write a 'rogram that &ill read in ! integer <alues into a , ( , t&o dimensional array" The <alues should be entered
<ia standard in'ut" The main 'rogram should then call a unction sum that &ill calculate the sum o the <alues and
return this sum bac) to the main 'rogram" The main 'rogram &ill out'ut the sum" This 'rogram sho&s that you can
successully 'ass a t&o dimensional array in C" +ou should also 'ass the number o ro&s and the number o
columns" @emember- the &ord argument is s'elled a-r-g-u-m-e-n-t"
Prime 3*% Brute +orce Primes
Write a 'rogram that &ill read in 'ositi<e integers and determine i each integer is a 'rime number or not" 6o so the
brute orce &ay by testing or di<isors bet&een 1 and the number and i e(actly t&o di<isors are ound- then the
number is 'rime" +our main 'rogram must call a unction *sPrime" The unction *sPrime returns either a 0 or a 1" *t
does not return the number o the di<isors"
*n so doing- your 'rogram should read in 'ositi<e integers rom a ile" The name o the ile is gi<en on the
command line <ia arg<O1P" +ou should read in the numbers until 7DF is reached as you do not )no& the 'recise
number o numbers in the ile"
The number 1 is not 'rime" The number # is 'rime" Dut'ut your ans&er to standard out'ut"
Problem 3.% Perfect 9umbers
Gree) Kathematicians too) a s'ecial interest in numbers that are eMual to the sum o their 'ro'er di<isors" % 'ro'er
di<isor o n is any number that e<enly di<ides n that is less than n itsel" For e(am'le- : is a 'erect number because
it is the sum o 1- #- and , &hich are the actors o : that are less than :" *n other &ords- &hen you add u' the
actors o a number- e(cluding the number itsel- you get the number"
Similarly- the number #> is a 'erect number because 1 T # T . T ; T 1. add u' to #>"
+ou are to &rite a 'rogram that contains a unction called *sPerect that ta)es an integer n and returns true H1I i n is
'erect and alse H0I other&ise" +our 'rogram should out'ut all the 'erect numbers bet&een 1 and 100-000"
Problem 30% Blan3 ;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<e blan)s by a single blan)" For e(am'le- i the in'ut is
=ets go to the mo<ies"
then the out'ut &ould be0 =ets go to the mo<ies"
The in'ut rom this 'rogram should come rom a ile &hose name has been su''lied <ia arg<O1P" The out'ut rom
this 'rogram should go to standard out'ut" 1ote0 This 'rogram came rom Cha'ter , in your %1S* C te(t boo)"
+ou should rely on the inite state machine conce't to see your &ay through to the solution" This &as discussed in
class"
Program 3#% Left S)ift Plus
Create a 'rogram that has the ollo&ing t&o <ariable deinitions &ith initiali?ers"
int ( S 1 U
unsigned int y S 1 U
Then- use the si#eof o'erator to ca'ture the number o bytes in an int on the machine" Then multi'ly the number o
bytes ound by > since there are > bits/byte" %ssign this <alue to an identiier named limit" Then 'lace the ollo&ing
inside a or-loo' ranging rom 0 to limit-1"
( S ( VV 1U
y S y VV 1U
'rintH8Jd JuWn9- (- y I U
Ceore you enter the or-loo'- 'rint out the initial <alues o ( and y" Ka)e your out'ut loo) readable" That is- ma)e
t&o nice columns &ith a label on each column" %re you able to e('lain the out'utG Ka)e sure that you can e('lain
the out'ut"
Program !0% =reatest Common Di8isor >Brute +orce ?et)od@
% ile contains an un)no&n number o 'airs o 'ositi<e integer <alues" Dne 'air 'er line" +ou are guaranteed that
each member o the 'air o numbers &ill be a 'ositi<e integer" +our 'rogram &ill learn the name o the ile rom
arg<O1P" +ou are to &rite a 'rogram that &ill ind the greatest common di<isor or each 'air o numbers" For this
'rogram- you should use a brute orce method that &ill use a loo' that &ill start at the minimum o the t&o numbers
and count do&n by 1 until the irst common di<isor o both numbers is ound"
Program !$% =reatest Common Di8isor >;ecursi8e 5uclid ?et)od@
% ile contains an un)no&n number o 'airs o 'ositi<e integer <alues" There &ill be one 'air 'er line" +ou are
guaranteed that each member o the 'air o numbers &ill be a 'ositi<e integer" +our 'rogram &ill learn the name o
the ile rom arg<O1P" +ou are to &rite a 'rogram that &ill ind the greatest common di<isor or each 'air o
numbers" +ou should use a recursi<e solution that embodies 7uclids method"+ou can ind the deinition o this
solution in your te(tboo) on 'age ##>" *t is 'roblem ."1;"
Program !% 9on recursi8e +actorial Use a functionA
Dne &ay to e('ress the deinition o the actorial o a number is
1X is 1 ( H1-1I ( H1-#I ( H1-,I ( H1-.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 <alue 0 or a 'ositi<e integer <alue rom the command line and then out'ut the actorial o
that number" +our 'rogram must call a actorial unction" This unction cannot and should not 'rint the ans&er" *t
should return the number bac) to the main unction" The main unction should 'rint the ans&er" The actorial
unction should use in iterati<e solution- non recursi<e strategy" *t should use a or-loo'"
Program !3% ;ecursi8e +actorial
Write a com'lete C 'rogram that com'utes the actorial o a number" This 'roblem is similar to 'rogram .# e(ce't
that the actorial unction must be recursi<e"
Program !!% ;andom 9umbers
Write a C 'rogram that &ill generate 10 random numbers bet&een -1! and 1!" That is a total o ,! 'ossible
numbers since 0 should be able to be generated" 6eine your o&n unction- say myrand- &hich then calls rand"
Program !,% ?alloc Up Space for 1ne <nteger
Write a C 'rogram that &ill malloc u' s'ace dynamically" The s'ace should hold e(actly one integer <alue" %ter
the s'ace has been reser<e- you should de'osit the number : into the s'ace" %ter the number : has been de'osited
into the s'ace you should 'rint the <alue that the s'ace currently holdsHthat is- :I" +ou need to create a <ariable that
contains a 'ointer to an int"
Program !*% ?alloc up Space for a $ Dimensional Arra4 of n <ntegers
Write a 'rogram that &ill read in a 'ositi<e integer number- say n- rom standard in'ut" Then you need to create
enough s'ace in the hea' <ia a call to malloc to hold an array o n integers" +ou then need to use the array inde(
o'erators to ill the array &ith n integer <alues rom entered by the user <ia calls to scanf" The inde( o'erator is also
reerred to as a subscri't o'erator Hie"- %OiP I" %ter the array has been illed &ith numbers- your 'rogram should
&al) do&n the array and build their sum" Print out the sum"
Program !.% (-in Primes
T&in 'rimes are t&o 'rime numbers that dier by #" For e(am'le- , and 4 are t&in 'rimes- so are 101 and 10,"
Write a 'rogram that 'rints all t&in 'rimes less than ,000" +our 'rogram should call a unction Is-rime that returns
1 i the number it recei<es is 'rime and it return 0 other&ise"
Program !0% ;elati8el4 Prime to 3,$
Write a 'rogram that &ill com'ute and 'rint out all 'ositi<e integers that are less than and relati<ely 'rime to the
integer ,41" T&o numbers are relati<ely 'rime i their greatest common di<isor is 1" +ou must call a gcd unction"
Program !#% Bits of +un
Write a C 'rogram that reads in a list o integers bet&een -#0000 and #0000 rom a ile named testdata+." +our
'rogram does not )no& ho& many integers there are in the ile" +our 'rogram reads these integers one by one and
ater each integer is read- it &ill then 'rint out the e(act number o 1s that e(ists in the numbers t&os com'lement
binary re'resentation" +ou &ill need a loo' to read in the number rom the ile until end o ile is determined" +ou
&ill need another loo' inside this loo' to 'rocess each bit &ith a gi<en number" Dne strategy could be to test the
rightmost bit" See i it is a 1 or a 0" +ou can do this by seeing i the number is an odd number or not" * the number
is odd- then a 1 e(ists in the rightmost 'osition" +ou can then rely u'on Cs right shit o'erator to mo<e all o the
bits to right one 'osition" +ou could enter the innermost loo' si?eo HintI L > times" 6o you understand these
&ordsG @emember0 The right shit o'erator in C does not guarantee that a 0 bit &ill be rolled in on the let side" +ou
could test to see i the number is negati<e or not and use a let shit o'erator instead" The let shit o'erator in C
al&ays guarantees a 0 is rolled in"
* the in'ut &as0
4,
,#
1
..
the out'ut &ould be0
.
1
1
,
Program ,0% 9onBpol4morp)ic Lin3ed List
Write a C 'rogram that reads in integers rom the )eyboard" +our 'rogram should then build a lin)ed list in the
hea'" 7ach node o the list contains an integer and a 'ointer to the ne(t node in the list" The inal node in the list has
a null 'ointer" +our 'rogram should insert integers into the ront o the list" %ter end-o-ile has been generated by
the user- your 'rogram uses the list to 'rint out the integers in the order in &hich they &ere ty'ed in" That is- you
need to 'rint the tail o the list irst" This can be easily done using a recursi<e strategy or 'rinting" Eey- ho& does
the user generate 7DF rom the )eyboardG +our te(tboo) e('lains this in cha'ter 1" +our te(tboo) is your best
riend"
Program ,$% <nteger Decomposition
*t is )no&n that e<ery integer n greater than 1; may be &ritten as the sum o three distinct integers- each greater
than one- that are 'air&ise relati<ely 'rimeU that is- no 'air o the three integers ha<e a common 'rime actor" Stated
another &ay- their greatest common di<isor is 1" To illustrate this act- &rite a 'rogram that reads <alues o n rom
successi<e in'ut lines and or each n sho& a decom'osition o n into the sum o three integers a- b- and c by
out'utting a line o the orm0
n S a T b T c
+our 'rogram may out'ut any 'air&ise relati<ely 'rime decom'osition" Furthermore the order o a- b- and c does
not matter" The in'ut integers are located in a ile named testdata41" 7ach integer is a number bet&een 1> and 1000
inclusi<e" 7nd o ile mar)s the end o the data stream"
Program ,% ?atri: ?ultiplication
The irst line o a ile contains an integer number- say n" This number is then ollo&ed by #n ro&s o integer
numbers" 7ach ro& contains n integer numbers" The irst n ro&s o numbers re'resent the <alues in a matri( %" The
second n ro&s o numbers re'resent the <alues o the matri( C" Katri( % and matri( C are both n ( n " Write a C
'rogam that &ill multi'ly these t&o matrices together orming a result matri( that is also n ( n" The <alue o n &ill
not be greater than 40" The name o the ile is in arg<O1P" +our C 'rogram must call a unction Katri(Kult &hich
&ill cause the matri( % and the matri( C to be multi'lied" +ou must- li)e all o these 'rograms- com'ile using the
ansi Wall o'tions" +our 'rogram cannot use global <ariables" 7(am'le data might be0
,
1 # ,
-# -# -.
4 1 0
> 1 .
# 1 ,
# 1 .
Problem ,3% Close 5noug) for All Practical Purposes
This %roblem was ta/en from the 0irst 1nnual 2ortheast 3egional High 4chool -rogramming 5ontest
5ham%ionshi%. The contest was held at Western 2ew 6ngland 5ollege on 7a8 1*, 186.
)his program is not that difficult. *or those of you who are math phobic I believe you can handle this. +emember the
te,tbook 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 can
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<e a''ro(imations using the iterati<e ormula
BnT1 S H1/#I H Bn T %/Bn I
Starting &ith B1 S 1 as an initial a''ro(imation or the sMuare root o % a ne& a''ro(imation B is com'uted using
the abo<e ormula" This ne& a''ro(imation- in turn- can be substituted in the ormula to com'ute a ne&er
a''ro(imation o B" This 'rocess can be continued until the sMuare o the ne& a''ro(imation is close enough to %-
that is
Y B
#
% Y V e
&here e is the 'rescribed degree o accuracy"
Write a 'rogram to com'ute the sMuare root o a number using the 'rocedure described abo<e" The number and the
le<el o accuracy &ill be s'eciied as command line arguments"
* the 'rogram &as launched &ith the ollo&ing command line-
bashN a"out #. "001
the out'ut o your 'rogram should be a line that states something li)e this
The sMuare root o #. is .">!!"
1ote0 +ou should chec) out the unction fabs in %''endi( 6" %re you getting a&ay rom distractionsG Go to the
library" Cring your mathematics boo)" 6o all the 'roblems in the irst our cha'ters"
Program ,!% +ibonacci Se6uence ><terati8e@
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<ious t&o terms"
Write a C 'rogram that contains a unction called Fibonacci" This unction- &hen gi<en a integer n NS 1- &ill return
the nth Fibonacci number" The unction should use a non-recursi<e- iterati<e strategy" The main unction should
'rom't the user to enter numbers until 7DF is generated by the user" 6o not use an array in the 'rogram"
Program ,,% +ibonacci Se6uence >;ecursi8e@
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<ious t&o terms"
Write a C 'rogram that contains a unction called Fibonacci" This unction- &hen gi<en a integer n NS 1- &ill return
the nth Fibonacci number" The unction should use a recursi<e strategy" The main unction should 'rom't the user
to enter numbers until 7DF is generated by the user" 6o not use an array in the 'rogram"
Program ,*% ?a:imum Sum Sub8ector
% ile contains a list o integers" The integers can- and are li)ely to- contain a mi( o 'ositi<e and negati<e <alues"
The name o the ile comes into your 'rogram <ia arg<O1P" The irst n numbers o the ile are read by your 'rogram"
The number n comes into your 'rogram <ia arg<O#P" The number n &ill be no greater than #00-000"
For e(am'le- the ile could contain the <alues0
-, 100 -. -# ! :, -#00 44

+our 'rogram &ill out'ut a number that is the largest sum ound in a sub<ector o this list" *n the e(am'le abo<e-
the out'ut &ould be 10, since that is the largest sum that can be ormed by adding the elements o any sub<ector"
% sub<ector can be the entire list- a single member o the list- or it can be any collection o ad5acent numbers" * the
entire list o numbers contains negati<e numbers then your 'rogram should out'ut 0"
Eere is another e(am'le0 1 . , -. >
The ans&er in this case &ould be 1# since the entire list o numbers orms the ma(imum sum"
Program ,.% Atoi
Write a 'rogram that obtains t&o 'ositi<e integer <alues- one rom arg<O1P and the other rom arg<O#P" The second
number is a single digit" @ecall that these numbers &ill ha<e a 8string9 ty'e and thus they &ill need to be con<erted
to a t&os com'lement re'resentationHa ty'e integerI" +our 'rogram is to determine the number o times the single
digit ound in arg<O#P occurs &ithin the integer <alue contained in arg<O1P"
Program ,0% =uardian Angles
Write a 'rogram that reads in three 'ositi<e integers" The three numbers re'resent the lengths o the sides o a
triangle" +ou are to decide &hat )ind o triangle it is0
Scalene no sides eMual
*sosceles e(actly t&o eMual sides
7Muilateral all sides eMual
@ight triangle Pythagorean theorem is true"
*n<alid triangle see hint belo&"
The Pythagorean Theorem can be stated as ollo&s0 the sum o the sMuares o any t&o sides is eMual to the sMuare o
the third side i and only i the triangle is a right triangle"
Thereore- there are essentially 4 dierent triangle ty'es0 scalene- isosocles- eMuilateral- scalene right- and isosceles
right" +our out'ut should be the most rele<ant ty'e "
To determine i the gi<en <ertices rom a <alid triangle you can use the triangle ine"ualit8 Hgoogle thisI" The
shortest distance bet&een t&o 'oints is a straight line"
7(am'le in'ut
, . 4
,. 1; ,>
, . 10
7('ected out'ut
, . 4 is a scalene right triangle
,. 1; ,>is a scalene triangle
, . 10 is not a triangle
Program ,#% =oldbac)Cs ConDecture
There is a amous mathematical con5ecture called the Goldbach con5ecture- that e<ery e<en integer greater than #
has the 'ro'erty that it is the sum o t&o 'rime integers" Write a C 'rogram that &ill 'ro<e that this con5ecture is
true or e<en numbers bet&een a ST%@T <alue and a F*1*SE <alue" For e(am'le- i ST%@T S ;00 and F*1*SE S
>00 the initial out'ut o your 'rogram &ould be0
;00 S 1; T :>,
;0# S 11 T :!1
The <alues o ST%@T and F*1*SE are to be su''lied by command line arguments"
Program *0% Products of Une6ual Primes
Write a C 'rogram to com'ute all integers bet&een ,00 and 400 that are a 'roduct o a 'air o uneMual 'rimes"
Write each integer to the screen on a se'arate line in the ollo&ing orm0
,01 S ; L .,
,0, S , L 101
3
Program *$% 9umber Pu22le
Write a C 'rogram that &ill read in t&o lines o data" The irst data line contains an integer 1 and the second data
line contains 1 dierent 'ositi<e integers" The 'rogram should arrange these integers so that e<ery number that is
less than or eMual to 1 occurs in its 'ro'er numeric 'lace and all other numbers are in increasing order" +ou may
assume that 1 VS #0" The data should be read into your 'rogram <ia calls to the fscanf unction" The name o the
data is located on the command line as arg<O1P"
7(am'le0 * the in'ut is
;
# ; > 1 #0 ! 4
The out'ut &ould be0 1 # > ! 4 #0 ;
Program *% +ile Comparison
% <ery useul 'rogram is one that is used to com'are t&o te(t iles to determine i they are e(actly the same" Write a
'rogram to com'are t&o te(t iles" The names o the iles are to be su''lied as command line arguments" The
'rogram should &rite a message to the screen indicating that the iles are e(actly the same or that there are
dierences" * the iles dier- the 'rogram should &rite to the screen the line numbers or the lines that are not the
same"
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
8ears ago. 9ou should first understand the bac/trac/ing solution to the :night;s Tour.
There are a''earing some <ery strange 'hone 'ads that contain digit buttonsH809 through 8!9I and other s'ecial
buttons re'resented by 8"9 " % 'hone 'ad consists o a set o hori?ontal ro&s- each containing characters
re'resenting the digit and other non-digit buttons" For e(am'le- the ollo&ing is the e(isting 'hone 'ad0
1#,
.4:
;>!
"0"
For a gi<en 'hone 'ad- you are to determine the total number o se<en-digit 'hone numbers one can dial &ithout
re'eating a digit by mo<ing bet&een digits using only Fnight mo<es as in chess" 1o se<en-digit 'hone number can
start &ith the ?ero digitU also the 'hone number must only contain digits" @ecall that a Fnight mo<es in an =-sha'e-
1 s'ace hori?ontal then # <ertical- or 1s'ace <ertical then # hori?ontal0
* the in'ut 'hone 'ad is incorrect Hbecause o missing digits- du'licate digits- or ro&s o une<en lengthI the
'rogram should out'ut on a single line 7@@D@ and then e(it" The 'rogram irst reads an integer bet&een one
and ten that s'eciies the number o hori?ontal ro&s in the 'hone 'adU thereater- each line contains a ro& o the
'hone 'ad" There &ill ne<er be more than 10 characters 'er in'ut line"
7(am'les
*n'ut Dut'ut
.
1#,
.4:
;>!
"0"
,#
,
1"0,.
4:"!"
";>#"
14
- * - * -
* - - - *
- - K - -
* - - - *
- * - * -
Program *!% S6uare Deal
*n'ut or this 'rogram &ill be t&o 'ositi<e integers entered rom the )eyboard <ia scan" The irst &ill be an odd
integer 1 in the range o , to 14" 1 &ill be used as the si?e o a sMuare array" The second integer &ill be an initial
<alue *" Coth 1 and * &ill be small enough so the * T 1 sMuared is less than 1000"
Cegin in the center o an 1 ( 1 array- &ith the integer *" * * is 'rime- then 'rint the number * in that 'osition o the
sMuare" Dther&ise- 'rint LLL in that 'osition" Ko<e to the right one sMuare- and test the integer *T1 or 'rimality"
Print *T1 i it is 'rime and LLL i it is not" Proceed in a countercloc)&ise s'iral through the sMuare until the sMuare is
ull o numbers and LLL" Then 'rint the array"
7(am'le0 * 1S4 and * S 1: then your out'ut should be0
LLL ,1 LLL #! LLL
LLL LLL 1! LLL LLL
LLL LLL LLL 1; LLL
LLL LLL #, LLL LLL
LLL ,; LLL LLL LLL
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 #=" 1ot 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<e integers"
+our shi' starts at location H=- #=- =I in the middle o the to' ace o this imaginary cube" *n a hy'ercra&l- you
select a direction HT/-I- an a(is H(- y- or ?I- and a distance to tra<el" Dnce this distance has been tra<ersed- you can
select a ne& direction and distance" * the shi' crosses an edge- it &ill immediately &ra' around the edge to e(ist on
the ace neighboring that edge and continue tra<eling until the total distance has been co<ered" *n the igure- assume
= is 10" The shi' ma)es the ollo&ing mo<es0
Ko<e 1e& =ocation
-? 1# 10 1> 0
T( ,. 1: 1> #0
Ty 1 1: 1! #0
%s the shi' mo<es in the ? direction- it crosses an edge and &ra's around to the bac) ace- no& continuing or
some distance in the y direction" When it sto's- the shi' is told to tra<el in the T( directionU this orces the shi' to
&ra' around t&o cube edges- irst changing direction to T? and then to direction S(- e<entually coming to rest on
the ront ace" Finally- there is a Ty mo<e" %t no time can the shi' sto' e(actly on an edge- because that &ould
translate the shi' to an alternate uni<erse"
+ou must &rite a 'rogram or the na<icom'uter that &ill enable Punisher to determine the H(- y- ?I coordinates o
the inal location o the shi' on the cube"
*1PAT
The irst line &ill be an integer =" +ou can assume that = NS ." 1ote that the cube has sides o length #=" 7ach
successi<e line o in'ut &ill contain three <alues se'arated by s'aces0 a directionHT/-I- and a(is H(- y- or ?I and a
'ositi<e integer re'resenting the distance to tra<el in that direction" The inal in'ut &ill be a single 8"9 character"
Start
#=
T(
T?
-?
-y
+ou can assume that no mo<e &ill 'lace the shi' e(actly on an edge o the cube" +ou can assume that all directions
are <alid He"g"- an in<alid direction rom the to' ace is TyI"
DATPAT
+our out'ut &ill be three integers- se'arated by s'aces- re'resenting the inal location"
Sam'le *n'ut Sam'le Dut'ut
10 1: 1! #0
- ? 1#
T ( ,.
T y 1
Program **% (romino (iling

4ome of 8ou are read8 to ta/e u% the challenge that this %roblem offers. 4ome of 8ou are not. If 8ou cannot do this
%roblem right now, do not get o&erl8 discourage. 9ou just need to sol&e a bunch of smaller %roblems first. If 8ou
cannot sol&e this %roblem, then let it go. 4ol&e the ones 8ou can sol&e. -erha%s 8ou can come bac/ to this one later.
%n 1 ( 1 H&here 1 is a 'o&er o t&oI chessboard can be tiled &ith =-sha'ed tiles that co<er three sMuares so that
any gi<en sMuare and only that sMuare is let unco<ered" %ssume that the ro&s o the board are numbered rom one
to 1 rom the to' do&n- and the columns o the board are numbered rom one to 1 rom let to right" The in'ut &ill
be three integers" 1 H a 'o&er o t&o- &ill be no more than si(teenI- the ro& number o the sMuare that should not
be co<ered- and the column number o the sMuare that should not be co<ered" The out'ut should be an 1 ( 1 array
&ith the sMuare not co<ered indicated &ith a blan) and three sMuares co<ered by each tile indicated by a distinct
%SC** character starting &ith X- &hich is ,, in decimal- and continuing &ith successi<e %SC** characters"
7(am'le0 For in'ut o > , :- the out'ut couldH solution not uniMueI be0
# # $ $ ( ( ) )
# $ ( )
% & & * +
% % & ! * * + +
- - . ! ! 2 3 3
- , . . 2 2 1 2
/ , , 0 4 1 1 5
/ / 0 0 4 4 5 5
* may hel' to thin) about the blan) cell as being 8tiled &ith a blan) character9"
This is a classic di<ide and conMuer" For the most 'art- the solution to this 'roblem is in your te(tboo)- but the
out'ut here is dierent and loo)s nicer"
Program *.% 1rder 9BS6uared ?a:imum Sum Sub8ector
This is the same 'roblem as 'roblem 4." The only dierence is that you should 'roduce a solution that is DHn log
nI"
Program *0% 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<ersed" Strings &ill terminate &ith a 'eriod" %ny normal 'rinting
character can be 'art o the string He(ce't a 'eriod &hich terminates the stringI" % 'eriod as the irst character o the
string &ill indicate the end o in'ut"
+our 'rogram should continually reMuest in'ut rom the user and then out'ut the ne& string" 6o not 'ut the 'eriod
at the end o your out'ut string" Words are deined to be entities that are se'arated by one or more blan)s"
7(am'le in'ut0 7(am'le Dut'ut0
This is a sam'le string" string sam'le a is This
This is another sim'le sam'le string" string sam'le sim'le another is This
This- * say- is a great dayX" dayX great a is say- * This-
Program *#% String Pattern ?atc)ing 9aF8e Approac)
Write a 'rogram that &ill 'rom't the user to enter a string o characters" There &ill be a ma(imum o 10 characters
entered or the string" The 'rogram &ill then as) the user to enter lines o te(t" % line is terminated &ith a Wn
character" %ter each line is entered- your 'rogram should determine i the 'attern e(ists &ithin the line" *s the
'attern a substring o the lineG * it is- then you should out'ut 8+es9 and then you should identiy the starting
'osition in the line &here the 'attern matches" 6o this or each line" The end o the out'ut is signiied by an em'ty
line" The user hits the carriage return &ith entering anything" +our 'rogram should not call the strstr unction" *
&ant you to create your o&n strstr unction" +ou can call it strstr"
Program .0% 'als) (ransform
*n class &e discussed the Walsh transorm" *t transorms a <ector o numbers into another <ector o numbers" *t
does so by multi'lying the original <ector on the let by a s'ecial matri(" *t is called a Eadamard matri(" % s'ecial
matri( has the 'ro'erty that it is orthogonal" The matri( contains only 1s and -1s"
For a #-element <ector the matri( is a # ( # o the orm0
1 1
1 -1
We discussed ho& to build the . ( . and > ( > matrices in class" We also discussed in class ho& to &rite a di<ide
and conMuer unction that &ill create an n by n Eadamard matri( on the ly" Write a C 'rogram that irst in'uts a
<alue n that is the si?e o the 'roblem" *t then in'uts i<e n element <ectors and com'utes their Walsh Transorms"
7ach element o the <ector should ha<e a ty'e double" Dut'ut the Walsh transorm o each <ector" @emember to
di<ide by n" +our 'rogram should call a unction called Walsh &ith the ollo&ing interace0
double L&alshH double %OP- int si?e I U
*n doing this 'roblem- ma)e sure that you reali?e that the s'ace or ans&er matri( must be located on the hea' and
not on the run time stac)"
Program .$% Partition Problem
Write a 'rogram that 'rom'ts the user or a number n" This number re'resents the number o 'ositi<e integers that
&ill ollo&" @ead in the n 'ositi<e integer <alues and determine i a 'artitioning o the numbers e(ist" 1umbers can
be 'artitioned i they can be di<ided into t&o grou's- say Grou' % and Grou' C and the sum o the numbers in
Grou' % eMuals the sum o the numbers in Grou' C"
For e(am'le- the numbers 1- .- ,- :- ,- 4 can be 'artitioned into t&o grou's0 1- .- : and ,- ,- 4"
The numbers 1000- 1 -1- 1- 1 cannot be 'artitioned"

+our 'rogram should ne<er say that a 'artition does e(ist &hen it does not" +our 'rogram should ne<er say that a
'artition does not e(ist &hen it does" * a 'artition does e(ist- then you should out'ut one e(am'le 'artition" +our
'rogram should be able to in'ut u' to 100 'ositi<e integers"
This is a good 'roblem"
%n e(ecutable o my solution is called ';1 and it is located in Zcanning/'ublic/101/'roblems" Can you ma)e your
correct 'rogram run aster than mineG Eo& do you )no& that your 'rogram is correctG
Program .% Guic3sort
*m'lement the Muic)sort algorithm" Who created itG 6o you understand itG We discussed it in class" @ead in the irst
n numbers rom a ile that contains lots o integers" Sort them" The number n is gi<en to your 'rogram <ia arg<O1P"
The name o ile o numbers is gi<en in arg<O#P" %ter your 'rogram is done sorting- it should out'ut the irst
number and the last number o the sorted list"
Program .3% 'allpaper
@ead Cha'ter 1 o the Turing Dmnibus" +ou should im'lement the &all'a'er algorithm that is ound &ithin that
cha'ter" The out'ut should 5ust be characters that you 'laced inside a t&o-dimensional array" +ou should then
out'ut the array"
Program .! ?a:imum Sum Sub8ector 1nce Again. >9 log 9 8ersion @
*m'lement the di<ide and conMuer H1 log 1I solution to the ma(imum sum sub<ector 'roblem that &as gi<en to
you in class" The 'seudocode &as gi<en" 6o you understand itG Can you e('lain itG Anderstanding the solution to
this 'roblem is a good thing" Try to igure it out in your head" +ou can do it"
Program .,% ?alloc Up A (-o Dimensional Arra4
The 'ur'ose o the 'rogram is or you to 'ractice reerencing a t&o dimensional array- but the s'ace or that t&o
dimensional array &as created at run-time <ia a call to malloc" @eali?e that Cs OP o'erator Hinde( o'eratorI cannot
be used in a double subscri'ting conte(t unless you do a &or)-around" *n this 'roblem you &ill malloc u' enough
s'ace to hold r ( c integer <alues <ia a single call to malloc &here r is the number o ro&s and c re'resents the
number o columns"
malloc H r L c L si?eo HintI I U
+ou should read in the <alues o r and c <ia the )eyboard using scan"
*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<ely laid out in memory" +ou need to <isuali?e this"
* &ant you to then ill the s'ace" * &ant you to use a nested or loo' to ill the array" 7ach cell should be assigned
the number :" Thus- you need to )ee' trac) o you o&n oset <ia iLc T 5 &ithin a single subscri't" %ter you ha<e
done this- you should &al) the entire array and then add u' each element to orm the sum" +ou should out'ut the
sum"
* &ant you to disco<er that you need to use a single O P" * do not &ant you to use a OPOP" *n act can youG @emember-
&hen you com'ile your code you should not ha<e any &arnings"
Program .*% Comple: 9umber ?odule
* &ant you to create three iles" The irst ile is called ';:"c and it contains- among other things- the deinition o the
main unction" The second ile is called com'le("h and it contains declarations only" *t also contains a ty'e
deinition Hnot a <ariable or a unction deinitionI" *t is a struct that has t&o ields0 one to hold the real 'art o a
com'le( number and the other ield &ill hold the imaginary 'art o a com'le( number" This &as discussed and
'resented in class" The third ile contains the code or t&o e(tern unctions" They are0
e(tern Com'le( %ddRCom'le( H 3 I
e(tern Com'le( Kulti'lyRCom'le( H 3 I
These t&o unctions return a Com'le(- that is- a struct that contains the t&o ields Hreal- imI"
+our main routine should 'rom't the user to enter the irst com'le( number- then it should 'rom't the user to enter
a second com'le( number" +our 'rogram &ill then 'rint out the addition o the t&o numbers ollo&ed by the
multi'lication o the t&o numbers" %1S* C 'ermits the return o a struct"
+ou can do this"
Problem ..% (a:iA (a:iA <Ta/ing from a %rogramming com%etition=
Cac)ground inormation0 *n the early 'art o this 're<ious century and *ndian boo))ee'er named @amanu5an
tra<eled to 7ngland to &or) &ith the amous 7nglish mathematician Eardy" Eardy had been sent 'a'ers &ritten by
the untrained @amanu5an- determined that the &riter &as a genius- and sent money so that @amanu5an could get to
Cambridge" The t&o men collaborated or many years" Dnce &hen @amanu5an &as ill and in the hos'ital- Eardy
&ent to <isit him " When Eardy &al)ed into the room his irst remar) &as- 8* thought the number o my ta(icab &as
1;#!" *t seemed to me a rather dull number"9 @amanu5an re'lied- 81o- EardyX *t is a <ery interesting number" *t is
the smallest number e('ressible as the sum o t&o cubes in t&o dierent &ays"9
* you chec)- you &ill see that @amanu5an &as correct"
1;#! S 1
,
T 1#
,
S 1 T 1;#>
1;#! S !
,
T 10
,
S ;#! T 1000
The ne(t 'ositi<e integer &ith this 'ro'erty is .10."
.10. S #
,
T 1:
,
.10. S !
,
T 14
,
We call numbers li)e 1;#! and .10. @amanu5an 1umbers"
+ou are to in'ut rom the )eyboard a 'ositi<e integer 1 less than or eMual to 1-000-000"
+ou are to out'ut to the screen a table o @amanu5an 1umbers less than 1 &ith the corres'onding 'airs o cubes-
and the cube roots o these cubes" The table must ha<e a''ro'riate headings and also include the order o each
@amanu5an 1umber" The order o 1;#! is 1 because it is the irst such number"
7(am'le0 For in'ut <alue 1 S 4000- the out'ut should be0
Ramanujan First Second First Second Order
Number Cube Cube Root Root
--------------------------------------------------------
1729 1 1728 1 12 1
729 1000 9 10
4104 8 4096 2 16 2
729 3375 9 15
Problem .0% State (ransition ?ac)ine
Consider the ollo&ing state transition diagram/table0
0 $ 3 !
A C C 6 7 %
B C 6 7 % C
C 6 7 % C C
D 7 % C C 6
5 % C C 6 7
The digits 0 through . are the in'ut <alues and the letters % through 7 are the state <alues" Gi<en an in'ut * and a
state s- the ne(t state is located at ro& s- column i" For e(am'le- i * S # and s S c- then the ne(t state is %"
Gi<en a start state s1 and an in'ut seMuence i1- i#- i,- 3- in- the inal state or the seMuence is calculated by locating
the ne(t state Hs#I or s1- then locating the ne(t state or Hs,I or s# and i#- then locating the ne(t state Hs.I or s,
and *,- etc" The state snT1 is the inal state"
For e(am'le- i 6 is the start state and the in'ut seMuence is #- 1- 0- .- then the inal state is 7"
+our tas) is to &rite a 'rogram that reads the start state- reads the in'ut seMuence- and dis'lays the inal state"
The start state and each <alue o the in'ut seMuence &ill be on a se'arate line" % negati<e number &ill mar) the end
o the in'ut" +ou may assume that the in'ut is <alid"
For e(am'le- i the in'ut to the 'rogram is0
C
1#
-1
The inal state is0 C
* the in'ut to the 'rogram is0
%
.
#
0
-1
The inal state is0 7
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"
Cy 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" ,;-
#>S! U #>-1S#; I" This ne& sMuare &as then ta)en as the starting 'oint and the 'rocess &as re'eated until the
numbers 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 rom
the standard in'ut" They &ill be 'ro<ided on one line starting &ith the <alue in the u''er right hand corner o the
sMuare and continuing cloc)&ise" +ou are to 'rint out the <alues at the corners o all the sMuares- including the
initial sMuare- until all the corners are all the same" +ou may assume that the ma(imum number o sMuares &ill be
#0" The original our numbers &ill integers bet&een 0 and 100 inclusi<e"
Sam'le *n'ut0
,; #> 1 #4
Sam'le Dut'ut0
37 28 1 25
9 27 24 12
18 3 12 3
15 9 9 15
6 0 6 0
6 6 6 6
#4
#>
,;
1
1#
#.
!
1#
#;
,
, 1>
!
14
! 14
0
:
:
0
:
:
:
:
Problem 00% Building t)e +ourier ?atri:
The construction o the n ( n Fourier Katri( &as discussed in class" The 'ur'ose o this assignment i or you to
in'ut a number n rom the command line &ith n being a 'o&er o t&o and then ha<e your 'rogram construct the n (
n Fourier Katri(" *n this assignment you should use your com'le("h ile and your com'le("c ile rom 'roblem ;!
abo<e" @ecall that the Fourier matri( is a matri( o com'le( numbers" The real 'art is the cosine o H#LP*I/n L i L 5"
The imaginary 'art is the sine o H#LP*I/n L i L 5"
This 'roblem gi<es you an o''ortunity to 'ractice building a multi-ile 'rogram"
* &ant you to allocate your s'ace or the n ( n array using malloc" +ou should do this in such a &ay that &ill enable
you to use double subscri'ts0 O P O P" @ecall that this &ill reMuire multi'le calls to the malloc unction not 5ust one
call" *t is im'ortant to me that you understand this"
Problem 0$% Selection Sort
% ile contains at most 100-000 integer numbers" The name o the ile is gi<en on the command line as arg<O1P" Sort
these numbers into ascending order using the Selection Sort strategy discussed in class" +ou should remember that
selection sort is an order n-sMuared algorithm and it &ill generally out'erorm bubble sort" %ter you ha<e sorted the
numbers- your 'rogram should out'ut the irst 4 numbers" *t should also out'ut the last i<e numbers too"
Problem 0% <nsertion Sort

% ile contains at most 100-000 integer numbers" The name o the ile is gi<en on the command line as arg<O1P" Sort
these numbers into ascending order using the *nsertion Sort strategy discussed in class" +ou should remember that
insertion sort is an order n-sMuared algorithm and it &ill generally out'erorm selection sort" %ter you ha<e sorted
the numbers- your 'rogram should out'ut the irst 4 numbers" *t should also out'ut the last i<e numbers too"
Problem 03% Discrete +ourier (ransform
To com'ute the discrete Fourier transorm o a <ector o length n reMuires that you multi'ly the <ector on the let by
an n ( n matri(" 7ach element o the matri( contains a single com'le( number" +ou com'uted the Fourier matri( in
a 're<ious assignment"
*n this transorm- or no&- &e &ill not di<ide each resulting com'le( number by n"
*n this 'roblem you &ill read in > loating 'oint numbers rom a ile" The name o this ile &ill be gi<en on the
command line as arg<O1P" 7ach o these <alues re'resents the real <alue o a single com'le( number" The
imaginary 'ortion o the number &ould be initiali?ed to ?ero"
Com'ute the Fourier transorm o the <ector containing these > <alues and out'ut the real and imaginary <alues o
the resulting HtransormedI <ector"
+our 'rogram should not be com'letely &ritten in a single ile" *t should ha<e a main unction &ritten in main"c" *t
should ha<e a com'le("h and a com'le("c ile"
*t should also ha<e a 7a/efile"
When you submit your solution to the grader- you should submit all our iles"
When you 'ass in your 'a'er solution to me- you should 'ass in a 'a'er co'y o all our iles to me"
+ou should create an array o structs"
+ou should create <ectors o structs"
This is not a diicult coding assignment" +ou can do it"
* you need some hel' rom me- as)"
Cest &ishes"
Program 0!% ;e8erse t)e Last n Bits
From time to time- in the course o human e<ents- it becomes necessary to ta)e an integer <alue and re<erse its last
n bits" *n this 'rogram- you are to com'lete the 'rogram belo&" % &or)ing <ersion o this 'rogram can be ound in
Zcanning/'ublic/101/'>." +ou can run my co'y so you can see &hats &hat" This is li)ely to be a challenging
'roblem" * do not &ant to discourage the a<erage student so do not ret too much i you do not get it" * &ould li)e to
see the students &ho are getting all the 'roblems in a timely manner try this one" Dur ability to re<erse the last n
bits o a number 'ositi<ely aects your lie each and e<ery day" The ans&er 'roduced &ill ha<e the u''ermost bits
?eroed out and the last n bits &ill be the re<erse o the last n bits o the argument ("
0 0
1 8
2 4
3 12
4 2
5 10
6 6
7 14
8 1
9 9
10 5
11 13
12 3
13 11
14 7
15 15
/*************************************************************************************/
/* Bit Reverse */
/*************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int reverse ( int x , int n ) {
int answer = 0 ;

/* insert additional declarators and executable lines here as you */
/* see fit. My solution does not add too many lines. */
return answer ;
}
int main ( int argc, char *argv[] ) {
int i ;
for ( i = 0 ; i < 16 ; i++ )
printf( "%d %d \n", i, reverse( i, atoi( argv[1] ) ) ) ;
return 0 ;
This is an e(am'le out'ut or bashN "/a"out .
For e(am'le- you )no& that the number is 0000 0000 0000 0000 0000 0000 0000 0001" * you
re<erse the last . bits- you &ould get 0000 0000 0000 0000 0000 0000 0000 1000- &hich is the
number >"
* you ran bashN "/a"out 4 then the 'air 14 ,0 &ould be 'art o the an&er" WhyG Well- 14 is
0000 0000 0000 0000 0000 0000 0000 1111 and i you re<erse the 4 bits youd get
0000 0000 0000 0000 0000 0000 0001 1110 &hich is ,0"
Dne more0 * you ran bashN "/a"out , then the 'air 14 ; &ould be 'art o the ans&er" WhyG * you
re<erse the last , bits o 14 and ?ero out the u''ermost bits youd get0
0000 0000 0000 0000 0000 0000 0000 0111
Got itG
Program 0,% Alligators and Duc3s
Su''ose there are n duc)s loating on the 'ond in a circle" The 'ond is also home or an alligator &ith a ondness
or duc)s" Ceginning at the 1
st
'osition- the alligator counts around the circle and eats e<ery mth duc) Hthe circle
closing as duc)s are eatenI" For e(am'le- the eaten order &hen nS>- m S . is
4 1 : , # . > ; as sho&n0
The ith duc) is the irst on the menu- the irst duc) is second- etc" Write a 'rogram &hich 'rints out the order o
disa''earance o the duc)s gi<en n Hnumber o duc)sI and m Hinte<al to the ne(t duc) to be eatenI to the screen" %t
least one blan) must se'arate each number" The <alues o m and n are to be entered rom the )eyboard <ia standard
in'ut" +ou should use malloc to create the s'ace or the m duc)s"
Program 0*% S)ell Sort
% ile contains at most 100-000 integer numbers" The name o the ile is gi<en on the command line as arg<O1P" Sort
these numbers into ascending order using the Shell Sort strategy discussed in class" +ou should remember that
selection sort is an order n-sMuared algorithm and it &ill generally out'erorm insertion sort" %ter you ha<e sorted
the numbers- your 'rogram should out'ut the irst 4 numbers" *t should also out'ut the last i<e numbers too"
Program 0.% Ac3ermannCs +unction
Go to htt'0//math&orld"&olram"com/%c)ermannFunction"html and read about %c)ermanns unction" Eere is the
unction0
A(x,y) = y + 1, if x is 0
A(x-1,1) if y is 0
A(x-1,A(x,y-1)) otherwise
1
,
#
:
;
. 4
>
Start
+our 5ob is to 'ass in a hardco'y solution to %c)ermans Function or small 'ositi<e <alues or ( and y" %ttach to
your code a nice loo)ing table" The ro&s o the table re'resent ( in'ut <alues and the columns o the table re'resent
y in'ut <alues" =able your table"
Fill in each cell o the table &ith t&o <alues" The irst <alue is the <alue o the %c)ermann unction or a gi<en (- y
'air" The second <alue is the user time it too) to com'ute the unction using the time utility" Ase the user time
<alue" Ka)e yoru ( and y <alues ha<e unsigned long ty'e" =et your 'atience and/or machine si?e to re'resent an
unsigned long determine ho& big o a table you &ill ill"
Start &ith <alues or ( S 0- 1- #- ,- 3
Start &ith <alues o y S 0- 1- #- ,- 3
1ote0 There is no electronic submission or this 'roblem- 5ust a hardco'y submission"
Program 00% Big+ib
+ou are to im'lement a 'rogram that &ill com'ute to three decimal 'laces the reMuency o a gi<en decimal digit H0
through !I in the decimal re'resentation o a Fibonacci number o gi<en inde( n" @ecall that &e start our seMuence
&ith nS1"
%n e(am'le interaction &ould be0
*n'ut the Fibonacci inde(0 100
*n'ut the digit &hose reMuency is desired0 ,
The digit , occurred &ith reMuency0 0"0!4
+ou must be able to deal &ith large Fibonacci numbers" +our algorithm must be able to handle &ithin no more than
,0 seconds o CPA time Huser timeI- numbers o the order FibonacciH1400I"
Program 0#% ;ecursi8e Digit Sum
@edo the digit sum 'roblem all o<er again- e(ce't this time ma)e sure that it is a recursi<e solution"
Program #0% ;ecursi8e Persistence
@edo the 'ersistence 'roblem all o<er again- e(ce't this time ma)e sure that it is a recursi<e solution"
Program #$% <ndependence Da4
<We ga&e this "uestion on a High 4chool -rogramming 5ontest on 2o&ember +
th
, 1+=
*n 1!!.- July .
th
ell on a Konday" Write a 'rogram that allo&s the user to re'eated enter in a year rom the
)eyboard until 7DF is reaching Hcontrol-6I and then the 'rogram &ill out'ut &hat day o the &ee) July .
th
alls on"
7(am'le in'ut0
1!!.
1>0!
1!:,
#0!:
7('ected Dut'ut0
Konday
Tuesday
Thursday
Wednesday
1ote0 +ou should ta)e into account lea' years" % lea' year is deined as any year such that it is di<isible by . but
not by 100 unless di<isible by .00" For e(am'le- #000 is a lea' year- but 1!00 is not"
+ou may assume that the year &ill be in the range 1>00 to #140 inclusi<e"
Program #% +re6uenc4 Distribution of Arra4 5lements
Write a C 'rogram that reads in #0 integers into an array" 7ach o the integers is bet&een -10 and 10 inclusi<e" *t
then &rites to the screen on se'arate lines each distinct integer along &ith the number o times that it occurs" * a
'articular integer does not occur then do not &rite its reMuency" The lines o integers should a''ear in descending
order" For e(am'le- i you had only the ollo&ing se<en integers0
-; , , -; 4 4 ,
as data in the array- your 'rogram &ould out'ut0
4 occurs # times
, occurs , times
-; occurs # times
+our 'rogram must irst ill your array o &ith #0 integers" 1e(t- you should 'rocess the array and out'ut the results
in the ormat sho&n abo<e" 6o not modiy or rearrange the elements in the integer array once they are stored" %n
e(ecutable image sho&ing you the right ans&er is gi<en in
Zcanning/'ubic/101/reM/reMdist
Program #3% <gpa4 Atinla4
Ta/en from the 18 >ni&ersit8 of ?owell High 4chool In&itational -rogramming 5ontest.
For this 'roblem- you are to translate a 'aragra'h o ordinary 7nglish te(t into Pig =atin" Anli)e real =atin- the
translation rules or Pig =atin are sim'le0
@ule 1I * a &ord starts &ith a consonant- then translate the &ord by 'lacing the initial letter at the end o the &ord
and a''ending a8" 7(am'le0 co& becomes o&cay and blan) becomes lan)bay
@ule #I * a &ord starts &ith a <o&el- then sim'ly a''end &ay to the &ord" 7(am'le0 a''le becomes a''le&ay and
order becomes order&ay"
The letter y should be considered a consonant"
The in'ut ormat o te(t is a series o lines rom standard in'ut" 7ach line containing a set o &ords to be translated"
% line containing i<e 'eriods H3""I indicates the end o the data" For e(am'le0
the Muic) bro&n o(
5um'ed o<er the la?y dog
because it &ould not mo<e
3""
* it hel's- no &ord &ill contain more than 10 characters" 1o line &ill contain more than >0 characters" The data
&ill not contain any 'unctuation or ca'ital letters"
Program #!% Simpl4 (-o Dimensional
The 'ur'ose o this e(ercise is to ma)e sure that you can in'ut numbers into a t&o dimensional array and
demonstrate that you can 'rocess the data in a airly sim'le manner" *n'ut or this 'rogram is a t&o-dimensional
array o loating 'oint data located in a ile named te(tile!." The in'ut array &ill contain , ro&s o data &ith each
ro& containing 4 columns o data" Thus- i the in'ut <alues are0
.",, 4",, 1"11 !!"00 100"00
1"0 ,,", 1#"4 1"1 -1000"00
##"1 11"! #". >", >"!
Then your 'rogram should out'ut0
The a<erage <alues or the three ro&s are0 .1"!4 -1!0".# 10",#
The a<erage <alues or the i<e columns are0 !"1. 1:">. 4",, ,:"1, -#!;";
Program #,% An Arra4 of Structs
This 'rogram is not conce'tually hard"
The 'ur'ose o this 'rogram is to gi<e you a bit o 'ractice mani'ulating structs" To accom'lish this you need to
read the a''ro'riate sections rom your boo) concerning arrays and structs" * you ha<e a Muestion- then 'lease eel
ree to as)" Dne s)ill you are trying to master is the inde'endent reading o technical materialH the te(tboo)I and
then a''lying &hat you ha<e read"
% ile called te(tile!4 has an un)no&n number o 1%K7 and %G7 'airs" The ma(imum amount o 'airs that
could be in the ile is 100" 7ach 'air is ound on a se'arate line" For e(am'le-
Shannon ..
Gauss ,
%chimedes !
@ussell ;;
Asing an array o structs- 'lease read each line o data into your 'rogram- sort the data by 1%K7 Hascending orderI
and 'rint out the name and the associated %G7" %t the end o the sorted list- 'lease 'rint out the a<erage age o the
entire grou' o names" For the abo<e e(am'le data your ans&er &ould be0
%rchimedes !
Gauss ,,
@ussell ;;
Shannon ..
The a<erage age is .0";4 years"
Program #*% +inite Space on t)e &eap
%lthough it may sound strange- * &ant you to &rite a 'rogram that is going to crash" Please &rite a 'rogram that
loo's indeinitely and each time through the loo' * &ant you to malloc u' s'ace or 1000 integers" The 'rogram
does nothing interesting- but * &ant you to get a sense that malloc reser<es s'ace dynamically and you only ha<e a
inite amount o s'ace" The general idea is something li)e this0
while( 1 ){ p = (int *) malloc( 1000 * sizeof( int ) ) ; }
Program #.% ;ecogni2ing a Palindrome
Write a C unction &ith 'rototy'e declaration0 int is_palindrome( char *string )
that returns 1 i the argument string is a 'alindrome and 0 i not" % 'alidrome string is the same character string
&hen read bac)&ards"
Then &rite a C 'rogram to test the C unction is_palidrome(). @ead strings rom the )eyboard in res'onse to
a 'rom't" *dentiy &hether or not the string is a 'alindrome by &riting the a''ro'riate message to the screen" The
'rogram is terminated by 7DF Hcontrol-dI"
Program #0% Print ()4self
* &ant you to create a C 'rogram that can 'rint itsel &ithout o'ening a ile"
* need be- you are 'ermitted to co'y a solution rom the &eb" * you do so- 'lease identiy the lin) &here you got
your solution"
*n either case- sta'le to your hardco'y solution a &ritten e('lanation o the code" +our statement should be
suiciently clear to con<ince me that you understand ho& the code &or)s" +our &ritten &or) should be 'resentable"
* your 'enmanshi' is 'oor- then 'lease ty'e your e('lanation"
There is no need to 'ro<ide an electronic submission or this 'rogram" % hard co'y &ith e('lanation &ill do"
Program ##% Logical Sorting of Strings
% ile contains #0 &ords" Dne &ord 'er line" 7ach &ord has no more than 10 characters" @ead all #0 &ords into an
array" +ou may use the deinition0
char &ordsO#0PO11P"
1ote that each ro& should contain u' to 11 cells since the null byte must be stored too"
1o& create another array containing #0 'ointers to characters" Something li)e0
char L&ordR'trO#0P
The array o &ords can be sorted &ithout e(changing the indi<idual elements o the &ords array" This is done by
creating an array o 'ointers that initially 'oint to the corres'onding array o &ords- element by element"
Hinitially- &ordR'trOiP S [&ordsOiP I"
% sort unction ta)es as arguments the 'ointer array and a si?e <ariable" The si?e <ariable in this case &ill be #0"
The sort unction &ill rearrange the 'ointers Hnot the &ordsI such that the data is sorted i accessed indirectly by
ollo&ing the rearranged 'ointers"
+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 <ia the getc library unction and con<ert all
al'habetic characters to lo&er case and then let rotate the al'habetic character by 1," For e(am'le-
2%- a\ n
2C- b\ o
2K- m\ ?
21- n\ a
etc3
The name o the in'ut ile is gi<en to the 'rogram on the command line as arg<O1P" +our 'rogram should &rite the
encry'ted characters to the standard out'ut de<ice HscreenI <ia the library routine 'utchar" %ll non-al'habetic
characters &hich are read in are to be &ritten to the out'ut unchanged"

You might also like