Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Part I: Programs
Chapter List
Chapter 1: In the Beginning Overview In the beginning, there was the ENIAC Mark I. One day an oper
Chapter 1: In the Beginning
Program 1: Hello World"Hello World" seems to be the first program in almost every programming book,
Program 2: Teacher's ProblemI used to teach C programming. This is the first question from the first
Program 3: Early Morning SurpriseThis program was written by a friend of mine while we were both at
Chapter 2: Starting Out on the Wrong Foot We were all novice programmers once. Back then, we would s
Chapter 2: Starting Out on the Wrong Foot
Program 5: First ErrorsEvery novice programmer starts out learning about simple expressions and how
Program 6: Gotta Have My SpaceHere's a short experiential program written by someone the first week
Program 7: The Crooked SquareThis is a short program to compute and print the squares of the numbers
Program 8: Mad CharacterThe novice programmer decided to check out how to use if statements with cha
Program 9: No CommentThis program computes the area of a triangle. The formula is simple, the progra
Program 10: The Not-So-Great-DivideThis is a simple program designed to figure out how many signific
Program 11: Two Files Is Too ManyThis is another way of doing "Hello World" and screwing up. What's
Program 12: Hurry Up and WaitThe code on which this program is based was written by a senior system
Program 13: The Program Is a Little IffyWhy does this program fail for some amounts? Also, this prog
Program 14: Shifty ProgrammingThe programmer knows that shifting left is the same as multiplying by
Program 15: WordlessThe following program was designed to see if a word is a keyword. Why doesn't th
Program 16: Slow but SureWhy is this program so slow? It takes a minute, 34 seconds on my system to
Chapter 3: One Character Wonders The programs in this chapter all work and do what they are supposed
Chapter 3: One Character Wonders
Program 18: ClassicIf you are a programmer, you've made the mistake contained in this program. If yo
Program 19: Prime SuspectThe program is a simple one designed to check the numbers between 2 and 9 t
Program 20: Simpler Than ExpectedThis program is supposed produce a list of the squares of the numbe
Program 21: No CommentWhat does the following program print? Why? 1 /***************************
Program 22: Getting Too Big for Our ParametersThe idea of this code is simple: Make sure that size i
Program 23: The Long and the Short of ItThe programmer wanted to test out his own version of strlen.
Program 24: Overly Simple DivisionThis program divides two integers. Although it's too simple to fai
Program 25: Maximum SurpriseThe loop in this program is designed to print a greeting ten times. But
Program 26: Trouble AreaThis program is supposed to make sure that the width and height don't get to
Chapter 4: Everyday Problems Every day programmers create new programs. Every day these programmers
Chapter 4: Everyday Problems
Program 28: Zero ErrorThe program is designed to zero out an array. So why doesn't it work? Is memse
Program 29: It's Elementary, My Dear ReaderThe following program is designed to print out a 3-by-3 m
Program 30: A Bit of TroubleThis program uses one variable to hold eight privilege flags. The progra
Program 31: Very Small NumbersThis programmer was smart. He decided to use bitfields to store flags
Program 32: Double TroubleWhy can we never find the double characters? 1 /**********************
Program 33: Bad CharactersThe following program should output ABC. What does it really do? 1 /**
Program 34: Non-CentsThis is a simple checkbook program. The program does a decent job for a while,
Program 35: So You Want to Print a MillionI didn't know we could have commas in C++ constants. So wh
Program 36: Stacked Too HighWhy does this program run out of stack space? 1 /*******************
Program 37: This Program Has a PointThe following program is designed to zero the array data, but so
Program 38: Good ValueThis is a piece of obvious code. So what does it really print? File: main.cpp
Program 39: Kindergarten Arithmetic RevisedWe all know that 1 + 1 = 2 and 1 + 1 + 1 = 3. Also 1/3 +
Program 40: Unbelievable AccuracyThis program is designed to figure the accuracy of the floating-poi
Program 41: A Bit of TroubleThe bit_out goes through a 16-bit value printing out the value of each b
Program 42: A Bit More TroubleWe fixed Program 41 by changing line 19. So now the program works, rig
Program 43: BaselessWe know that 2 is an int. So why does C++ think it's a float and call the wrong
Program 44: Ordering ProblemThe following code is supposed to find the difference between adjacent e
Program 45: Triple SurpriseAre a,b,c in descending order? Does the program agree with you? 1 /**
Program 46: Nothing Goes WrongWhy does the following program sometimes dump core? 1 /***********
Program 47: Microsoft BackwardnessWhy does the following program fail to open the file when compiled
Program 48: File FolliesThe following program works just fine for a while, and then it refuses to re
Program 49: It's As Easy As Falling off a LinkWhy does the following program sometimes dump core?
Program 50: What Is Truth, Anyway?Computers have turned "The truth will set you free" into "The trut
Program 51: A Surplus of PlusesThe programmer tried to do the right thing when defining the ++x and
Program 52: The Case of the Disappearing RectangleWhat's the area of our sample? 1 /************
Program 53: Maximum ConfusionThe max function is simple, the test code is simple, and the answer is
Program 54: Jumping off the Deep EndWhy does this program leak memory? 1 /**********************
Program 55: Sheepish ProgrammingFarmer Brown, a sheep farmer, had a neighbor who could just look at
Program 56: The Magic Is Gone from the ProgramThe following program is designed to see if two files
Program 57: How Not to Read a FileWhat kind of portability problems exist in the following code?
Program 58: Weird NamesThe subroutine tmp_name is designed to return the name of a temporary file. T
Program 59: Son of Weird NamesThis program is designed to generate unique names every time tmp_name
Program 60: Grandson of Weird NamesSo we've fixed our program again and now are using C++ strings. B
Program 61: Looking Through a Dictionary SlowlyI wrote the following program when I was a junior at
Program 62: Zipping AlongWhat could be simpler than assigning a value to two constants and printing
Chapter 5: C Code, C Code Break In spite of the efforts of language designers, there is still a lot
Chapter 5: C Code, C Code Break
Program 64: π in Your EyeThe file math.h defines the constant M_PI. What do we get when we print thi
Program 65: Temporary InsanitySometimes a bogus file name is returned. Sometimes the program dumps c
Program 66: Buffer to NowhereThe programmer decided to speed up the buffered I/O by increasing the s
Program 67: Let's Play "Hide the Problem"The following program dumps core with a floating-point divi
Program 68: MiscalculatingThe assignment here is to make a four-function calculator. The user is sup
Program 69: Sum ProblemThis program is designed to add three numbers, 1, 2, and 3. But when we run i
Program 70: Two SimpleWhy does 2 + 2 = 5986? 1 /************************************************
Program 71: UnsynchronizedThe assignment here is to make a four-function calculator. The user is sup
Program 72: No End in SightThis simple program is to copy the standard input to the standard output.
Chapter 6: Premature Breakage The C++ preprocessor gives you a lot more flexibility with the languag
Chapter 6: Premature Breakage
Program 74: Gross ErrorWhy does the following program report a syntax error on line 16. What's wrong
Program 75: Quick ExitThe ABORT macro is designed to issue an error message and exit. The program sh
Program 76: Double TroubleThe macro DOUBLE is designed to double the value of its argument. The test
Program 77: No ValueThe following program fails to compile because the value is undefined. We never
Program 78: Margin of ErrorIf we have a paper width of 8.5 inches and use 1 inch for margins (1/2 in
Program 79: Square DealC++ doesn't have a power operator, so we define our own macro to compute X2.
Program 80: Area BombingWe need to compute the area of a rectangle. We have the top in two parts and
Chapter 7: Classes with No Class When Bjarne Stroustrup invented C++, he not only created a great pr
Chapter 7: Classes with No Class
Program 82: The Case of the Disappearing ArrayWe have a simple array class and an even simpler test
Program 83: Wild OutputA student of C++ wanted to see how constructors and destructors were called,
Program 84: Construction ProjectThe student wanted to see when the copy constructor and the operator
Program 85: Queueing Up Too LongThis program creates a very simple, well-formed queue class. Yet whe
Program 86: Lack of Self-AwarenessThe following program is designed to test out our simple array. Ye
Program 87: Exceptional ExceptionThis stack class is designed to be more robust and throws an except
Program 88: File This!Due to some brain-damaged program requirements, the following function must co
Program 89: Just Because I'm Paranoid Doesn't Mean the Program Isn't Out to Get MeIn order to illust
Program 90: It's As Easy As Rolling off a LogIn order to track a memory leak, our clever programmer
Program 91: Stacked WrongIn the following program we define an unsafe class, stack, and a safer vers
Program 92: Name GameWhat does the following program print? File: first.cpp 1 #include <string
Program 93: No MagicSomething strange was happening to the class info. Your valiant author was assig
Program 94: Speed KillsThe new and delete function calls are costly. If you want to speed up your pr
Program 95: Sending the Wrong MessageWhy does this program generate strange results? 1 /********
Program 96: Pure FunThis program is based on a simple idea. Let the list class handle the linked lis
Chapter 8: Expert Confusion Welcome to one of the toughest parts of the book. The few programs in th
Chapter 8: Expert Confusion
Program 98: Debug ResistantThe programmer had a clever idea. He would put a bunch of code in an:
Program 99: Phantom FileThere's no file named delete.me in our directory. So why does this program k
Chapter 9: Portage to Hell C++ is supposed to be a portable language. It's a lovely phrase, "suppose
Chapter 9: Portage to Hell
Program 101: Point of No ReturnWhy does the following program write out a correct file on UNIX and a
Program 102: Zipping AlongOn most UNIX systems, this program works. On MS-DOS, it does not. Why?
Chapter 10: A Few Working Programs Programmers love tricks. In this chapter, we take a look at some
Chapter 10: A Few Working Programs
Program 104: Nothing SpecialWhat's the purpose of the funny if statement in the subroutine below? It
Program 105: Waving the FlagOne of the problems with cute tricks is that far too many programmers do
Chapter 11: Threaded, Embedded — Dreaded Overview When NASA attempted to launch the first space sh
Chapter 11: Threaded, Embedded — Dreaded
Program 106: Taking Out the TrashWe have a memory-mapped input port pointed to by in_port_ptr. The d
Program 107: Better Trash CollectorWe've fixed Program 106 by adding the keyword "volatile." But thi
Program 108: Short TimeThe programmer needed to create a precise short delay in his program. He disc
Program 109: Short Time RevisitedThe programmer attempted to fix Program 108 by changing the multipl
Program 110: Short Time IIIProgram 109 has been fixed. Now the delay is closer to what we expect. No
Program 111: A Bump on the Race TrackThis program starts two threads. One reads data into a buffer,
Program 112: Hurry Up and WaitFor some reason this program runs for a while and then stops: 1 #i
Program 113: Flag WavingThis program contains a small part of the UNIX terminal driver. (The UNIX te
Program 114: Slow ProgressThis program consists of two threads. The first, sum, does some work that
Part II: Hints Hint 1: During the early days of the railroads, they had a problem with trains hitti
Part II: Hints
Part III: Answers Answer 1: The problem is that a large herd contains 10,000 sheep. That's 40,000 l
Part III: Answers
Chapter 4: Everyday Problems
Chapter 7: Classes with No Class
Chapter 10: A Few Working Programs
← Prev
Back
Next →
← Prev
Back
Next →