-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Sunday 22 November 2015

Turning programmer into a beast

As you know that the semi-colon is normally used to terminate a statement in the field of programming.A programmer tries not to miss semi-colon in his program otherwise his program would be full of errors.

 Do you want to turn a programmer into a beast?
Simply delete the semi-colon from his code and see the magic !!!!

Caution: Do not stay in his room after doing this otherwise he will kill you. :-)


Turning a programmer into a beast

Comparison between C++ and Haskell

Consider that you are a "Haskell" programmer and you have to write several lines of codes to simply print "Hello World". What would be your feelings?

Coding in Haskell

So,as a  C++ programmer,you should be proud of your self as you only have to write cout<<"Hello World"; to print the same statement.

Saturday 21 November 2015

Binary search in C++

"Sequential search" and "binary search" are one of the most important concepts in C++.I have  discussed the concept of sequential search previously.So, today I am going to discuss the concept of binary search in C++.

Binary search

Binary search is a quicker method of searching for value in an array.It is very quick but remember that it can only search a sorted array.It cannot be applied on an unsorted array.

Procedure

The following steps should be followed to search a number in an array by this method.

  • It locates the middle element of array and compares with the search number.
  • If they are equal, search is successful and the index of middle element is returned.
  • If they are not equal, it reduces the search to half of the array.
  • If the search number is less than the middle element,it searches the first half of array.Otherwise it searches the second half of the array.The process continues until the required number is found or loop completes without successful search.
The following program demonstrates the concept of binary search in C++.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void main( )
{
int array[10]={10,20,30,40,50,60,70,80,90,100};
int n;
int mid;
int start=0
int end=9;
int loc=-1;
int i;
cout<<"Enter any number:";
cin>>n;
while(start<=end)
{
mid=(start+end)/2;
if(array[mid]==n)
{
loc=mid;
break;
}
else if(n<array[mid])
end=mid-1;
else
start=,id+1;
}
if(loc==-1)
cout<<"Number not found."<<endl;
else
cout<<"Number found at index:"<<loc<<endl;
getch( );
}


Output

Enter any number:40
Number found at index:3

Wednesday 18 November 2015

Sunday 15 November 2015

Sequential search in C++

Sequential search

Sequential search is also called linear search or serial search.It is a simple to search an array for the desired value.It follows the following steps to search a value in an array:

Steps

  • Visit the first element of the array and compare its value with the required value.
  • If the value of the array matches with the required value,the search is complete.
  • If the value of array does not match,move to next element and repeat same process.
Loops are frequently used to visit elements of array for searching a value.You can start the counter variable of loop from 0 and move it to the last index of an array.

An example to demonstrate this concept is given below:

Source code


#include<iostream>
#include<conio.h>
using namespace std;
void main( )
{
int array[11]={10,20,30,40,50,60,70,80,90,100,110};
int i;
int n;
int location=-1;//location is initially null
cout<<"Enter value to search:";
cin>>n;
for(i=0;i<11;i++)   //you cannot write "for(i=0;i<=11;i++)" ........writing this will generate an error
if(array[i]=n)
loc=i;
if(loc== -1)
cout<<"Value not found in the array.";
else
cout<<"Value found at index"<<loc;
getch( );
}

Output

Enter value to search:40
Value found at index:3

Reference

The reference of this article has been taken from the book, "Object Oriented Programming in C++" by Robert Lafore.

Monday 9 November 2015

C++ manipulators

C++ manipulators

Manipulators are used to change/rotate the output in different styles.They are the mlost common way to control the format of output in C++.

Some of the most important manipulators that are used in C++ are:
  • endl
  • setw
  • setprecision
Today,in my blog,I will discuss the manipulators mentioned above and their functionality in C++.

'endl' Manipulator:

                                      The word  'endl' stands for end of line.It is used to move the cursor to the beginning of the next line.It requires no parameter.

Example:

cout<<"codingwithcplusplus.blogspot.com"<<endl;
cout<<"Blogging my passion";

It will show the output as below:

codingwithcplusplus.blogspot.com
Blogging my passion

'setw' Manipulators:

                                                    The word 'setw' stands for set width.It is used to display the value of an expression in specified columns.The output is right justified by default.It is necessary to include header file "iomanip.h" to use this manipulator.

Example:

cout<<"Hello"<<setw(4)<<"World";

It will show the output as below:

Hello    World

I think no more explanation is required. :-)

'setprecision' Manipulator:

                                               It is used to set the number of digits to be displayed after decimal point.The value is rounded with the use of this manipulator.

Example:

Suppose the value of x=10.0

cout<<setprecision(2)<<x;

It will show the output as:

10.00

Now,consider

cout<<setprecision(5)<<x;

It will show the output as:

10.00000