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

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








Saturday, 24 October 2015

Concept of enumeration in C++

You have often heard about "enumeration" in C++.Today I will explain this concept with the help of articles taken from different websites.

Definition of enumeration

Enum(enumeration) is a user-defined type consisting of a set of enumerators( enumerator --- named integer constant).

The idea behind enumerated types is to create new data types that can take on only a restricted range of values. Moreover, these values are all expressed as constants rather than magic numbers--in fact, there should be no need to know the underlying values.

Declaration of enumeration

The enum is declared as:

enum enum-type-name { enum-list } enum-variable;

In this form, enum-type-name is optional. However, if you want to use enum type in several places, it is better to use another way of enum declaration:

enum enum-type-name { enum-list };

//... (and somewhere below)

enum enum-type-name enum-variable;

Of course, in the second case enum-type-name cannon be omitted.

An example is given below which will show you how to declare enumeration in C++.
enum e_acomany
{
Audi,
BMW, 
Cadillac, 
 Ford,
 Jaguar, 
 Lexus, 
 Maybach, 
 RollsRoyce, 
 Saab
};

Below is a sample code that will help you to undersatnd this concept.

Source code

#include<iostream>
using namespace std;
enum colours{blue,red};
int main( )
{
colours c;
c=blue;
cout<<"Blue is at number:"<<c<<endl;
c=red;
cout<<"Red is at number:"<<c<<endl;
return 0;
}


Output

Blue is at number:0
Red is at number:1

Credits


For this blog,I have taken help from the following websites:







Saturday, 17 October 2015

Importance of pointers in C++

Today I found a very helpful article on "programmers.stackexchange.com" regarding the importance of pointers in C++.This article was written by David Thornley.So,I decided to share it with all my readers.

Importance of pointers in C++

Pointers are necessary for dynamic memory location, many data structures, and efficient handling of large amounts of data. Without pointers, you'd have to allocate all the program data globally or in functions or the equivalent, and you'd have no recourse if the amount of data grew beyond what you had originally allowed for. I hesitate to use absolutes here, but as far as I know all modern computer languages have pointers in some form or other.
In most languages that use pointers, there are certain sorts of references that are pointers, and perhaps certain sorts of references that aren't, and there is no further notational difference. A Lisp cons cell is a pair of pointers, although a fixnum is not a pointer. In Java, the variable used for the instance of a class is a pointer, but an int isn't. The language syntax doesn't reflect that.
C is unusual in that pointers are optional, explicit, and allow explicit pointer arithmetic. It is perfectly possible to write struct foo bar; struct foo * baz;, and once you've allocated memory for bazyou can use both bar and baz to represent struct foos. Since pointers are optional, it is useful to have notational differences. (It's essential in C++ for smart pointers, as given boost::shared_ptr<foo> bar;bar.reset() has one meaning and bar->reset() is likely to have a much different one.)
(Actually, explicit pointers were often used in other languages when C was originally being developed, such as ^ in Pascal. C is an older language than most in common use today, and it shows.)
One of C's design goals was to write Unix in, and therefore it needed to handle memory locations in a detailed manner. (C is actually one of a family of system implementation languages common when it was being designed, another example being Cybol for Control Data computers. C is the one that became a big hit.) Therefore, it is possible to manipulate C pointers directly, assigning memory addresses and calculating new ones. This also led to some design decisions in C. C arrays are based heavily on pointer arithmetic, and indeed an array decays into a pointer in very many situations. Passing variables to C functions by reference is done by pointer. There was no strong need for arrays and passing variables by reference in the form that other contemporary languages had, so C didn't get those.
So, the answer is that, in most languages nowadays, you use pointers constantly without being reminded of the fact. In C, and to a lesser extent C++, you use pointers either to do low-level things, or as accomplish higher-level things that there's no special notation for.