Sunday, March 31, 2013

Car race game in c++

Have you played this came before...
I have created this game in C++, Simple and sweet game.

Nice car game in C++ using Borland Turbo C++ Compiler.
Set BGI Graphics path in initgraph() function according to your pc's Trubo C BGI library.
Here mine is c:\\turboc3\\bgi.

Keyboard Controls to play the game ;
A - Move car left
D - Move car right
Enter/ESC to exit the game.

If your compiler's graphic library is not included then you will find many errors in this program.
To avoid this follow the steps:
1. Go to compiler's option menu (ALT+O)
2. Then select Linker option
3. Then select Libraries option
4. Then Mark X to Graphics Library option using space button
5. Then select OK

All done.
If you want to save this changes permanently then do this:
1. Go to Option menu
2. Select Save then select OK

Still having problem then ask me or comment below.
Enjoy the Game.

A JavaScript Calculator

Copy and paste this code in a notepad and save as a .html or .htm then open this file with any browser to run calculator program.

Output of code will be like this

Monday, March 25, 2013

Program to calculate power of any number with user defined function

Last time we calculated power of any number using inbuilt function power() declared in math.h here.
But in this post we have our user defined power() function that works same as the built in function.

Check this code:

Wednesday, March 20, 2013

Program to use printf() without semicolon

A surprising program,
printf() function call without semicolon !!

Whenever we use a printf() function or any function which ends with semicolon ( ; ), and if we not place the semicolon at the end, then compiler gives an error like "Statement missing ;"

Then how can we do this ??
Check this program which prints a static string without semicolon..

The logic behind is that, an  if statement returns false or zero (0) only when the condition is false or zero (0). In all other cases it returns true, you can try any positive value, any string, character, or any negative value at place of zero (0) in below program. This will explain how a conditional statement works.

Some other programs that also works on this concept:-
Below program is using looping statement for and while. These statements also use condition checking so, they also follows the properties of  if statement mentioned earlier.
Here in program I have also used break statement to break the loop. If break is not used then loop will go infinite.

Program to calculate ACSII value of characters, digits and symbols

Most of character and string operations are based on ASCII values.
ASCII value is standard value for every character, digit and symbol in C compiler.
There are 128 ASCII values but Extended ASCII can represent 256 characters.

A Complete ASCII Table:

A really long chart, hmmm !!
Don't worry you don't need to remember these all values. You can find common ASCII values using a simple logic. What you need to do is -

  • Take a character variable to get user input.
  • Then print that character variable with %d format specifier.
  • It will print ASCII value of that character.
Note: Generally %c format specifier is used with character variable to scan it and print it, but if we use %d to print it, then it will print ASCII value.

A simple program which takes a character input and prints its ASCII  and reduces the burden of remembering all these values :-

Program to reverse a string

Have you ever tried to reverse a string ??
strrev(), a function from string.h is able to do this.
Just pass the string to the function and the string will be reversed.
For example if you pass "Hello" as input then the output will be "olleH".

Try different strings with this function to get reverse.
Have Fun !!

Program to copy a string

Want to copy a string to another ??
Here is a function to copy value of a string to another string. This function strcpy() is also available in string.h header file of C Library. This function also takes two strings as arguments and copies second argument string  to first string.

Check this program to learn more-

Program to concatenate two strings

In string.h header file there is another function to manipulate string, The strcat() function is used to concat two strings. This function takes both strings as parameters and joins them.
Here is the example-

For concatenation of strings using strcmp(), size of first argument must be greater because strcmp() function appends the second string with the first string.
For example if we pass "John" as first string and "Singh" as second string then second string "Singh" will be appended with the first string "John" and after concatenation, value of first string will be "JohnSingh".

Program to find length of a string

If we want to calculate number of characters in a string then it can be done with the help of a function strlen(). This function takes a string as argument and then return the number of characters in string.

As we know that index of array always starts with 0 and every character string contains a NULL character ('\0') . So this function counts the indices of string until it gets the NULL character.

Check this program-

Program to compare two strings

After reading about string. Now we are comparing two strings. It is very easy to compare two numbers using some comparison operators (>,<,>=,<=,==,!=). But if we are talking about strings, these operators are not generally applicable to them. If we want to manipulate a string then we can use some inbuilt functions of string.h header file.
Here we are using strcmp() function to compare two strings. This function takes two strings as argument for their comparison.If both strings are equal then it returns 0. If first string is less then second string then it will return any negative value and in the last case, when first string is greater then second string it returns a positive value.
This function compares characters of both strings according ASCII (American Standard Code for Information Interchange) values, then gives result.

This simple program will clear how comparison will be done-

Program to use string (Character array)

Before we use string we should have an idea about "What is a String ??"
A string is just a character array and follows all the properties of array. If we declare an array with char datatype then it will be called a string.
char array_name[size/limit];

Here in above syntax you see, we are using char as datatype of the array. String also has size or limit for characters as an integer array has. String is used to store character input like name, addresses etc.
We can get a string input using scanf() function with %s format specifier and we can print the string with printf() function with the same format specifier.
But the problem with scanf() and printf() is that, scanf() function only reads string before space character and printf() also prints the string till it don't gets a space character. So we use two different functions gets() and puts() to solve this problem.

gets() is used to get string input and puts() is used to output it. These both functions override the above mentioned problem which we are having with scanf() and printf().

Here is a simplest string program which gets a character string and prints it-

Program to calculate x to the power n using power function of math.h

A simple program to calculate power of a given number.
We have an inbuilt power() function in math.h. This function takes two arguments.


In above example the power() function is taking two arguments. First argument is a number and second one is power() for that number. So this function will return num to the power pow. We are using a temporary variable to hold the result because power() function returns a value.
We can use inbuilt math.h functions for basic mathematical calculations. For example we can use sqrt() function to get square root of a number. ceil(), floor(), log() and many other trigonometric functions are already declared in math.h header file.

This complete program will explain the how power() function really works-

Thursday, March 14, 2013

Program to swap two numbers without using temporary variable

Program to swap two numbers

Program to calculate Compound Interest

Program to calculate Simple Interest

Program to implement Call by Reference (CBR)

Before understanding call by reference, you should have an idea of call by value.

When we passes reference of arguments to a function as actual argument and uses pointer in function definition as formal argument then it will be call by reference.

Pointer:  It is a type of variable which holds address/reference of another variable.

The terms call by value and call by reference are used together. The difference is that in call by value, a copy of actual arguments is passed whereas in call by reference, reference (address) of arguments is passed to the function.
As we know that reference is passed to the function so any changes made to formal arguments will directly affect the value of actual arguments.

Here is the program that will illustrate the concept of Call by Reference :

In this program we are passing address of "a" and "b" using address-of-operator (&). And two pointer variables "x" and "y" are used to hold these addresses.
Here values of "a" and "b" are interchanged after swapping.

Program to Implement Call By Value (CBV)

When we passes any argument to a function, a copy of the arguments is passed to the function.
If we change value of the arguments in the function it will not affect the original arguments which are passed to the function. This is called Call by Value.

The arguments which are passed to a function are known as Actual arguments and the arguments declared in function definition is known as Formal arguments.
In call by value Formal arguments contains copy of actual arguments.
In  following example "a" and "b" are the Actual arguments which are passed to the swap( ) function and "x" and "y" are the Formal arguments .

This program shows the how call by value works-
This is a simple swapping program.Swapping means interchanging. Here in this program a function named "swap( )" is derived for swapping of two variables. We are passing two values as "a" and "b". Check the value of "a" and "b" before calling the swap( ) function. And after execution of swap( ) function, again check the values of "a" and "b". You will see that both values are same. This is because when call by value is performed any changes made to value of Formal arguments does not affect the value of Actual arguments.

Sunday, March 10, 2013

Program to convert temperature

Program to generate Fibonacci series using recursive function

Program to print Fibonacci series

Program to print reverse digits of a number using array

Program to print reverse digits of a number using mathematical logic

Program to calculate factorial using recursive function

Program to find factorial

Program to calculate GCD using recursive function

Program to calculate GCD

Program to check a number is prime or not

Program to find second largest number in the list using sorting method

Program to find GCD and LCM of two numbers

Program to use different user defined functions for lines

Program to sort (order) element of an array

Program to find value at any index in array

Saturday, March 9, 2013

Program to calculate average of n numbers with the help of array

Program to implement a simple function

Program of menu driven calculator using switch case

Program to implement switch case

Programs to know differences between getch(), getche() and getchar()

This post will explain major difference between getch(), getche() and getchar() functions. All of these functions get character input from user.
Generally it is determined that getch() function is used  to  freeze console output. But it happens because whenever getch() function is called it waits for input of keyboard. Until the input is given getch() freezes the output screen. After getting input from user program continues.
getche() also takes character input from keyboard and it also freezes output screen but the difference between getch() and getche() is that getche() displays the character which is inputted by the user whereas getch() does not display the inputted character.
Functioning of getchar() is same as getche() . It also shows the inputted key. But getchar() takes input from user until Enter  key is pressed.

This getch() program will terminate after getting any key as input.

This getche() program will also terminate after getting any key as input but it will display the key which you will press. You can see the output window again using Alt+F5 key combination. If you will try to see the output of above program then you will see nothing because getch() does not show the pressed key.

This getchar() program will terminate when you will press Enter key. Till then it will continue showing the pressed keys.

Program to generate a square table

Program to print 5 numbers using array

Program to print 5 numbers using normal method

Program to check whether a number is even or odd

Program to calculate area of triangle using Hero's formula

Program to calculate area of triangle using base and height

Program to calculate area and perimeter of a rectangle

Program to calculate area and circumference of a circle

Program to print multiplication tables between two limits

Program to print multiplication table

Program to print odd numbers

Program to print even numbers

Program to print 1 to 10 using for loop

A new term Loop
In programming languages loop is used for repetitive execution of statements for certain conditions.

Basically three types of loops are available :

  • For loop
  • While loop
  • Do-While loop
Complete tutorial on loops and break and continue here

In below program we are using for loop to generate counting of 1 to 10, easiest program to understand concept of loop:

Program to compare two numbers using if else

The term conditional statements is used for If, If-Else,Switch etc.
Conditional statements are used to execute a block of code for a specific condition.

Want complete explanation on conditional statements : visit

After getting idea about conditional statements now check this program, this program is using if-else statement to compare two numbers :

Program to calculate remainder

If you don't know what is Remainder ??
Check this... 

We use % (modulus) operator in c to calculate remainder. This operator works on integer type. Because if we use float type variable then there will be no remainder and resultant will also be in float type.

This program will calculate remainder -

Program to perform all arithmetic operations

Program to divide two numbers

Program to multiply two numbers

Program to subtract two numbers

Program to add two variables

After adding two constant values now we are adding two variables.
In this program we are taking two variables a and b of integer type.
And a variable c to store the result of a+b.
Value of a and b will vary according to user input.

Try this program :

Program to add two constants

After printing a simple "Hello" word.
Now its turn to do little advance....

Before learning this you should be familiar to data types and basic operators used with C language.

Here in this program we are adding two constant values and assigning that value to a variable. And then we are printing that variable using printf() function.
Isn't it simple ?

Program to print Hello

Welcome to the world of C Programming

Before you see this most basic program you should know :

Header file :
In easiest manner we can say that a header file contains definition of predefined functions and macros. Extension of a header file is .h.
We have many header files in directory of C compiler. Most common of them are "stdio.h" and "conio.h" .
We use two type of syntax to use these header files-

  • #include<filename.h>
  • #include"filename.h"
If we want to use any predefined function then we can use header file associated with that function.
For example if we want to use printf() or scanf() function then we have to include stdio.h because these functions are defined in this header file.

Syntax is referred as grammar or rules to use a programming language.

Predefined word means already defined in Compiler Library.

Now check this program this will print "Hello" word on the output screen:

In this above program we used three predefined functions clrscr(), printf() and getch().

  • clrscr() function clears previous output of output screen. It works same as the cls command in we use in DOS.
  • Whatever we will type in double quotes of printf() function it will be displayed on output screen.
  • And the third function is getch(). It stops or freezes the output screen until user presses any keyboard key. Two terms getche() and getchar() also used simultaneously with getch(), see the comparative study of then here.

Wednesday, March 6, 2013

Audio and Video Synthesis and Recognition

Audio and Video synthesis & recognition:
It is the latest technique evolved (invented) by current operating system. Basically audio and video speech synthesisers are invented for easy to use and reduce the efforts of users for any particular system.
Audio speech synthesiser is used for conversion of typing commands or text commands in the form of modulated audio input. There is a predictive dictionary available for matching the modulated input. If the modulated input and the predictive text are match completely then user can collect accurate answer otherwise if does not match a distorted message or output would be collected by the user.
Video speech synthesiser is used for accurate calculation of video from a single video entity or among the group of video entities. This type of synthesiser results the accurate video without the help of it’s concerning audio input. Video speech synthesiser is basically used in military services computing science organisations and from the observation of satellite images.

HTTP Requests and Web Server

It is a type of protocol which deals with the web information regarding to the client request. It provides some security for the demanding URLs (Unified/Uniform Resource Locater). Common URL refers to files, directories, or objects that perform complex tasks like database lookup and internet searches.
There are two types of requests available for the HTTP –
                               I.            HTTP GET request
                             II.            HTTP POST request
For each request there are some response should provide from the other side (server).

                               I.            HTTP GET request:
The word get is an HTTP method indicating that client wants to obtain a resource from server. The remainder of the request provides the path name of the resource, protocol name and version number with some optional headers.
                             II.            HTTP POST request:
The HTML post method typically post data to a server so the common uses of post request is to send some form of data or document to a server.

Web server:
                               I.            IIS web server
                             II.            Apache web server

To request document from servers users must know the host names on which the web server software exist. Users can request documents from local web server (client computer) or remote web server.
            Web server can be access through the computer name or through the local host.
Local Host: Host name that references the local machine & normally translates to the IP address (loopback address).

1.     Microsoft IIS (Internet Information Services) server:
Microsoft IIS is a web server that is included with several versions of windows so IIS enables a computer to serve documents. To install IIS server open the “Add or Remove program” from control panel. Click on “Add/Remove windows components”. Then check the checkbox next to internet information services & click next. But it is necessary to keep the original windows operating system disk to complete the installation. There are different versions available of IIS that are IIS 5.1, IIS 6.0 & the latest version is IIS 7.0 & these versions typically uses HTTP.

2.     Apache server:
The apache HTTP server is maintained by apache software foundation. It is currently most popular web server because of its stability, efficiency, portability, security and small size. It is open source software that runs on UNIX, Linux, Windows, Mac OS and many other operating systems.
      In Mac OS and Linux apache is pre installed. After installation of apache HTTP server start the application from the program menu, Click on apache HTTP server then click on control apache server then click on monitor apache server. And if it is necessary for sharing the data, it could be possible from the system preference tab and opening the sharing preference tab and opening the sharing preference pane and click the checkbox next to web share.

Multitier application:
Web based applications are multitier application and sometimes it is called as n-tier application. It is divided functionally into separate tiers. It may be possible that tiers can locate on the same computer. The tiers of web based application generally reside on separate computers.
      Here the diagram represents basic structure of a three tier web based application –

Data or Information tier:
The bottom tier is known as data or information tier which maintains the application’s data. This tier stores data in a relational database management system. This may reside on one or more computers which together comprised the application’s data.
Business logic tier:
The middle tier is known as business logic tier which control information between the application data and its client.
Client tier:
This tier is user interface. It converts tasks into user understandable format.


Data: Unprocessed information (Row facts and figure).

Information: Processed Data.


Database: Collection of data in interrelated manner.


Primary key:
It will be unique in table. Like – University enrolment number.

Foreign key:
A primary key of a table used as a reference key in another table called foreign key.

Composite key:
It is clear that composite means combination of multiple information. If we want to define area then it will distinguish in multiple attributes like street.

Unique key:
It will also be unique but not primary. In this if information will not filled then bloke will be null but in primary key it can’t be null.

SQL Operations:
1.     Create Database
2.     Select Database
3.     Insert Command
4.     Delete Command
5.     Update Command

Data Access Object (DAO)

Data Access Object (DAO):
The Microsoft Data Access Object (DAO) is an approach of database programming which is similar to ODBC. In this approach instead of using CRecordset and CDatabase, we use CDaoRecordset and CDaoDatabase.
Features of DAO are –
1.     DAO is a set of COM interface. These interfaces are set of pure virtual functions.
2.     The required COM model is located in DAO350.DLL.
3.     It has support for Jet Database engine.
4.     The Visual Basic Application (VBA) Automation controller can use DAO object and can make use of the DAO Library.
5.     The MFC classes are used to implement the concept of DAO. The MFC database classes for DAO are –
i.                    CDaoDatabase: An interface for using database.
ii.                  CDaoWorkspace: An interface for managing the single user connectivity with the database.
iii.                CDaoRecordset: An interface for accessing records.
iv.               CDaoTableDef: An interface for manipulating the base table.
v.                 CDaoQueryDef: An interface used to execute queries on database.

Types of Database that can be opened with DAO-
1.     Access Database
2.     ODBC Database Source
3.     ISAM-Type Database Source
4.     External Tables

Powered by Blogger.