Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Ayush Gupta
Page 24 of 44
How to quickly swap two arrays of the same size in C++?
In this tutorial, we will be discussing a program to understand how to quickly swap two arrays of same size in C++.For this we will be using a quick method called std::swap() for swapping the elements of the two given arrays.Example#include #include using namespace std; int main (){ int a[] = {1, 2, 3, 4}; int b[] = {5, 6, 7, 8}; int n = sizeof(a)/sizeof(a[0]); swap(a, b); cout
Read Morenegative_binomial_distribution in C++ with Examples
In this tutorial, we will be discussing a program to understand negative_binomial_distribution in C++.This function follows the negative Binomial discrete distribution and produces integers according to this random distribution.Example#include using namespace std; int main() { //setting number of experiments const int exps = 10000; const int numberstars = 100; default_random_engine generator; negative_binomial_distribution distribution(4, 0.5); int p[10] = {}; for (int i = 0; i < exps; ++i) { int counting = distribution(generator); if (counting < 10) ++p[counting]; } cout
Read MoreHow to store Data Triplet in a Vector in C++?
In this tutorial, we will be discussing a program to understand how to store a Data triplet in a vector in C++.To store three elements in a single cell of a vector we will creating a user defined structure and then make a vector from that user defined structure.Example#include using namespace std; struct Test{ int x, y, z; }; int main(){ //creating a vector of user defined structure vector myvec; //inserting values myvec.push_back({2, 31, 102}); myvec.push_back({5, 23, 114}); myvec.push_back({9, 10, 158}); int s = myvec.size(); for (int i=0;i
Read MoreSwapping of subranges from different containers in C++
In this tutorial, we will be discussing a program to understand swapping of subranges of different containers in C++.For this we will be provided with vectors and lists, and we need to swap some of their elements.Example#include #include #include #include using namespace std; int main(){ vector v = { -10, -15, -30, 20, 500 }; list lt = { 10, 50, 30, 100, 50 }; swap_ranges(v.begin(), v.begin() + 3, lt.begin()); for (int n : v) cout
Read MoreMenu Driven C++ Program for a Simple Calculator
In this tutorial, we will be discussing a program to create a menu driven program for a simple calculator.This program will give user the ability to choose among the following mathematical operations − addition, subtraction, multiplication, division, HCF and LCM.Example#include using namespace std; //displaying the menu void menu(){ cout
Read MoreTemplates and Static variables in C++
In this tutorial, we will be discussing a program to understand templates and static variables in C++.In case of function and class templates, each instance of the templates has its own local copy of the variables.Example#include using namespace std; template void fun(const T& x){ static int i = 10; cout
Read MoreMaximum of all Subarrays of size k using set in C++ STL
In this tutorial, we will be discussing a program to get maximum of all subarrays of size k using set in C++ STL.For this we will be provided with a array of size N and integer K. Our task is to get the maximum element in each K elements, add them up and print it out.Example#include using namespace std; //returning sum of maximum elements int maxOfSubarrays(int arr[], int n, int k){ set q; set::reverse_iterator it; //inserting elements for (int i = 0; i < k; i++) { q.insert(pair(arr[i], i)); } ...
Read MoreCustomizing termination behavior for uncaught exception In C++
In this tutorial, we will be discussing a program to customize behavior for an uncaught exceptions in C++.Usually, the exception is handled by the try-catch block, but there are instances where there isn’t a matching catch block and the program just terminates. This terminate() function is modifiable as per user requirements.Example#include #include using namespace std; //defining custom terminator void myhandler(){ cout
Read MoreDifferent methods to reverse a string in C/C++
In this tutorial, we will be discussing a program to understand different methods to reverse a string in C/C++.ExampleUser-defined reverse() function −#include using namespace std; //function to reverse given string void reverse_str(string& str){ int n = str.length(); for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); } int main(){ string str = "tutorialspoint"; reverse_str(str); cout
Read MoreDoes C++ compiler create default constructor when we write our own?
In this tutorial, we will be discussing a program to understand if the C++ compiler creates a default constructor when we write our own.Generally, the C++ compiler uses the default constructor when no one is defined, but always uses the one defined by the user if any.Example#include using namespace std; class myInteger{ private: int value; //other functions in class }; int main(){ myInteger I1; getchar(); return 0; }OutputCompiles successfullyExample#include using namespace std; class myInteger{ private: int value; public: myInteger(int v) //user-defined constructor { value = v; ...
Read More