Wednesday, 18 November 2015

'Character' to 'integer' datatype conversion



A very simple way to convert the character format into the integeral format is the subtraction of the ASCII value of character '0' from the ASCII value of the cosidered character.

for example, lets see a code

char ch;
ch='5';
int n;

n=ch-'0';        //character to integer conversion 

Program requiring conversion :

Input : String "f5b6c3s4"

Output: fffffbbbbbbcccssss

program code :

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char ch[10];
cout<<"enter the code\n";
gets(ch);
int n=strlen(ch);
for(int i=0;i<n;i++)
{
char ch1=ch[i];
i++;
int c=ch[i]-'0';          //character to integer conversion
for(int j=0;j<c;j++)
cout<<ch1;
}
getch();
}



Wednesday, 21 October 2015

'dos.h'


The headerfile "dos.h" is used to perform some system functions like :

* delay()
* sleep()
* getdate()
* gettime()
* setdate()
* settime()
* sound()
* nosound()

'delay()' and 'sleep()' :
 these are two similar functions used to make delay in the ouput of the program.

 for example :
 #include<iostream.h>
 #include<conio.h>
 #include<dos.h>
 int main()
{
    cout<<"This c++ program will exit in 10 seconds.\n";      

    delay(10000);                      

    return 0;
}

and same thing can be executed through 'sleep(10)'.

'gettime()' and 'getdate()':

these two functions are used to get the time and date of the system in the program.

for example
 #include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
  clrscr();
  struct date d;
  struct time t;
  getdate(&d);
  gettime(&t);
  int day,month,year;
  int hour,min,sec;
  day=d.da_day;
  month=d.da_mon;
  year=d.da_year;
  hour=t.ti_hour;
  min =t.ti_min;
  sec= t.ti_sec;
  cout<<"The current date is "<<day<<"/"<<month<<"/"<<year<<"\n";
  cout<<"The current time is "<<hour<<":"<<min<<":"<<sec<<"\n";
  system("pause");
  }

output :

  The current date is 22/10/2015
  The current time is 11:24:05
  Press any key to continue.

'setdate()' and 'settime()' :
through these functions we can set the date and time of the system simply using the fuctions:
setdate(&var); and
settime(&var);
note:the members like 'd.da_day' should be integeral always.

'sound()' and 'unsound()':
functions used for adding sound.

example :

#include<dos.h>
void main()
{
for(i=100;i<=1000;i+20)
{
  sound(i);
  delay(50);
}
unsound();
system("pause");
}

Friday, 11 September 2015

Printing Statement Without 'semicolon(;)'












Generally, we use 'cout' to print anything on the screen in c++ language. For example :
To print "Hello Everyone", we use the statement as:

cout<<"Hello Everyone \n";

And we have to terminate the statement with 'semicolon(;)'.

Can we print this Statement without using terminating it with ';' ??

Yesss.. we can do such thing by the following code :

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
   clrscr();
 if(cout<<"Hello Everyone")      //printing without ';'
{

}
system("pause");
}

So, this code will print the line "Hello Everyone" without using the semicolon(;).

Thursday, 20 August 2015

Time and Date in 'C++'












Sometimes,we need to add time and date in our 'c++' programs.
For that,here are two functions which can be used to add these utilities.
|----------------------------------------------------|
|   for adding time  ->   _strtime(variable);   |
|   for adding date  ->   _strdate(variable);    |
|----------------------------------------------------|

The headerfile for these functions is '<time.h>'.

for example :-

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
    clrscr();
    char date[10];
    char time[10];
    _strdate(date);           \\function for fetching 'system date'
    cout<<"The current date is  \n"<<date<<"\n" ;
    _strtime(time);          \\function for fetching 'system time'
    cout<<"The current time is \n"<<time<<"\n";
    system("pause");
}


 Use _strtime_s and _strdate_s instead for security sensitive programs.

Friday, 31 July 2015

Memory allocation for 'Integer Datatypes'

In c++,the integeral datatype is always allocated with '2-bytes' memory for storing the value of the particular dataobject of the integeral datatype.
The reason for this fixed  allocation is just a management of 'total integer range' represented under integral datatype in digital notation.
In C++,the range for integer datatype is '-32768 to +32767'.i.e.,only the number that is between this range can be assigned to any integeral dataobject.Beyond that the range is assigned under 'long int'.
So, the total numbers are 65536.
So, As we know, the allocation of memory for any data is done always in binary format(bit format) i.e.,either 0 or 1.

So,

for any decimal number
------------------------------------------------------------------------------------
 dec = 2b      where b -> required binary digits.
------------------------------------------------------------------------------------           
The binary digit required for '4' decimal number representation = 2 (as 4 = 22 => b=2)
 0   =   00
 1   =   01
 2   =   10
 3   =   11
 ( i.e. 2 bits are required for storing 4 decimal numbers)
         
  The binary digit required for '8' decimal number representation = 3 (as 8 = 23 => b=3)
                                                                                    
    0   =   000
    1   =   001
    2   =   010
    3   =   011
    4   =   100
    5   =   101
    6   =   110
    7   =   111                                                                                
( i.e. 3 bits are required for storing 8 decimal numbers)
  
And likewise,
   
The binary digit required for '65536' decimal number representation = 16 (as 65536 = 216 => b=16)

So, here ,we are required with 16 bits to allocate the memory to the range of '65536' integers.
And hence ‘16 bits’ is equivalent to ‘2-bytes’.

Hence, this is the reason that the allocation of memory for integeral datatype is fixed as ‘2-bytes’.
All other formats of datatypes like float,double,long double,character  are also allocated the memory in same manner. 

Wednesday, 15 July 2015

Arrow Operator

What's Arrow Operator ??


Actually, arrow operator is used to tend down from any higher value to the lower value using the while loop in the programs.
---------------------------------------------------------------------------------------------------------
* Used for tending down any higher value to lower value*
---------------------------------------------------------------------------------------------------------
Lets say, we have the value of 'i=10'. and we want to move towards '0' by printing all these values of the variable 'i'. 
Then, we can simply apply the 'arrow operator(-->)' in the while loop without using any decremental statement like 'i--' in the while loop for the variable 'i'.
So the Simple code for this example would be :

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{    clrscr();

  int i=10;
  cout<<"Tending Down Value of Variable i is \n";
  while(i-->)                 // Arrow Operator ( --> )
{
  cout<<i<<"\n";
}
  system("pause");
}

So, the output of the code will be :

Tending Down Value of Variable 'i' is 
10
 9
 8
 7
 6
 5
 4
 3 
 2
 1
 0

 press any key to continue

--------------------------------------------------------------------------------------------------------
*It reduces the use of decremental statements in the loop* 
--------------------------------------------------------------------------------------------------------

So, By using the 'arrow operator', we can get rid of the decremental statements in the while loop.
Here, this task could also be performed simply using the statements as :

while(i>0)
{
  i--;                  // decremental statement ( i-- )
  cout<<i<<"\n";
}

But , the 'arrow operator' makes this conditional useless.

Friday, 10 July 2015

Screen Pausing in C++

Function 'getch( )' is misused in programs



Generally when we pause our program's output screen by using the function 'getch( )' .
But the actual use of the function 'getch()' is to catch a single character from the keyboard dynamically. 
So,when we use the 'getch( )' function in the end of the program,it actually dosen't pause the program or screen while execution, but it waits for the entry of any type of character for further execution of the program and then it gets terminated.

For example :-                        #include<iostream.h>
 #include<conio.h>
  void main()
 {
   clrscr();
   cout<<"Hello Everyone /n";
   getch();                                    //misused                                              'getch()' function
      }

So, here after printing hello on the screen,the program waits for the entry of the charcater through the getch() function and when we press any key, the job is completed and the program finds no more
statements and hence terminates.
Hence, the program was actually waiting for the response but not pausing the execution.

-------------------------------------------------------------------------------------------------------------
*Use 'System( )' function for pausing the execution* 
-------------------------------------------------------------------------------------------------------------

So,for actual pausing of the execution screen, we should use the 'system()' function.
for example : 
                                              #include<stdlib.h>
#include<iostream.h>
#include<conio.h>
 void main()
 {
   clrscr();
   cout<<"Hello Everyone /n";
   system("pause");     // use of 'system()'                                              function
 }
                                           
Output will be as :

    Hello Everyone
    press any key to continue                                          
                                   



--------------------------------------------------------------------------------------------------------