Sunday, 1 May 2016

File handling to import and export huge data using Comma separated values


While writing a c program we all deals with the bulk data which may be available on spread sheets like Excel but we can't directly read into the program but we can read a single sheet a workbook with the help of csv format. To do so we have to save the file in csv format from excel. Then read that file in c program using file handling.
The csv file separate the data by commas
but if we have the commas in the data then the field will be enclosed in the double quotes
like "1,2" .
Again we may also require the double quotes also in the file so it can also be written as
"1,2"" this will generate the output as  1,2"
Hope u got the basic concept of a csv file
Rest is the basic file handling

Sunday, 27 March 2016

Tokens in C language

Definition 1: The Tokens are the basic building blocks any programming language. They are the smallest unit of the programming language that cannot be further broken.
There are 6 types of tokens in the C language.
1.       Identifiers
2.       Keywords
3.       Constants
4.       Variables
5.       Operators
6.   Punctuations


Example of the tokens in the C program
void main()
{
printf(“welcome”);
}

In the above example
  •  void is keyword
  •  main is the function name  an identifier.
  • printf() is again a function name 

Tuesday, 13 May 2014

How to find the frequency of processor on WinXP

The header file #include is used in windows platform
Declare the variable freq of type large 
 LARGE_INTEGER freq;

call the function
QueryPerformanceFrequency(&freq);

print the value of freq

This will get you the frequency of you CPU

Code:
#include
#include
#include
int main()
{
  LARGE_INTEGER freq;
  QueryPerformanceFrequency(&freq);
  cout << "The resolution of this timer is: " << freq.QuadPart << " Hz." <  getch();
}

Saturday, 10 May 2014

How to calculate the Execution time of a program

For calculating the time in C language we can use the header file time.h

The time.h header file contains a macro name called CLK_TCK.

It has been made obsolete in some of compiler.

This variable provides the number of clock ticks per second.

The default value in many compiler of this macros is 18.2

Now to record the number clock tick between two events we can use the declaration like

clock_t  start,end;

here start and end are the variables.

start is used to begining tick value

end is used to strore ending tick value.

 that is by calling start= clock();

and end=clock();

now elapsed time in second will be given by the formula

(end -start)/CLK_TCK

The time calculated by this functions will be in seconds.

and the accuracy of this functions is for  1/18.2 seconds and it can't record the events which are completed 
in time less than this.

Saturday, 27 July 2013

Number representation and Range of float data type

In c language the size of float in bytes is four, which is an equal to 32 bits.
These 32 bits are divided in 3 parts:
1.    Sign bit: Single bit to store the sign i.e. 0 will represent positive and 1 will represent negative.
2.    Exponent bits: In float we will be using 8 bits to store the exponents.
3.    Mantissa bits: remaining 23 bits are used to store mantissa

An example of number representation:

Let the number be 5.5 
Now the binary equivalent of the same will be (101.1)2
In the floating point representation we represent the number between 1 and 2.
This is done by representing in scientific notation as
(1.011)2 X 22  
We get this notation similarly we do in math ie 1.011 X 100 which is the equivalent of 4 or 22.
Sign à 0
Exponent à 00000010
Mantissaà 0110000000000000.0000 upto 23 bits
here we have not written 1 before fraction as it is understood .

Range of float

We know that we have exponent of 8 bits so the range comes out to be from 0 to 255 or in sign magnitude it can be written as -127 to 128.
0 and 255 are used to store non normalized data (Data with less accuracy).
Now the range of exponent is from -126 to 127.

The smallest Positive number that can be represented:


0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
±
Exponent bits
Mantissa bits

it comes to be 1.0 X 2-126
1.0 is the mantissa
-126 is the signed decimal representation of exponent bits.
So the smallest number that can be represented will be 2-126  or it is equal to 1.17549E-38.

The largest  Positive number that can be represented:


0
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
±
Exponent bits
Mantissa bits

It comes to be (2.0 – 2-23 )X 2127
Mantissa can be considered as 2, as 2-23 is very small in magnitude.
127 is the signed decimal representation of exponent bits.
So the smallest number that can be represented will be 2 X 2127 or it is equal to 3.40282E+38.
Range of Float comes out to be ±3.40282E+38