Ayush Gupta

Ayush Gupta

433 Articles Published

Articles by Ayush Gupta

Page 23 of 44

Draw a line in C++ graphics

Ayush Gupta
Ayush Gupta
Updated on 23-Mar-2020 7K+ Views

In this tutorial, we will be discussing a program to draw a line in C++ graphics.To implement different shapes and sizes, animations, graphics.h library is used in C++.Example#include int main(){    int gd = DETECT, gm;    initgraph(&gd, &gm, "");    line(150, 150, 450, 150);    line(150, 200, 450, 200);    line(150, 250, 450, 250);    getch();    closegraph();    return 0; }Output

Read More

Does C++ compiler create default constructor when we write our own?

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

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 Live Demo#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 = ...

Read More

Delete and free() in C++ Program

Ayush Gupta
Ayush Gupta
Updated on 23-Mar-2020 194 Views

In this tutorial, we will be discussing a program to understand delete() and free() functions in C++.Both of these functions are primarily used for the same purpose i.e freeing up unused memory. The delete() operator is for the ones allocated using new() and free() for the ones allocated using malloc().Example#include #include int main(){    int x;    int *ptr1 = &x;    int *ptr2 = (int *)malloc(sizeof(int));    int *ptr3 = new int;    int *ptr4 = NULL;    //incorrect usage of delete    delete ptr1;    delete ptr2;    //correct usage of delete    delete ptr3;    delete ptr4;    getchar();    return 0; }

Read More

Different methods to reverse a string in C/C++

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

In this tutorial, we will be discussing a program to understand different methods to reverse a string in C/C++.ExampleUser-defined reverse() function − Live Demo#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 More

Customizing termination behavior for uncaught exception In C++

Ayush Gupta
Ayush Gupta
Updated on 23-Mar-2020 383 Views

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 Live Demo#include #include using namespace std; //defining custom terminator void myhandler(){    cout

Read More

Creating a C/C++ Code Formatting tool with help of Clang tools

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

In this tutorial, we will be discussing a program to create a C/C++ code formatting tool with the help of clang tools.SETUPsudo apt install python sudo apt install clang-format-3.5Then we will create a python file in location where the current user has read and write permissions.Exampleimport os cpp_extensions = (".cxx", ".cpp", ".c", ".hxx", ".hh", ".cc", ".hpp") for root, dirs, files in os.walk(os.getcwd()):    for file in files:       if file.endswith(cpp_extensions):          os.system("clang-format-3.5 -i -style=file " + root + "/" + file)Create a file formatting file in the top directory of the current user.Outputclang-format-3.5 -style=google -dump-config ...

Read More

Counts of distinct consecutive sub-string of length two using C++ STL

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

In this tutorial, we will be discussing a program to count distinct consecutive sub-string of length two using C++ STL.For this we will provided with a string. Our task is to count and print all the unique substrings of length two from the given string.Example Live Demo#include using namespace std; void calc_distinct(string str){    map dPairs;    for (int i=0; i

Read More

Counting Inversions using Set in C++ STL

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

In this tutorial, we will be discussing a program to count inversions using set in C++ STL.Inversion count is a measure of how near the array is to be completely sorted. If the array is already sorted, the inversion count will be 0.Example Live Demo#include using namespace std; //returning inversion count int get_Icount(int arr[],int n){    multiset set1;    set1.insert(arr[0]);    int invcount = 0; //initializing result    multiset::iterator itset1;    for (int i=1; i

Read More

Count the number of 1&rsquo;s and 0&rsquo;s in a binary array using STL in C++

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

In this tutorial, we will be discussing a program to count the number of 1’s and 0’s in a binary array using STL in C++.For this we will be provided with an array. Our task is to count the number of 0’s and 1’s present in the array.Example Live Demo#include using namespace std; // checking if element is 1 or not bool isOne(int i){    if (i == 1)       return true;    else       return false; } int main(){    int a[] = { 1, 0, 0, 1, 0, 0, 1 };    int n = sizeof(a) / sizeof(a[0]);    int count_of_one = count_if(a, a + n, isOne);    cout

Read More

Count smaller elements on right side using Set in C++ STL

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

In this tutorial, we will be discussing a program to count smaller elements on right side using set in C++ STL.For this we will be provided with an array. Our task is to construct a new array and add the number of smaller elements on the right side of the current element at its position.Example Live Demo#include using namespace std; void count_Rsmall(int A[], int len){    set s;    int countSmaller[len];    for (int i = len - 1; i >= 0; i--) {       s.insert(A[i]);       auto it = s.lower_bound(A[i]);       countSmaller[i] = ...

Read More
Showing 221–230 of 433 articles
« Prev 1 21 22 23 24 25 44 Next »
Advertisements