Monday, 28 October 2013

How to Code Efficiently - for Software Programmers



Writing a computer program is a stressful job for programmers out there. Normally when writing a program, a proper documentation is done. Programmers plan the flow of their program using diagrams. There are several methods for the proper planning a computer program. There are many diagram types that can help you plan your diagrams. Few examples are work flow diagrams, ER diagram (database), mind maps and all UML (unified modeling language) diagrams. 

Mind mapping is the virtual brainstorm session of programing. Mind maps are the blue print of any powerful system. It is important to keep these small beginnings clarified, so you won’t get in to trouble of scrambling up the code. Flowcharts can be taken as one of the primary steps when doing a software project. If you are the team lead of a particular software project, you should encourage other users to refer these diagrams to build up a complete system where there will be no errors. When you follow a proper code of conduct your code will be bugs free and you will ultimately include all the effort to your code.


Let’s take a little example of a guessing game. In the specific scenario I use a flowchart diagram (below fig: 1.1). When designing a software program it is always good to pre-design it like in the fig 1.1 since it allows you to visualize what you are doing which will give you an better idea about the outcomes. It also helps you to clearly identify the input, processes and output to the system. And the flow of the diagram will be described virtually with arrows. The program shown here as a flowchart and below in actual computer instructions is a classic example of a computer program.

Fig 1.1




This flowchart is drawn from a Visio alternative called Creately. With Creately it is not hard to draw Flowcharts & it isn’t hard to see what the computer will do when this program runs. Let’s look in to how this flowchart is going to work.
  
  1. As soon as the program starts, computer prints "Guess a number" on the screen. 
  2.  You must input a number from keyboard and then press the return. The program reads the number as soon as you enter it.
  3. The program check for the condition where the number you have guessed is larger than the computers number. If so it will print “Too Big” on the screen.
  4. And if it's smaller, the computer prints "Too Small"
  5. If you did not get the correct answer, the computer reverts to input stage to get another number from the keyboard.
  6. The computer will do the same thing until you get the right answer or until you terminate the program.
  7. After you guess correctly, the computer stops the loop and will let you know by printing "Correct!" and then asks if you would like to "Try Again?"
  8. The program again waits for the keyboard input. If you type “y” and press return the program starts over, picking a new number for you to guess.
  9. Type any other key, press the Enter key, and the program terminates itself.

As you can see, the whole process is crystal clear when you draw it in a flowchart. How does it look when the diagram converted to a program? I used C language to write this program as it’s a language known by everybody. Compile using a C++ compiler.
 
  1. #include <iostream>
    #include <cstdlib>    \\ Deaclaring this library for random numbers
    using namespace std;

    int main() {
                   
                    int iSecret, number; \\ Declaring Variables
                    iSecret = rand() % 10 + 1; \\ Creating a Random Number
                    char a;
                   
                    cout<<"Guessing Game!\n";
                    cout<<"Guess a Number Between 1- 10:\n";
                   
        cin >> number; \\ Getting the number from the user
       
        if (number < iSecret ) \\comparing the number with the guessed
        {
                    cout<<"Too Low\n";
        }
        else if (number > iSecret)
        {
                    cout<<"Too High\n";
        }
        else if (number == iSecret)
        {
                    cout<<"Correct!\n";
        }
       
        cout<<"The Number was =\n"<<iSecret;
       
        cout<<"Try Again?\n";
        cin >> a;
       
        if (a == "y")
        {
                    goto 7;
        }
       
                    return 0;
    }
     

No comments:

Post a Comment