-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;
Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Monday, 25 April 2016

File Access Methods in C++

I have discussed the concept of "files" in my previous blog.Today I am discussing the different file access method.

File access Method

The way in which a file can be accessed is called file access method.It depends on the manner in which data is stored in the files.Different file access methods are given below:

Sequential Access Method:

                                                                   It is used to access data in the same manner in which it is stored in a file.This method reads and writes data in a sequence.In order to access the last record,it has to to read all the records before the last one.

The record is not fixed in this access method.It means that each record occupies different amount of memory.

One of its advantages is that memory is not wasted.One of its disadvantages is that it takes very long time to search a record.For example,if the user wants to search the 4th record,it has to read the first 3 records to access the 4th record.

Random Access Method:

                                          It is used to access the data directly without accessing the preceding data.This method does not read or write data in a sequence.It is faster than sequential access method.

It stores data in fixed length records.This is the main disadvantage of this access method.It wastes memory space.Suppose a record of 10 bytes is used to store a complete sentence in a file.If a sentence consists of a single letter like 'H', it will occupy 10 bytes to store this letter.

However,this method speeds up access time because the position of each record can be determined easily as each record takes fixed length.Database management systems store files in a random access format.

Saturday, 23 January 2016

Concept of files in C++

Many students are confused with the concept of file handling in C++.So,today I will give you a little introduction about the concept of files in C++ which the essence of file handling.This would be a series of blogs which will help you to understand this concept easily.So,stay tuned !!1

Concept of files

As we see that a program inputs data from the user and stores it in variables.The data stored in the variables is temporary.When the program ends,all data stored in variables is also destroyed. The user has  to input data again from keyboard when the program is executed next time. the data has to be entered each time the program is executed that wastes time. The user may also type wrong data at different times.

Definition of files

A file is a collection of related records.It stores data about an entity(if you are unfamiliar with the concept of entity, remember that an entity is an object about which data is to be gathered.e.g., student is an entity). A data file can be used to provide input to a program.In can also be used to store the output of a program permanently.


Advantages of files

Now we will discuss the advantages of files in C++.

  • Files can store large amount of data permanently.
  • Files can be updated easily.
  • One file can be used by many programs for same input.
  • Files save time and effort to input data via keyboard.

Types of files

C++ provides two types of files.This categorization is based on how data is stored in files. Following are the types of files in C++.

Text files

A type of file that stores data as readable and printable characters in called text file.A source program of C++ is an example of text file.


Binary files

A type of file that stores data as non-readable binary code is called binary file.An object file of a C++ program is an example of binary file.


Reference

This reference of this article is taken from the book,Object Oriented Programming using C++(IT series).

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








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.

Friday, 16 October 2015

Real programmer

Are you a real programmer????
See this image and decide !!!
(This image has been taken from www.sisodia.com)

real programmer

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:


DataType *var;

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

Concept of pointers


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


#include <iostream>

using namespace std;

int main ()

{

   int  var1;

   cout << "Address of var1 variable: ";

   cout << &var1 << endl;

   return 0;

}

Output


Example of pointer
Example of pointer


Reference


To know the reference of this blog,

Friday, 11 September 2015

Object Oriented Programming in C++ by Robert Lafore

Introduction

"OOP in C++" book

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:


                    
                         Click here