Tuesday, January 31, 2012

Finding 1’s Complement of a Binary Number is C

The one;s complement operator (~) is a unary operator that causes the bits of its operand to be inverted ( i. e. reversed) so that 1s becomes 0s and 0s becomes 1s. This operator always precedes its operand, The operand must be and integer-type. The one’s complement operator is sometimes referred to as the complementation operator. It is a member of the same precedence group as the other unary operations. Thus, its associativity is right to left. Now the following example illustrates the implementation of 1’s complement operator is a program,

Source Code:

#include <stdio.h>
int main(){
    unsigned i = 0x7fff;
    printf("hexadecimal value: i = %x   ~i = %x\n", i, ~i);
    printf("decimal value: i = %u    ~i = %u\n", i, ~i);
    return 0;


hexadecimal value: i = 7fff   ~i = ffff8000
decimal value: i = 32767    ~i = 4294934528

Monday, January 30, 2012

Implementation of Dijkstra’s Shortest Path Algorithm in C++

Dijkstra’s Shortest Path Algorithm is popular algorithm for finding shortest path between different nodes. The algorithm (Pseudo Code) is as follows
procedure Dijkstra (G): weighted connected simple graph,
with all weights positive)
[G has vertices a = v0, v1, ..... , vn = z and weights w(v1, v2)
where w(vi, vj) = INFINITY if [vi, vj] is not an edge in G]

for i := 1 to n
L(a) := 0
[ the labels are now initialized so that the label of a is 0
and all other labels are INIFINITY, S is empty set]
while z is not belongs to S
u := a vertex not in S with L(u) minimal
S := S U [u]
for all vertices u not in S
If L(u) + w(u,v) < L(v) then L(v) := L(u) + w(u,v)
[this adds a vertex to S with minimal label and updates the labels
vertices no in S]
end [L(z) = length of a shortest path from a to z]

Sunday, January 29, 2012

Transformation (Translation, Rotation and Scaling) of a two dimensional objects in C/C++

1. Translation
A translation is applied to an object by repositioning it along a straight-line path from one coordinate location to another. We translate a two-dimensional point by adding translation distances, tx and ty, to the original coordinate position (x,y) to move the point to a new position (x’, y’)
x’ = x + tx,         y’ = y + ty
The translation distance pair (tx, ty) is called a translation vector or shift vector. In matrix form the translation of a two dimensional object can be written as 
2. Rotation

Saturday, January 28, 2012

Random number generation in C/C++

Many programs need random number to complete their certain goal. For example, a quiz program needs random number to randomize the question number to be asked first. Likewise a dice game needs random number to select the face when it is thrown. So there are many situations where the need of random number has no alternatives. Almost all programming languages provide the way to generate random numbers. In the same way C/C++ also provide some functions that generate random numbers. stdlib.h in C and cstdlib in C++ provides a function rand() to generate a random number. This function, when called, returns a integer from 0(0x0000) to 32767 (0x7FFFF).

Friday, January 27, 2012

New Standard Flowchart of for loop.

An ISO committee purposed a new Standard Flowchart of for loop. The previous flowchart of for loop is same as that of while loop. The new flowchart is shown below. Here initialization is the initialization of variable like A = 0 or A = 5, which is the value of variable before loop is executed. The increment/decrement is the updating part like A++ or A—or something like that. Here the variable is either incremented or decremented as according to the need of problem. The exit condition is the condition for a loop to be terminated. If exit condition is met, control is transferred out of the loop body. Note carefully that it is exit condition but not loop condition. For example if a loop condition is A<=10, then exit condition must be A>10

Thursday, January 26, 2012

Mini project Database Management System (DBMS) using wxWidget in C++

wxWidget is a very good library for GUI programming. It helps to build windows form very effectively. Many application we are familiar with are made in wxWidget (like Code::Blocks). But wxWidget is totally object oriented and seems to be a headache for a beginner. Even to write Hello World program, it needs a lot of knowledge and code conversion. But once you master it, then everything gets simpler and you can build a big application within few weeks. Now, lets move to our topic. Mini project Database Management System is built totally in C++ with wsWidget as Graphics Library. Obviously, it is built in Object Oriented fashion because both C++ and wxWIdget are object-oriented. In this project you can make a new database, make tables, give fields and everything, For security region, the database is fully password protected and a file is used to store the passwords. You can also select existing database and modify it. Since it is a mini project, don’t expect too much from. Just download and see the code, get some idea and start to build your own application. Hope this project help you. If you have any questions and suggestions regarding this project or this blog, don’t forget to comment, this will help me to do my job effectively. Thank you

The source code is available on GitHub.

Wednesday, January 25, 2012

GLUT Tutorial – How to detect a Mouse Click and Mouse Motion

In previous tutorial, we discussed about handling Keyboard event using GLUT. In this tutorial we pay our close attention towards Detecting Mouse click and Mouse motion.
Detecting Mouse Click
GLUT provides a function glutMouseFunc which is responsible for detecting mouse click generated by a program. Its syntax is
void glutMouseFunc(void (*func)(int button, int state, int x, int y));

Tuesday, January 24, 2012

Drawing an Ellipse with Mid Point Ellipse Algorithm in C/C++

The theory for drawing Ellipse with Mid Point Algorithm is same as that of Circle drawing. But the difference is that the Ellipse is divided into two regions because it has two radii. The regions are separated from each other at a point where the slope of the tangent line is –1. So we need to draw 2 regions in first quadrant and draw in other quadrant symmetrically.
The following section implements Mid – Point Ellipse Algorithm in C/C++. The source code is complied using gcc Compiler and Code::Blocks IDE. To print a pixel, SetPixel() function of windows.h is used.
Note: to run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGW\lib )  in linker setting.

Monday, January 23, 2012

Drawing a Circle with Mid – Point Circle Algorithm in C/C++

As in the previous line drawing algorithm, we sample at unit intervals and determine the closest pixel position to the specified circle path at each step. For a given radius r and screen center position (xc, yc), we can first set up our algorithm to calculate pixel positions around a circle path centered at the coordinate origin (0, 0). Then each calculated  position (x, y) is moved to its proper screen position by adding xc to x and yc to y. Along the circle section from x = 0 to x = y in the first quadrant, the slope of the curve varies from 0 to –1.  Therefore, we can take unit steps in the positive x direction over this octant and use a decision parameter to determine which of the two possible y positions is closer to the circle path at each step. Position in the other seven octants are then obtained by symmetry.

How to do Texture Mapping in OpenGL?? – A sample example.

Texture mapping is the process of applying colors from an image to an object created in OpenGL. A simple example would be, applying a brick image onto a square which makes the square look like an original wall.
OpenGL supports four basic texture map types. 1D, 2D, 3D and cube map textures. The following code is an example of a 2D texture mapping, which provides a basic usage of textures.
Note: To run this code on your machine, make a bitmap image (64 x 64) and name it floor.bmp . Keep this image in the same folder where your exe file is located. If you are Code::Blocks user, then compiling from IDE results in Error, it says that it couldn’t found the Image. So just compile it and run the exe  file which is  inside debug or release folder. Visual Studio user can directly run this program without even including <windows.h> but Code::Blocks user must include <windows.h>.

Sunday, January 22, 2012

Numerical Method: Newton’s Forward and Backward Interpolation in C/C++

int main()
    float x[10],y[15][15];
    int n,i,j;
    printf("Enter n : ");
    for(i = 0;i<n;i++){
            scanf("%f %f",&x[i],&y[i][0]);
    //forward difference table
            y[i][j] = y[i+1][j-1] - y[i][j-1];
    printf("\n***********Forward Difference Table ***********\n");
//display Forward Difference Table
    //backward difference table
//for j = 0 initially input is taken so we start from j=1
            y[i][j] = y[i][j-1] - y[i-1][j-1];
    printf("\n***********Backward Difference Table ***********\n");
//display Backward Difference Table
return 0;

Friday, January 20, 2012

Implementing Bresenham’s Line Drawing Algorithm in C/C++

Bresenham’s Line Drawing Algorithm is an accurate and efficient raster line-generating algorithm developed by Bresenham. In this algorithm, we first consider the scan – conversion process for lines with positive slope less than 1. Pixel positions along a line path are then determined by sampling at unit x intervals. Starting from the left end point (x0, y0) of a given line, we step to each successive column (x position) and plot the pixel whose scan – line y values is closet to the line path. Assuming we have determined that the pixel at (x(k), y(k)) is to be displayed, we next need to decide which pixel to plot in column x(k+1). Our choices are the pixels at positions (x(k) + 1, y(k)) and (x(k) + 1, y(k) + 1).

Drawing Straight Line using DDA Algorithm in C/C++

The digital differentia analyzer (DDA) is a scan-conversion line algorithm. In this algorithm, we sample the line at unit intervals in one coordinate and determine corresponding integer values nearest the line path of the other coordinate and plot those coordinate (pixel) in computer screen. Consider first a line with positive slope. If the slope is less than or equal to 1, we sample at unit x intervals (dx = 1) and computer each successive y value as y(k+1) = y(k) + m. Subscript k takes integer values starting from 1, for the first point, and increases by 1 until the final endpoint is reached. For lines with a positive slope greater than 1, we reverse the roles of x and y.That is, we sample at unit y intervals (dy = 1) and calculate each succeeding x value as x(k+1) = x(k) + (1/m)

Wednesday, January 18, 2012

SetPixel() function on windows.h . How to use it??

If you are learning computer graphics, you may need to create graphical structure using pixels. When trying to implement this in program, you need the programming language that has facility to print a single pixel on the screen i.e. the language that supports pixel operation. Fortunately almost all Lagrange with GUI facility support pixel printing  Now what about C/C++? You cannot print a pixel in console window. You need some graphical mechanism for this. Fortunately windows user can use windows.h library for this.
windows.h provides a function SetPixel() to print a pixel at specified location of a window. The general form of the function is
SetPixel(HDC hdc, int x, int y, COLORREF& color);
where, x and y are coordinates of pixel to be display and color is the color of pixel. To print a pixel only writing SetPixel command and embedding windows.h is not sufficient. You need some extra linking operation for this. You must link the library libgdi32.a in your IDE's linker setting before using the SetPixel function. The following program draws a horizontal line to a window. If you have any confusion please ask me in the form of comment, I will reply you as soon as possible.


To run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGW\lib )  in linker setting.

Tuesday, January 10, 2012

Game project “Tank Game” using C++ with Allegro. Source code and Exe – Free Download

“Tank Game” is a very interesting game. The game is built in C++ platform with Allegro as Graphics Library. If you are planning to develop a game, then allegro is also there for you. It supports 2D game and has so many features for Game programming than SDL. The game is divided into many levels. The user is redirected to another level if he completed one level successfully. In order to complete a level, a tank must defeat its enemies. One interesting point about this game is that it uses Artificial Intelligence. All enemies are provided with Artificial Intelligence to find out the direction of tank.
The full source code with executable file can be downloaded from Here

Monday, January 9, 2012

GLUT Tutorial – Handling Keyboard Events

Almost all of the games use Keyboard to control the objects i.e. to move, jump, turn, slide etc.  Keyboard events is essential part of game programming. All game development libraries support Keyboard Handling. GLUT also has nice and simple functions that provide the facility of Keyboard controlling. GLUT provides different functions to handle NORMAL key events like 1 , 2 , A, B, ESC (having ASCII code)  and SPECIAL key events like UP, DOWN, F1 etc.

Sunday, January 8, 2012

Some Lab assignments on 8086 programming I

Question :  Write a program to display a string "Programming is Fun" in the screen using string displaying function
    .STACK 64
    MOV AX,4C00H
    INT 21H

    MOV AH,09H
    INT 21H

Question: Write a program to display the same string using character reading function (use current address operator $ to count the no of characters)

Friday, January 6, 2012

Android – Why Google map is not displayed in my application??

There are some reasons due to which a Google map is not displayed in Android Application instead a grey portion with squares is displayed. Some possible reasons are as follows.

Do not using API key : In order a Google Map to be displayed on your application, you need a unique API key. You need to set it in the constructor of the MapView or using the attribute android:apiKey if you are constructing a MapView from an xml layout. If you do not use API key, Google map will not display.

If API key is wrong: You need to specify the Google maps key that is generated for the keystore you are signing your application with. If you supply other API key that is not generated for the keystore that you sign you application with, no map will be displayed.

If you don’t have internet permission: Google map needs an internet connection. If your android OS doesn’t give the permission for internet, then the map will not display. To request an internet permission, add the following tag on AndroidManifest.xml file outside <application></application>.

<uses-permission android:name="android.permission.INTERNET" />

If you don’t include map library: This may be the another reason for the problem. Use the add the Google map library inside <application></application> as below

<uses-library android:name="com.google.android.maps" />

Thursday, January 5, 2012

Android - How to obtain Google API key for Windows ?

There are some steps to obtain a Google API key for windows. The steps are described in detail below.
While you are developing and debugging your application, you will likely be sigining your application in debug mode — that is, the SDK build tools will automatically sign your application using the debug certificate. To let your MapView elements properly display Maps data during this period, you should obtain a temporary Maps API Key registered to the debug certificate. To generate an MD5 fingerprint of the debug certificate, first locate the debug keystore. The default location of debug keystore is     

I am planning to develop an Android Application in JAVA platform. Where should I start??

When I was thinking to learn android application development a year ago, I had a same problem, where should I start from?  If you guys are also planning to learn android application development and don’t know where to start then don’t worry. This post will be very useful to you.

Language Selection : Java is best language for Android Application Development.If you are not familiar with JAVA, then hurry up and learn JAVA. JAVA is a very powerful, portable, object-oriented and platform independent programming language. If you are familiar with C++ , C# or any other Object-Oriented Language then it will be very easy for you to learn JAVA.

IDE Selection:  Eclipse is best IDE (Integrated Development Environment) to learn android. It has good supports for android. You can use either Eclipse Helios or Eclipse Indigo, but I recommend Eclipse Indigo . Search on Google and download the Eclipse. You can find a lot of tutorials and Documentation in Web for android with Eclipse than other.

Download SDK:  Android SDK  (Software Development Kit) is a core part of Android Application development. In short, SDK is a set of development tools used to develop application for android platform. The Android SDK includes sample projects with source code, development tools, an emulator, and required libraries to build Android applications. You can download Android SDK from http://developer.android.com/sdk/index.html. There is a steps to follow while downloading SDK. So download and install SDK before running to application development.

Download Eclipse ADT:

Android Development Tools (ADT) is a plugin for the Eclipse IDE that is designed to give you a powerful, integrated environment in which to build Android applications.

ADT extends the capabilities of Eclipse to let you quickly set up new Android projects, create an application UI, add components based on the Android Framework API, debug your applications using the Android SDK tools, and even export signed (or unsigned) .apk files in order to distribute your application. To download the ADT, click the link http://developer.android.com/sdk/eclipse-adt.html and follow the steps.

If you successfully completed the above steps, you are ready to build and learn android application development.