Google
Useful Links: ____ Article Search -___ Link Parters ___- Ebook Library___- Product Search

place1holder

.
New Articles
Ebook Library
Link Exchange
Advertising Space
Computer Directory
Join Our Newsletter

Name:
Email:


You will recieve a weekly email that contains new articles, useful product recommendations & more! [Privacy]



place2holder



Further
Reading ...
Web Security As Part Of Call Center Services
When call centers came into being, several web innovations are being done to be part of call center services. Call center services not only took place in a call center but also in many web sites that offered call center services for the benefit of...

If You Want to Tinker, Be a Thinker, or You'll Turn Your Puter into a Clinker
If You Want to Tinker, Be a Thinker - Or You'll Turn Your Puter into a Clinker by Mike Smith We recently received a request for help at the Puter Tutor Message Board, and realized that the answer to the question was one that every computer user...

Free email in the Business World: The unknown risk
In today’s business world it is clear that the most important tool that a business can have is email. Given that fact, it is incredible to think that some businesses still use a free email service. The reason for my concern lies within that “fine...

How to burn a DVD.
How to burn a DVD. DVD-R is a one time burn only. DVD-RW are DVD's that you can copy over many times. Most DVD's will not play in all players or computers. There are many different programs, DVD Studio Pro (for Mac) and Toast 6 (for PC) being the...


 

Incremental Programming




Written By:
John Dirk

Programming Help for Beginners Incremental Programming

Many novice programmers start a program with a "big bang" approach, meaning they want to write the whole program in one step and then only they would try to compile and run. However as humans we make many mistakes when programming, especially given that we have to use a programming language to do the job and most programming languages differ drastically from natural languages. Because it is very likely that we would make mistakes, we have to ensure that we leave room so that we could easily identify the mistakes we have made.

Incremental Programming Helps

The solution to this situation is to adopt an incremental approach in programming. The idea is to keep building your code using several small working pieces. The pieces of code would not do a complete job. Rather they will lay out the skeleton for the final complete program. Once you are convinced the correct skeleton is in place, you could go on and add muscle to the program.

Incremental Programming Process

In incremental programming, the program is incrementally built using several iterations. In each iteration of the program, it is compiled and run to ensure that whatever we have in a given increment is correct. It is very important to always have a working version of the program, no matter how much logic we have implemented so far. Here is a simple algorithm depicting the incremental programming process

<pre>Identify the main

parts of the program Write the initial stub to include all the parts Compile and run to verify the correctness Fix bugs if any While whole program is not complete Change the code to implement more logic Compile and run to verify the correctness Fix bugs if any End while Program complete</pre>

As you could notice in the above process, we always compile and run the program whenever we add some logic. The idea is to ensure that we write a clean program all the time. One would think that this is going to waste time as we compile and run the incomplete program many times. However this process going to save time rather than waste time in the long run. The rationale is that, when you write the whole program at once and try to compile, you would introduce many bugs at once to the program and it takes more time to debug. The incremental approach on the other hand cut down time to debug by eliminating bugs along the way.

An Example

Let's look at a very simple example on how to apply incremental programming. The sample problem is to write a simple billing program for a coffee dispenser utility. The input to the program from the user would be the number of coffee cups. Given the number of coffee cups to dispense, the program is required to calculate the amount and display to the user.

The first step is to come up with the solution

skeleton. The solution consists of main three steps.

<ol> <li>get input</li>

<li>calculate amount</li> <li>display result</li> </ol>

The simplest skeleton one could

write is:

<pre>#include <iostream>

using namespace std;

int main() { cout << "Get input" << endl; cout << "Calculate amount" << endl; cout << "Output result" << endl; return 0; }</iostream></pre>

The next would be to break up the program into functions:

<pre>#include <iostream>

using - continued below ...





continued ...
namespace std;

void getInput(); void calculateAmount(); void printResult();

int main() { getInput(); calculateAmount(); printResult();

return 0; }

void getInput() { cout << "Get input" << endl; }

void calculateAmount() { cout << "Calculate amount" << endl; }

void printResult() { cout << "Output result" << endl; }</iostream></pre>

As you can notice, we just broke the program into a collection of functions. It still does more or less the same thing. Ok the next step is to complete the getInput function. We need to decide on the parameters and return types. We need to read the number of coffee cups. Hence we have to send a reference parameter to the method. Change the signature if the method to:

<pre>void getInput(int &noOfCups)</pre>

and

implement the method like the following:

<pre>void getInput(int &noOfCups) { cout << "Enter the number of cups: "; cin >> noOfCups; }</pre>

We got to change the main program to map this change. So replace the line

<pre>getInput();</pre> with <pre>int noOfCofeeCups = 0; getInput(noOfCofeeCups);</pre>

Now we got to compile and run the program before moving

forward.

Next implement calculateAmount method: We need to define the constant

containing the price of a single coffee cup:

<pre>const float PRICE = 5.50;</pre>

now change the signature of the calculateAmount method and implement it. We need 2 parameters here, the number of cups and the price at which each cup is sold.

<pre>float calculateAmount(int noOfCups, float price)

float calculateAmount(int noOfCups, float price) { return (noOfCups * price); }</pre>

Remember to compile and run again. Finally we implement the printResult

method.

<pre>void printResult(int noOfCups, float price, float amount);

void printResult(int noOfCups, float price, float amount) { cout << noOfCups << " cups of cofee @ " << price << " each

totals to " << amount << endl; }</pre>

Ok the final version of the program is:

<pre>#include <iostream>

using namespace std;

const float PRICE = 5.50;

void getInput(int &noOfCups); float calculateAmount(int noOfCups, float price); void printResult(int noOfCups, float price, float amount);

int main() { int noOfCofeeCups = 0; getInput(noOfCofeeCups);

float result = calculateAmount(noOfCofeeCups, PRICE); printResult(noOfCofeeCups, PRICE, result);

return 0; }

void getInput(int &noOfCups) { cout << "Enter the number of cups: "; cin >> noOfCups; }

float calculateAmount(int noOfCups, float price) { return (noOfCups * price); }

void printResult(int noOfCups, float price, float amount) { cout << noOfCups << " cups of cofee @ " << price << " each

totals to " << amount << endl; }</iostream></pre>

We now have a working program that we are convinced is logically

correct and that solves the problem.The use of a coding standard when

writing the code also made our lives easy.

We would not have this much confidence in our program if we had used the "big bang" approach. Incremental approach is more reliable, safe and easy to follow. For students and beginners this would be an ideal way to approach their programming problems with confidence.


About the Author

None


_Additional Resources ...









Getting Started In Information Technology Computer Consulting
One of the best things about being in the Information Technology industry is consulting. For purposes of this article I'm using the term consulting in reference to side jobs or moonlighting work. While full time Computer Consultants can also...

Is "Spyware" Watching You?
Imagine my surprise when I received a phone call from a friend who told me he'd been the victim of a "spyware" attack that left him shaking at his loss of privacy. I listened to his horror story with a sympathetic ear, but I felt secure since I...

The Basic Facts About Business Web Hosting
There are a number of hosting options, from free to dedicated servers. Let’s spend a few moments and look over the options. Free Hosting The first and not recommended is free hosting. Remember what they say, there is nothing free. Well it’s the...



This website is powered by Hostland ...