Found 7347 Articles for C++

C++ Program to Print Matrix in Z form?

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

136 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

175 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 or Permutation Sort?

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

178 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

161 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

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

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

95 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

Print system time in C++ (3 different ways)

Sunidhi Bansal
Updated on 02-Jul-2020 08:52:10

11K+ Views

There are different ways by which system day, date and time can be printed in Human Readable Form.First wayUsing time() − It is used to find the current calendar time and have arithmetic data type that store timelocaltime() − It is used to fill the structure with date and timeasctime() − It converts Local time into Human Readable FormatDay Month Date hour:month:second YearExample#include #include // used to work with date and time using namespace std; int main() {    time_t t; // t passed as argument in function time()    struct tm * tt; // decalring variable for localtime()   ... Read More

C/C++ Program for Median of two sorted arrays of same size?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

285 Views

Here we will see how to get the median of two sorted array of the same size. We will use C++ STL to store array elements. After getting two arrays, we will merge them into one. As two arrays of same size are merged, then the final array will always hold even number of elements. We need to take two middle elements, then get the average of them for the median.Algorithmmedian(arr1, arr2)Begin    arr3 := array after merging arr1 and arr2    sort arr3    len := length of arr3    mid := len/2    median := (arr3[mid] + arr3[mid-1])/2 ... Read More

Bisymmetric matrix in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

131 Views

Here we will see one program that will help to check whether a matrix is bisymmetric or not. The Bisymmetric matrix is one square matrix that are symmetric about both of the major diagonals. The below matrix is an example of bisymmetric matrix.1 2 3 4 5 2 6 7 8 4 3 7 9 7 3 4 8 7 6 2 5 4 3 2 1AlgorithmcheckBiSymmetric(mat, n)Begin    for i in range 0 to n – 1, do       for j in range 0 to i – 1, do          if mat[i, j] is ... Read More

Array::fill() and array::swap() in C++ STL?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

361 Views

In this section we will see what are the usage of array::fill() and the array::swap() in C++ STL.The array::fill() function is used to fill the array with some specified value. Let us see one example to get the idea.Example Live Demo#include #include using namespace std; main() {    array arr = {00, 11, 22, 33, 44, 55, 66, 77, 88, 99};    cout

Array::crbegin() and array::crend() in C++ STL?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

69 Views

Here we will see the crbegin() and crend() functions of array in C++ STL.The array::crbegin() function is used to get reverse iterator. It returns constant reverse iterator pointing to the last element of the container. This function does not take any parameter.The array::crend() function is reverse of crbegin(). This returns the iterator which is pointing the last element of the reversed iterator.Let us see some code examples to get better idea.Example Live Demo#include #include using namespace std; main() {    array arr = {00, 11, 22, 33, 44, 55, 66, 77, 88, 99};    cout

Advertisements