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

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