Posts

Showing posts from November, 2022

Basics of rand() in C++ & its Application.

Image
 Basics of rand() The rand() function in C++ generated a random value in the 'int' acceptable values. This can be used in multiple cases that helps optimize our code for a better result . Let us start by learning the basic syntax and how we can manipulate the rand() function to generate a value which we would prefer to see more often. #include<bits/stdc++.h> using namespace std ; int  main () {     //syntax of rand ()  function         cout<<rand () <<endl ;              //max value of rand ()  function         cout<<RAND_MAX<<endl ;              //trends  with   'int'  data  type         cout<<INT_MAX<<endl ;              //controlling rand ()  values              //between  0-9   ( inclusive )         cout<<rand () % 10 <<endl ;                  //between  0-99   ( inclusive )         cout<<rand () % 100 <<endl ;              //generate at max  100  values     //each value being at max  10000     cout&l

Basics of Exception Handling in C++

Image
 Exception Handling in C++ In this article we will look at the basic syntax of exception handling with the code for the execution of handling exceptions by starting off with a simple example.  We will also look into a simple project idea. Exception handling in a banking management system . Division by zero is indetermined , so a basic division code with an input of denominator being 0 would say core dumped . This is because this is an exception . #include<bits/stdc++.h> using namespace std ; //function  for  handling exceptions float  division ( int  a ,   int  b ) {          //denominator can  not  be  0      if ( b== 0 )              // if  denominator  is   0         //then ,  exit  with  this statement         throw  "Division by 0" ;              // if  the conditions are met properly     //then  return  the value      return  a/b ;      } int  main () {      int  a , b , res ;     cin>>a>>b ;          // "try"  to do the following under the  

Basics of String in C++

Image
String in C++ Basic Implementation & Syntax The below code leads to the basic syntax in creating and accepting user input for a string. Note that we are using the 'getline()' function and not the conventional 'cin' to accept user input. The reason for this is that 'cin' will accept the string up till it encounters a 'space' after which the string will be terminated to the input . The getline() function however accepts the entire line as an input, including the space. #include<bits/stdc++.h> using namespace std ; int  main () {     //declaring a string variable     string sample ;          //accepting a string     getline ( cin , sample );          //storing charecters  in  vector     vector<char> partofsample ;          //vectors basic concepts     //Explained  in  another shorts      for ( int  i= 0 ; i<sample.length (); i++ )      {         partofsample.push_back ( sample [ i ]);      }          //printing  and  checking     cout<

Basics of Vectors in C++

Image
BASIC SYNTAX IN C++ This blog contains the code to the basic syntaxes of vectors & STL in C++. The first code (copy-paste & execute for yourself) deals with accepting integer type input and adding it to the vector. Usually vectors work almost exactly like arrays. Syntax used below -      vector<int>     push_back()     size() #include<bits/stdc++.h> using namespace std ; int  main () {     //declaring the vector of  type  integer     vector< int > sample ;          //base condition to accept values     cout<< "Enter the elements" <<endl ;          // we check  if  entered value  is  integer      //  type   or   not      int  n ;      while ( cin>>n )      {                  //push_back adds this value to the vector         sample.push_back ( n );               }     cout<<endl ;          cout<< "The elements you entered were-" <<endl ;     //accessing each element of the vector      for ( int  i= 0 ; i<