C++ String Class and Its Applications

Arnab Chakraborty
Updated on 31-Jul-2019 12:48:26

129 Views

The C++ has the String class. That is different than the traditional C strings. The C string is actually the character array. In C++, the string class has few different properties. It has different functions, that can be used to perform different tasks. Here we will see the important features of the String class.In the first section we will see how the constructors of the string class works in different way. Let us see by example.Example#include using namespace std; int main() {    string str("This is a string");    cout

C++ Program to Print Matrix in Z Form

Arnab Chakraborty
Updated on 31-Jul-2019 12:43:25

254 Views

Here we will see how to print the matrix elements in Z form. So if the array is like below −5 8 7 1 2 3 6 4 1 7 8 9 4 8 1 5Then it will be printed like: 5, 8, 7, 1, 6, 7, 4, 8, 1, 5AlgorithmprintMatrixZ(mat)Begin    print the first row    i := 1, j := n-2    while i < n and j >= 0, do       print mat[i, j]       i := i + 1, j := j - 1    done    print the last row EndExample#include #define MAX 4 using namespace std; void printMatrixZ(int mat[][MAX], int n){    for(int i = 0; i

C++ Program for Zeckendorf's Theorem

Arnab Chakraborty
Updated on 31-Jul-2019 12:40:35

302 Views

Here we will see how to check whether the given sum is found by adding some nonneighbouring Fibonacci numbers or not, if so, what are the numbers? For example if the give sum value is 10, this is sum of 8 and 2. Both 8 and 2 are Fibonacci terms and they are not adjacent. Let us see the algorithm to get the idea.AlgorithmnonNeighbourFibo(sum)Begin    while sum > 0, do       fibo := greatest Fibonacci term but not greater than sum       print fibo       sum := sum - fibo    done EndExample#include using ... Read More

C++ Program for Bogosort (Permutation Sort)

Arnab Chakraborty
Updated on 31-Jul-2019 12:38:27

294 Views

Here we will see another sorting algorithm called the Bogo Sort. This sort is also known as Permutation sort, Stupid sort, slow sort etc. This sorting algorithm is particularly ineffective sorting technique. This comes under the generate and test paradigm. It repeatedly generates a permutation until it is sorted. The conception is very straight forward. Until the list is sorted just shuffle the elements.AlgorithmbogoSort(array, n)Begin    while the arr is not sorted, do       shuffle arr    done EndExample#include #include using namespace std; bool isSorted(int arr[], int n) { //check whether the list is sorted    or not ... Read More

C++ Internals

Arnab Chakraborty
Updated on 31-Jul-2019 12:32:50

285 Views

Here we will see the Class internals. Before that we will see Default constructors, which are related to internals. The default constructor is one constructor (defined by user or compiler) that does not take any argument. Now the question comes, why the default constructor is used?If the default constructor is not given, the compiler will implicitly declare default constructor. Default constructors are used to initialize some class internals. It will not affect the data member of the class. The compiler inserts default constructor in some different situations. Suppose a class is derived from another class with default constructor, or one ... Read More

Add 1 to Number Represented as Array - Recursive Approach

Arnab Chakraborty
Updated on 31-Jul-2019 12:24:28

333 Views

In this section we will see one interesting problem. Suppose one number is given. We have to increase this number by 1. This is extremely simple task. But here we will place the number as an array. each digit of that number is placed as an element of the array. If the number is 512, then it will be stored as {5, 1, 2}. And also we have to increase the number using recursive approach. Let us see the algorithm to get the clear idea.Algorithmincrement(arr, n, index)Initially the default value of index is 0begin    if index < n, then ... Read More

Add 1 to a Number Represented as Linked List

Arnab Chakraborty
Updated on 31-Jul-2019 12:20:04

228 Views

Here we will see how to add 1 with a number stored into a linked list. In the linked list, each digit of the numbers is stored. If the number is 512, then it will be stored like below −512 = (5)-->(1)-->(2)-->NULLWe are providing the list into the increment function. That will return another list after adding 1 with it. Here we are using the C++ STL linked list. Let us see the algorithm to bet better idea.AlgorithmincrementList(l1)Begin    carry := 1    res := an empty list    for each node n from l1, scan from last to first, ... Read More

Active and Inactive Cells After K Days

Arnab Chakraborty
Updated on 31-Jul-2019 12:16:53

363 Views

Here we will see one interesting problem. Suppose one binary array is given of size n. Here n > 3. A true value or 1 value indicates that the active state, and 0 or false indicates inactive. Another number k is also given. We have to find active or inactive cells after k days. After every day state of ith cell will be active if the left and right cells are not same, if they are same, then it will be inactive. The left most and right most cell has no cell before and after it. So left most and ... Read More

Rat in a Maze with Multiple Steps or Jump Allowed

Arnab Chakraborty
Updated on 31-Jul-2019 12:13:48

485 Views

The rat in maze problem is one of the well-known problem of the backtracking. Here we will see that problem with little variation. Suppose one NxN maze M is given. The starting point is top left corner M[0, 0], and the destination is right bottom corner M[N – 1, N - 1]. One rat is placed at the starting point. Our goal is to find a path from starting point to ending point that can be by the rat to reach the destination. Here the rat can jump (The variation). Now there are some constraintsThe rat can move either towards ... Read More

A Puzzle on C/C++ R-Value Expressions

Arnab Chakraborty
Updated on 31-Jul-2019 12:08:17

186 Views

Here we will see one puzzle. Suppose there is a program which is given as below, we have to tell what will be the output and why?Example#include using namespace std; int main() {    int x = 0xab;    ~x;    cout

Advertisements