Sunday, 6 December 2015
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. :-)
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( );
}
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.
Saturday, 14 November 2015
Comparison between C++ and Java
Now you should be a proud C++ programmer !!!!
Resource
This pic has been taken from groupinfo4u.com .
Wednesday, 11 November 2015
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.
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.
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 } enum-variable;
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.
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:
Tuesday, 20 October 2015
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 baz
you can use both bar
and baz
to represent struct foo
s. 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.
Labels:
C,
C++,
C++ coding,
C++ programming,
C++ programs,
Coding,
CPP,
khgamujtaba,
Pointers,
Programs
Friday, 16 October 2015
Thursday, 8 October 2015
Pointer Declaration in C++
Pointer Declaration
As I have discussed in the previous blog that a pointer is a variable whose value is the address of another variable.Remember that the declaring a pointer is same as declaring a simple variable.An asterisk "*" is used in the declaration that indicates that the variable is a pointer variable.
Syntax
The syntax of declaring a pointer is as follows:
Example
int *p;
Now,I will demonstrate this concept by a simple program.
Source code
#include<iostream>
#include<conio.h>
using namespace std;
void main( )
{
int n;
int *ptr; // it indicates that "ptr" is a pointer
cout<<"Enter an integer";
cin>>n;
ptr=&n;
cout<<"The value of n:"<<n<<endl;
cout<<"The address of n:"<<ptr<<endl;
getch( );
}
Output
Thursday, 1 October 2015
Monday, 28 September 2015
Example of pointers in C++
As we know that a pointer is a variable whose value is the address of another variable. To demonstrate it,consider the following example.
Source code
Labels:
C,
C++,
C++ coding,
C++ programming,
C++ programs,
Coding,
CPP,
khgamujtaba,
Pointers,
Programs
Sunday, 20 September 2015
Friday, 11 September 2015
Object Oriented Programming in C++ by Robert Lafore
Introduction
Object Oriented Programming in C++
Object-Oriented Programming in C++ begins with the basic principles of the C++ programming language and systematically introduces increasingly advanced topics while illustrating the OOP methodology. While the structure of this book is similar to that of the previous edition, each chapter reflects the latest ANSI C++ standard and the examples have been thoroughly revised to reflect current practices and standards.
Author:
Robert Lafore
Date of publishing:
December 29, 2001
Publisher:
Sams Publishing
Language:
English
Genre:
Programming
ISBN:
- ISBN-10: 0672323087
- ISBN-13: 978-0672323089
Rating:
4.5/5 stars
Votes:
54
Reviewer:
David Horn
Review date:
January 30, 2004
Reviewer rating:
5/5 stars
Download link:
Thursday, 3 September 2015
Concept of pointers in C++
Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need.
Concept of pointers
Pointers are aptly named: they "point" to locations in memory. Think of a row of safety deposit boxes of various sizes at a local bank. Each safety deposit box will have a number associated with it so that the teller can quickly look it up. These numbers are like the memory addresses of variables. A pointer in the world of safety deposit boxes would simply be anything that stored the number of another safety deposit box. Perhaps you have a rich uncle who stored valuables in his safety deposit box, but decided to put the real location in another, smaller, safety deposit box that only stored a card with the number of the large box with the real jewelry. The safety deposit box with the card would be storing the location of another box; it would be equivalent to a pointer. In the computer, pointers are just variables that store memory addresses, usually the addresses of other variables.
Declaration of pointers
The general form of a pointer variable declaration is:
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication.
Reference
The reference of this blog is taken from
Thursday, 27 August 2015
Saturday, 22 August 2015
Matrix multiplication in C++
You have often learnt the method of matrix multiplication in Mathematics.
Matrix multiplication
Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.
Source code
#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();
void multiplication();
int mat1 [3][3], mat2[3][3],mat3[3][3], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 3*3 matrix multiplication."<<endl<<endl;
cout<<"Press 2 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
multiplication();
break;
case 2:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}
goto x;
}
}
void multiplication()
{
cout<<"\nEnter values for first 3 x 3 matrix:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for (j = 0 ; j <= 2 ; j++ )
cin>>mat1 [i][j];
}
cout<<"\n Enter values for second 3 x 3 matrix:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cin>>mat2[i][j];
}
cout<<"\n The first 3 x 3 matrix entered by you is:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<< mat1[i][j]<<"\t";
cout<<endl<<endl;
}
cout<<"\n the second 3 x 3 matrix entered :\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<< mat2[i][j]<<"\t" ;
cout<<endl<<endl;
}
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
{
sum = 0;
for ( k = 0 ; k <=2 ; k++ )
sum = sum + mat1 [i][k] * mat2[k][j];
mat3[i][j] = sum ;
}
}
cout<<"\nThe product of the above two matrices is:\n";
for ( i = 0 ;i<= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
Output
Labels:
Arrays,
C,
C++,
C++ coding,
C++ programming,
C++ programs,
Coding,
CPP,
khgamujtaba,
programming,
Programs
Monday, 17 August 2015
Matrix addition and subtraction in C++
You are well aware of the matrix addition and subtraction in Mathematics.
Matrix addition
Matrix subtraction
Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.
Source code
#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();
int mat1[2][2], mat2[2][2],mat3[2][2], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 2*2 matrix addition."<<endl<<endl;
cout<<"Press 2 for 2*2 matrix subtraction."<<endl<<endl;
cout<<"Press 3 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}
goto x;
}
}
void addition()
{
cout<<"\nEnter values for first 2 x 2 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for (j = 0 ; j <= 1 ; j++ )
cin>>mat1 [i][j];
}
cout<<"\n Enter values for second 2 x 2 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j=0 ; j<=1 ; j++ )
cin>>mat2[i][j];
}
cout<<"\n The first 2 x 2 matrix entered by you is:\n";
for ( i=0 ; i<=1 ; i++ )
{
for ( j=0 ; j<=1 ; j++ )
cout<< mat1[i][j]<<"\t";
cout<<endl<<endl;
}
cout<<"\n the second 2 x 2 matrix entered :\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cout<< mat2[i][j]<<"\t";
cout<<endl<<endl;
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
cout<<"The addition of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
void subtraction()
{
cout<<"\nEnter values for first 2 x 2 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for (j = 0 ; j <= 1 ; j++ )
cin>>mat1 [i][j];
}
cout<<"\n Enter values for second 3 x 3 matrix:\n";
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cin>>mat2[i][j];
}
cout<<"\n The first 2 x 2 matrix entered by you is:\n"<<endl;
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cout<< mat1[i][j]<<"\t";
cout<<endl<<endl;
}
cout<<"\n the second 2 x 2 matrix entered :\n"<<endl;
for ( i = 0 ; i <= 1 ; i++ )
{
for ( j = 0 ; j <= 1 ; j++ )
cout<< mat2[i][j]<<"\t";
cout<<endl<<endl;
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
cout<<"The subtraction of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
Output
Saturday, 15 August 2015
Initialization of 2-d arrays in C++
Initializing 2-dimensional array
The 2-D arrays which are declared by the keyword int and able to store integer values are called two dimensional integer arrays. The process of assigning values during declaration is called initialization. TheseArrays can be initialized by putting the curly braces around each row separating by a comma also each element of a matrix should be separated by a comma.
Example:
int mat[3][3]={ {2,3},{4,5},{6,7} }
Initializing 2-d character array
The 2-D arrays which are declared by the keyword char and able to store characters are called two dimensional character arrays. 2-D character array can be initialized by putting the curly braces around each row as well as apostrophe around every alphabet.
Example:
char character[3][3]={'b', 'a' ,'t' ,'f', 'a' ,'t' ,'m' ,'a' ,'t'};
reference
The reference of this article is taken from
In my next blog inshAllah,I will post an important example of 2-d arrays that will be very helpful in understanding this concept.So,stay tuned.
Labels:
Arrays,
C,
C++,
C++ coding,
C++ programming,
C++ programs,
Coding,
CPP,
Programs
Thursday, 13 August 2015
Subscribe to:
Posts (Atom)