Saturday, 2 September 2017

What is bits/stdc++.h in C++?(master file in C++)

<bits/stdc++.h> is a master file in c++. It is basically a header file which includes every standard library file. you need this file when you don't know which standard file is included in the program. It saves our time and solves the confusion. Let take an example if you want to find square root of a number, you have to include <math.h> file but if you use  <bits/stdc++.h> you don't need any other file.

#include<bits/stdc++.h>
using namespace std;
int main()
{
   float a;
   cout<<"Enter the number:";
   cin>>a;
   cout<<"Square root of the number is:"<<sqrt(a)<<endl;
   return 0;
}

output:-

Enter the number:3
Square root of the number is:1.73205

NOTE: <bits/stdc++.h> is use instead of <iostream> in C++.

ADVANTAGES:-


  1. In the competitive contest, using this file is a good idea, when you want to reduce the time wasted in doing chores, especially when your rank is time sensitive.
  2. It reduces typing in the program and solves the confusion.
  3. you don't have to remember all the  STL of GNU C++ for every function you use.
DISADVANTAGES:-


  1. It is not a standard header file of GNU C++ library. If you use other compilers it might fail. Like there is no such file in MICROSOFT VISUAL STUDIO.
  2. Using it would increase lots of unnecessary stuff and increases compile time.
  3. This header file is not a standard header file of C++ so, non-portable and should be avoided.

Tuesday, 29 August 2017

Topics in C++

Arithmetic Operations in C++(Addition,subtraction,multiplication,division and modulus) With The Help Of Global,Local And Default Variables.

Let understand arithmetic operations with the help of global and local variables.
The arithmetic operation is used for some computation or calculation.
Addition of two variables:-
#include<iostream>
using namespace std;
int x=6; //global variable

int main()
{
   int y=60,z; /* y is declared and initialized,y and z                 are local variable */
 
   z=x+y;
   cout<<"Addition:"<<z;
   return 0;
}


output:-
Addition:66
here x is the global variable(we can use the global variable outside the main function because it is available globally) y and z are local variables(we can use these variables inside the function in which they are declared). y is declared and initialized in the main function but you can give value to y  at the runtime by using cin.

Subtraction of two variables:-

#include<iostream>
using namespace std;
int main()
{
 int x,y,z;
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=x-y;
 cout<<"Subtraction:"<<z;
 return 0;
}
output:-

Enter the value of x:53
Enter the value of y:30
subtraction:23
Multiplication of variables:-

#include<iostream>
using namespace std;
int main()
{
 float2 w=0,x,y,z; //default value for w
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=w*x*y;
 cout<<"multiplication:"<<z;
 return 0;
}


output:

Enter the value of x:2
Enter the value of y:3
multiplication:0


here we have a default value to w that means if we forget to give a value to w it will automatically provide a value to w.
Take an example: if we assign a value to w on runtime say w=2, then answer will be 2*2*3=12 not zero.
do it yourself give the default value of w is zero then assign a value to w by using cin then see what will be the answer and also you can assign a value for w after default value through cin then latest value of w will be include in multiplication.

Division of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x/y;
 cout<<"Quotient:"<<z;
 return 0;
}

output:-
Enter the value of x:67
Enter the value of y:9
Quotient:7.44444
Modulus of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x%y;
 cout<<"Remainder:"<<z;
 return 0;
}

output:-
Enter the value of x:45
Enter the value of y:6
Remainder:3
when you divide two variables it gives you quotient where modulus give you the remainder.
if you use int variables is only provide either quotient or remainder, if you use float it will give you perfect value.
Example: divide 7 by 2 if you take 7 and 2 as the integer the quotient is 3 and modulus is 1 where if you take 7 and 2 as float it will give you 3.5 as answer.

                             HAPPY CODING

Wednesday, 23 August 2017

Data Types in C++

while doing programming in any programming language, you need some specific words to tell the compiler. Here I will explain these words.

TOKENS:-it is the smallest unit in a program, these are of five types.


  1. Keywords:-These are the words that convey a special meaning to the language compiler like default, case, break etc.
  2. Identifiers:-It is the name given by the user for a unit of the program.Identifiers can contain letters and digits.example are MYFILE,_DS,z2t0z9.
  3. Literals:-These are data items that never change their value during a program run (often referred to as constants).examples integer-constant,character-constant,floating-constant,string-literal.
  4. Punctuators:-It enhance a program's readability and give proper meaning to statements, expressions etc as per syntax. examples are {},[ ].
  5. Operators:-These are tokens that trigger some computation or action when applied to the variable and other objects in an expression.examples are arithmetic operators, logical operators, relational operators, conditional operators.i will explain it further.
DATA TYPES:-Data types are means to identify the type of data and associated operations for handling it.C++ data types are of two types:-

  1. Fundamental data types: These are those that are not composed of other data types.These are six fundamental data types in C++.
  • Int Data type: Int stores integer i.e. 34,-678 etc.They have no fractional parts.
  • Char Data type: Char stores character.character can store character variable.examples are &,b,% and 3.
  • Float Data type: Float stores floating-point numbers.it stores number having integer plus fractional part.example are  8.98888,7.89.
  • Double Data type: Double stores twice as int and float.
  • Void Data type: The void type specifies an empty set of values.It is used as the return type for functions that do not return a value.
  • Bool Data type: Bool stores Boolean algebra.It stores either true or false.

DATA TYPES MODIFIERS:-Expect type void, the basic data types can be modified by using modifiers such as


  • signed
  • unsigned 
  • long 
  • short
     2.Derived Data Type: These are derived from the fundamental data type.


  • Array: Arrays refer to a named list of a finite number n of similar data elements.Examples are ary[1],ary[2].......ary[n].
  • Function: A function is a named part of a program that can invoke from other parts of the program as often needed.
  • Pointer: A pointer is a variable that holds a memory address(type *ptr).
  • Reference: A reference is an alternative name of an object.The general declaring of reference variable is :type &ref-var=var-name;
  • Constant: The keyword const can be added to the declaration of an object to make that object a constant rather than a variable.The general declaration of constant is -const type name=value;




  • User-Defined Derived Data Types:-There are some derived data types that are defined by the user.
  1. Class:- A class represent a group of similar objects.
  2. Structure: A structure is a collection of variables referenced under one name, providing convenient means of keeping related information together.
  3. Union: A union is a memory location that is shared by two or more different variables, generally of different types at different times.
  4. Enumeration:- An alternative method for naming integer constants is often more convenient than const.
more information will be provided on these topics as we go deeper in C++.



Tuesday, 22 August 2017

What is namespace in C++?

when you use the namespace, your program looks like:-

#include<iostream>
using namespace std;
int main()
{
 cout<<"Hello World";
 return 0;
}
when you don't use the namespace, your program looks like:-

#include<iostream>
int main()
{
 std::cout<<"Hello World";
 return 0;
}
without "using namespace std;" when you write for example "cout<<;", you'd have to put "std::cout<<;",
Another role of the namespace is that it solves name conflicts among files and allow them to use with the same name.Let take an example,
you have a file let say main, and you want to include two files file_one and file_two in main but file_one and file_two both have the same function display when you include these file in the main file and call these file having the same function name it produces a conflict and shows an error.
The error will be the redefinition of function this is because we are not declaring display inside any declarative region such as namespace.C++ think you are redefining the function.To solve this conflicts we use the namespace.we use namespace then file name then we define the function in both file then we use this function in the main file in INT MAIN() { file_one::function}. so namespace avoids collision among same name file and makes them executable.




Thursday, 17 August 2017

Basic Syntax Of C++(what is main,#include,iostream,namespace and comments in c++)

program to print a string on the screen(DevC++)

The above program is one of the simplest programs that can be written in C++, but it does include basic elements that every C++ programs have. Let us have a  look at these elements one by one:

1. COMMENTS IN C++ PROGRAM 
Comments are pieces of code that the compiler simply does not execute.Comments are written for the explanation of the steps or program.There are two ways to insert comments in C++ programs.

(i) Single line comments with / /:-The comments that begin with //are single line comments.The compiler simply ignores everything following in that same line.In above picture line 1 represents the single line comment, whatever written after // will be discarded.
(ii)multi-line comments with /*.......*/:-The comments begin with /* and end with */.That means, everything that falls between /* and */will be discarded.In above picture line, 8 and 9 represent multi-line comments.

2. #INCLUDE
The statements that begin with the #(hash) sign are directives for the preprocessor(a computer program that modifies data to conform to the input requirements of another program).That means these statements are processed before compilation takes place.INCLUDE statement tells compiler's preprocessor to include the header file in the program.

3. INT MAIN (){.....}
The line indicates the beginning of the main function.Whatever written between the curly brackets {}
will be executed.It is essential to have the main function.INT is data type will be explained further.

4. IOSTREAM(header file)
The header file iostream is included in every C++ program to implement input/output facilities, without this file we cannot input or take the output from the program.

5. COUT(pronounced "see-out")
Cout stands for console output.It helps in printing the values on the screen.

6. CIN(pronounced "see-in")
Cin stands console input.It takes input from input devices.

7. INSERTION OPERATOR("<<")
Insertion is performed by insertion operator"<<".

8.EXTRACTION OPERATOR(">>")
Extraction is performed by the extraction operator">>".

9. SEMICOLON(;)
The semicolon (;) character used to finish every executable statement of a C++ program and it must be included after every executable instruction.

10. RETURN 0
The return instruction makes the main() to finish and it returns a value, in this case, it is returning 0(zero).Returning 0 is the most usual way of telling that program has terminated normally.

                      click here to know what is namespace  

Tuesday, 18 July 2017

How to install DevC++ and use it

Install DevC++ on Windows Operating System


Hello Folks,
Today I am going to share the steps how to download and install DevC++ in your Windows OS and also share how to use it and compile the program on it.

STEP BY STEP PROCEDURE TO INSTALL DEVC++ 

  1. As we know DevC++ IDE is a free portable, fast and simple C/C++ IDE.you can download it from here (open source) DOWNLOAD
  2. After opening the tab now, click on download (which will appear in the green box).
  3. After downloading the setup file double click on the Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup.exe(approx 50MB).
  4. Then press Agree Button.
  5. Press Next Button.
  6. Then comes the next windows which give you the option to choose to install location. If you want to change the location then go to Browse the option and select the desired Destination folder. And Press the Install Button (Note the Destination Folder is the folder where all your Dev-C++ installation file and setup stores).
  7.  It will take some time to Install
    .
  8.  After Installing all the files it will ask the option to choose for whom you want to install the Dev-C++. I mean for all Users or for some specific one.
  9. Now you successfully installed the Dev-C++ IDE. If you want to Run then just click on Finish or Untick the check box option then Finish.
  10. Now you can Configure the software according to your convenient.
    Now Press Next->Next->OK to successfully open the IDE.

  1. Now click on DevC++ icon appears on the desktop.a tab appears on the window.
  2. click on file(in the top most left corner).then File->new->source file or press ctrl+N.

Sunday, 16 July 2017

Introduction To C++

The  C++(pronounced c plus plus) is a middle-level programming language was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup.C++ is a combination of  Simula 67(which is an object-oriented programming language )  and C.In early stages it is known as "C with classes".
The name C++ was coined by Rick Mascitti where "++" is the C increment operator.The maturation of the C++ language was attested to by two events:

  • The formation of an ANSI (American National Standard Institute) C++ committee and
  • The publication of the annotated C++ reference manual by Ellis and Stroustrup.
The latest C++ standards document was issued by ANSI/ISO in the year 2011 namely C++11 or formally C++0x.
The major reason behind the success and popularity of C++ is that it supports the object-oriented technology, the latest in the software development and the most near to the real world.C++ runs on the variety of platforms, such as Windows, Mac Os and the various versions of UNIX.