C++ Articles - Page 420 of 719

Sort the Matrix Diagonally in C++

Arnab Chakraborty
Updated on 29-Apr-2020 11:06:15

371 Views

Suppose we have N x M matrix, we have to sort this diagonally in increasing order from top-left to the bottom right. So if the matrix is like −331122121112The output matrix will be −111112221233To solve this, we will follow these steps −Define a method called solve(), this will take si, sj and matrix matn := number of rows and m := number of columnsmake an array called tempi:= si and j := sj, and index := 0while i < n and j < minsert m[i, j] into temp, then increase i and j by 1sort temp arrayset index := 0, ... Read More

Break a Palindrome in C++

Ravi Ranjan
Updated on 17-Jun-2025 17:57:32

2K+ Views

In this article, our task is to break a palindromic string. We have to replace exactly one character with any lowercase English letter such that the string becomes a lexicographically smallest possible string that isn't a palindrome. Now after doing so, we have to find the final string. If there is no way to do so, then return the empty string. Example Here is an example of breaking the given palindrome string by replacing one letter and the output is a non-palindromic lexicographically smallest string: Input: abccba Output: aaccba In the above ... Read More

Delete Leaves With a Given Value in C++

Arnab Chakraborty
Updated on 29-Apr-2020 10:24:59

163 Views

Suppose we have a binary tree and an integer target, we have to delete all the leaf nodes with value target. We have to keep in mind that once we delete a leaf node with a value target if it's parent node becomes a leaf node and has the value target, it should also be deleted (we need to continue doing that until we can't). So if the tree is like below, and the target is 2, then the final tree will be like the last one −To solve this, we will follow these steps −Define a recursive method called ... Read More

Delete elements in C++ STL list

Ayush Gupta
Updated on 23-Mar-2020 08:48:31

242 Views

IIn this tutorial, we will be discussing a program to understand how to delete elements in the C++ STL list.For this, we will be using the pop_back() and pop_front() function to delete the element from last and the front respectively.Example Live Demo#include #include using namespace std; int main(){    listlist1={10,15,20,25,30,35};    cout

Initialization of a multidimensional arrays in C/C++ Program

Ayush Gupta
Updated on 23-Mar-2020 08:39:05

201 Views

In this tutorial, we will be discussing a program to understand how to initiate a multidimensional array in C/C++.While declaring a multidimensional array, the value of the leftmost dimension can be left empty, but all other dimensions must be provided.Example Live Demo#include int main(){    int a[][2] = {{1,2},{3,4}};    printf("%lu", sizeof(a));    getchar();    return 0; }Output16

Extended Integral Types (Choosing the correct integer size in C/C++)

Ayush Gupta
Updated on 23-Mar-2020 08:37:36

126 Views

In this tutorial, we will be discussing a program to understand extended integral types in C/C++.The data types in C are very loosely defined. Their range values changes on the basis of the compiler being 32 or 64 bit. To specify the compiler range you want to use in your program we use intN_t.Example Live Demo#include using namespace std; int main(){    uint8_t i; //mentioning the bit to be 8    i = 0;    cout

File Handling through C++ Classes

Ayush Gupta
Updated on 11-Dec-2024 19:20:31

1K+ Views

In this tutorial, we will be discussing a program to understand File Handling through C++ classes. What's File Handling? File handling is an important concept in programming, which allows a program to read from and write to files. C++ has built-in classes and functions for file handling, which is done using file stream classes that are part of the  library. The most common file stream classes in C++ are ofstream, ifstream, fstream. ofstream: This class is used for output file streams, where it allows the user to write data to files. ... Read More

Draw a line in C++ graphics

Ayush Gupta
Updated on 23-Mar-2020 08:30:09

6K+ 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

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

Ayush Gupta
Updated on 23-Mar-2020 08:27:02

214 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
Updated on 23-Mar-2020 08:24:21

159 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; }

Advertisements