Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 24 of 44

Count smaller elements in sorted array in C++

Ayush Gupta
Ayush Gupta
Updated on 16-Mar-2020 325 Views

In this tutorial, we will be discussing a program to count smaller elements in sorted array in C++.In this we will be given a number and our task is to count all the elements present in the sorted array which are smaller than the given number.Example Live Demo#include using namespace std; int countSmaller(int arr[], int n, int x){    return upper_bound(arr, arr+n, x) - arr; } int main(){    int arr[] = { 10, 20, 30, 40, 50 };    int n = sizeof(arr)/sizeof(arr[0]);    cout

Read More

Core Dump (Segmentation fault) in C/C++

Ayush Gupta
Ayush Gupta
Updated on 16-Mar-2020 5K+ Views

In this tutorial, we will be discussing a program to understand core dump (segmentation fault) in C/C++.It happens due to reasons like when code tries to write on read only memory or tries to access corrupt memory location.ExampleModifying a string literalint main(){    char *str;    str = "GfG";    *(str+1) = 'n';    return 0; }Accessing out of array index bounds#include using namespace std; int main(){    int arr[2];    arr[3] = 10;    return 0; }Accessing an address which is freed#include #include int main(void){    int* p = malloc(8);    *p = 100;    free(p);    *p = 110;    return 0; }OutputAbnormal termination of program

Read More

Converting Strings to Numbers in C/C++

Ayush Gupta
Ayush Gupta
Updated on 16-Mar-2020 323 Views

In this tutorial, we will be discussing a program to understand how to convert strings into numbers in C/C++.C/C++ provides two ways to convert strings into numbers.Example Live DemoUsing sscanf()#include int main(){    const char *str = "12345";    int x;    sscanf(str, "%d", &x);    printf("The value of x : %d", x);    return 0; }OutputThe value of x : 12345Using stoi() Live Demo#include #include using namespace std; int main(){    string str1 = "45";    string str2 = "3.14159";    string str3 = "31337 geek";    int myint1 = stoi(str1);    int myint2 = stoi(str2);    int myint3 = stoi(str3);    cout

Read More

Convert a String to Integer Array in C/C++

Ayush Gupta
Ayush Gupta
Updated on 16-Mar-2020 3K+ Views

In this tutorial, we will be discussing a program to understand how to convert a string into integer array in C/C++.For this we will create a new array. Traverse through the given string, if the character is a comma “, ”, we move on to the next character else add it to the new array.Example Live Demo#include using namespace std; //converting string to integer array void convert_array(string str){    int str_length = str.length();    int arr[str_length] = { 0 };    int j = 0, i, sum = 0;    //traversing the string    for (i = 0; str[i] != ...

Read More

Virtual destruction using shared_ptr in C++

Ayush Gupta
Ayush Gupta
Updated on 12-Mar-2020 219 Views

In this tutorial, we will be discussing a program to understand virtual destruction using shared_ptr in C++.To delete the instances of a class, we define the destructor of the base class to be virtual. So it deletes the various object instances inherited in the reverse order in which they were created.Example Live Demo#include #include using namespace std; class Base {    public:    Base(){       cout

Read More

Using class to implement Vector Quantities in C++

Ayush Gupta
Ayush Gupta
Updated on 12-Mar-2020 231 Views

In this tutorial, we will be discussing a program to understand how to use class to implement vector quantities in C++.Vector quantities are the ones which have both magnitude and direction. Here we will be implementing them using classes and then performing basic operations on them.Example Live Demo#include #include using namespace std; class Vector {    private:    int x, y, z;    //components of the Vector    public:    Vector(int x, int y, int z){       this->x = x;       this->y = y;       this->z = z;    }    Vector operator+(Vector ...

Read More

Trivial classes in C++

Ayush Gupta
Ayush Gupta
Updated on 12-Mar-2020 549 Views

In this tutorial, we will be discussing a program to understand trivial classes in C++.When a class/ struct contains explicitly defaulted value inside it, then it is known as Trivial classes. Further trivial classes have their own constructor, assignment operator and destructor.Example//using the default constructor struct Trivial {    int i;    private:    int j; }; //defining your own constructor //and then marking it as default struct Trivial2 {    int i;    Trivial2(int a, int b){       i = a;    }    Trivial2() = default; };Output(No output as we are just defining classes here and not creating object instances from them.)

Read More

transform_inclusive_scan() function in C++

Ayush Gupta
Ayush Gupta
Updated on 12-Mar-2020 97 Views

In this tutorial, we will be discussing a program to understand the transform_inclusive_scan() function in C++.Example Live Demo#include #include using namespace std; namespace point_input_iterator {    template    OutputItrator transform_inclusive_scan(InputItrator first,       InputItrator last,       OutputItrator d_first,       BinaryOperation binary_op,       UnaryOperation unary_op){             *d_first = unary_op(*first);       first++;       d_first++;       for (auto it = first; it != last; it++) {          //calculating the prefix sum          *d_first = binary_op(unary_op(*it), *(d_first ...

Read More

Containership in C++

Ayush Gupta
Ayush Gupta
Updated on 12-Mar-2020 2K+ Views

In this tutorial, we will be discussing a program to understand containership in C++.The parameter if a certain class contains another class is called as containership. The inside class is called contained class, while the class in which it is present is called container class.Example Live Demo#include using namespace std; class first {    public:    first(){       cout

Read More

Templates and Static variables in C++

Ayush Gupta
Ayush Gupta
Updated on 02-Mar-2020 665 Views

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 Live Demo#include using namespace std; template void fun(const T& x){    static int i = 10;    cout

Read More
Showing 231–240 of 433 articles
« Prev 1 22 23 24 25 26 44 Next »
Advertisements