Code Snippet Index

C

Convert a Number to Words

Do I have to spell it out for you? This short C code will do just that. It will spell out an integer number in English words. The banks do that on large checks, and it would be nice to get one of those every now and then. (Read More)
2

Permutations of String using recursion

This program was written and tested in unix/linux environment (in EMacs editor )with a GCC compiler. (Read More)

Find height of a binary tree

This code enables one to find the height of a binary tree using a queue and a marker. (Read More)

Reversing a linked list - recursively.

This code allows you to create a linked list and reverse it recursively. (Read More)

Comparing Sorting Routines

I have joined the thousands who have done it before, and have compared a number of sorting routines. The sorting is done on the same random-integer arrays. No surprises, quicksort wins this simple comparison hands down. There are clever combinations of sorting routines that are faster, like the... (Read More)

Read a Line of Text from the User, Discard Excess Characters

If the user tries to put 80 characters in a 20-character buffer, you may have issues. This snippet shows one way to cap the input and discard excess input. See also Safe Version of gets() and Read a Line of Text from the User. (Read More)

Days Since a Given Date

This snippet shows one way to calculate the number of days since a given date until present time. Note that this code will not work for dates outside of the current epoch, which typically begins on January 1, 1970. (Read More)

Strings: Delete Trailing Blanks and Tabs

This program use a while loop to delete trailing spaces and tabs from a string. It is meant to be more of an example of the application of the while loop. (Read More)

Strings: Looking at ASCII Code

Commonly strings consist of ASCII characters, some are printable, some are in the backgrounds like the bell, carriage return, linefeed, tab and so forth. This code displays a table of ASCII characters and the corresponding decimal value. As always, you are encouraged to learn here. (Read More)

Convert Fahrenheit to Celsius

A simple program to show you how to create a table of Fahrenheit and Celsius values using a for loop. You may be able to learn from this code. (Read More)

Read a Floating-Point Value from the User, Part 2

Some issues, such as leading whitespace and trailing characters that cannot be part of a number, were not handled in Read a Floating-Point Value from the User, Part 1. Here such issues receive lip service. See also Read a Floating-Point Value from the User, Part 3. (Read More)

Read a Floating-Point Value from the User, Part 1

Obtaining user input can be done in many surprisingly different ways. This code is somewhere in the middle: safer than scanf("%lf", &n), but not bulletproof. It is meant to be a simple but relatively safe demonstration. Note also that there would be slight differences for using float instead of... (Read More)

Read and Write a Structure to a File in Binary Mode

This snippet asks the user to fill in a structure and saves each record to the file. It uses append mode to add each new record to the end of the file. After all input is taken, it prints out the contents of the file. Disclaimer: fread and fwrite may not work portably between different... (Read More)

Read an Integer from the User, Part 2

Some issues, such as leading whitespace and trailing characters that cannot be part of a number, were not handled in Read an Integer from the User, Part 1. Here such issues receive lip service. (Read More)

Strings: Using Pointers

My first experience with pointers. Learned how to spell a string foreward and backward using pointers. I hope you can learn too! (Read More)

Plotting math functions (Python)

VPython's fame is with 3D animated modeling, but it's plotting abilities, while not flashy, are easy to understand and very useful. This program uses VPython to plot math functions y = sin(x) and y = cos(x) and y = sin(x)*cos(x). (Read More)

Modification of Vigenere en/deciphering algorithm

Here's a cute little encipher/decipher program with a Tkinter GUI I wrote a while back. It's an implementation of a derivative of the Vigenere algorithm; the algorithm is taken from Laurence Smith's Cryptography: The Science of Secret Writing, Amazon link here. It's a dated book (and the technique... (Read More)

Word Frequency in a Text String (Python)

This program takes a text string and creates a list of words. Any attached punctuation marks are removed and the words are converted to lower case for sorting. Now you can generate a dictionary of the words and their frequencies. The result is displayed using a sorted list of dictionary keys. A... (Read More)

Timing with Module timeit (Python)

Python has a module specifically made for timing built-in or owner coded functions. This code snippet explains how to use this feature. (Read More)

Testing the Psyco Module to Speed Up Python

The psyco module has been around for a while and has been used to speed up the Python interpreter. For those of you who think speed is all important, take a look at a typical example. Psyco is similar to Java's just in time compiler. How does psyco do it? Well, Python normally compiles source... (Read More)

Timing a Function (Python)

Python24 introduces the function decorator that lends itself nicely to the timing of a function. As a sample function we are using the ever popular and rather stodgy prime number generator. The prime number generator seems to exist only to fluster students and to make niggling comparisons of the... (Read More)

Tkinter Countdown

Just a colorful ten second countdown to New Year. Hope you can learn some code from it. (Read More)

Zip and Unzip Data (Python)

The Python module zlib allows you to compress a typical text string to about one half its original size. A handy feature when you have to transmit or save a large amount of text or data. It saves you time both writing and later reading back the compressed file. The data can then be decompressed... (Read More)

A Simple Look at Bitwise Operations (Python)

Python uses C syntax to perform bitwise operations. Take a look at code that introduces you to bitwise operations at the binary level. (Read More)

Musical Beeps (Python)

If you have a Windows computer you can play musical beeps through the internal speaker. In this case it will sound like a very tiny Big Ben, brought to you by the module winsound and its method Beep(). (Read More)

Class Implements a Structure/Record (Python)

Python does not have a type like struct in C or record in Pascal, but it can be easily implemented with a class. This little code snippet shows you how to do it. (Read More)

A Simple Class Inheritance Example (Python)

Python is entirely object oriented and using classes is made relatively simple. Beginners have a certain angst when it comes to using classes. There is a hump in the learning curve, which I like to overcome with this example. Inheritance really makes sense and saves you a lot of extra code... (Read More)

Area Unit Conversion (Python)

Did you ever want to know how how many square inches are in a square meter? This short Python code allows you to get the answer. It uses a dictionary to simplify the conversions. Could be the start of a nice GUI program using radio buttons. (Read More)

Processing two Lists (Python)

The Python list container can be used for many practical things. In this somewhat light-hearted introspection of modern day politics we are looking at two lists and attempt to clear out elements that shouldn't be in both lists. No offense to anyone is intended, the outcome with the sets was a... (Read More)
1

Which Day of the Week was my Birthday?

A while ago I created a code snippet in C for the same question. Solving this question with Python is a lot simpler, and on top of that Python takes care of impossible dates with the appropriate error message. (Read More)

Python can handle Very Large Numbers

Scientists and deficit spenders like to use Python because it can handle very large numbers. I decided to give it a test with factorials. Factorials reach astronomical levels rather quickly. In case you can't quite remember, the factorial of 12 is !12 = 1*2*3*4*5*6*7*8*9*10*11*12 = 479001600,... (Read More)
C++

Swapping two numbers

This question came up on the forum. How do you swap two numbers without using a temporary variable? I used all the power of my brain to solve this at four o'clock in the morning. In all fairness, swapping two numbers using a temporay variable is about five times faster. This code is just for... (Read More)

transversion system directories

short program that will build a stl::list of directory names and all the files they contain. recursively calls itself when a new directory is encountered. This has been compiled with both VC++ 6.0 and Dev-Shed compiles on MS-Windows XP Pro. It will not work on *nix or probably MAC computers. ... (Read More)

Prime Factorization

The program uses an implementation of the Sieve of Eratosthenes to generate a small list of prime numbers which is subsequently used for direct comparison to find the prime factors for any whole number up to and including 478939. This limitation is due to the primitive method used to generate... (Read More)

Pauls awesome Stack of death

My second C++ lab, bit of fun if i do say so my self. Its a stack that uses a linked list in C++ alot of debugging stuff still in here, but I like the debugging output anyway. Sorry about the lack of comments and (Read More)

Colorful clock of Time

I know what your thinking, You and everyone else has always wanted a clock that sits at the top of your command prompt and tells you the time. Why ? I made this in first year when i spent countless hours programming and little in bed. I decided i needed somthing that told me all the things... (Read More)
C#

Open/Close CD tray

Open/Close CD tray (Read More)

Mandelbrot Fractal Graphics (BCX basic)

A simple experiment with the ever so popular Mandelbrot fractal graphics, call it mathematical art, nice to look at. This set of experiments loops through a number of colors to make the whole thing more exciting. (Read More)
PHP

Array Example

This little snippet will teach you all about arrays and arrays of arrays, and so on and so forth. (Read More)

Multi-Page Search Navigation

Have you ever tried programming one of these: Showing Page 1 of 17 Go To: 1 | 2| 3….. Well I have written a nifty class that will take care of it for you. You can even change the formatting outside of the class. Call this class right after your query and right before where you build your html... (Read More)


About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC