Found 7197 Articles for C++

Write Code to Determine if Two Trees are Identical in C++

sudhir sharma
Updated on 17-Apr-2020 13:17:27

381 Views

In this problem, we are given two trees. Our task is to write a code to check whether the two trees are identical or not.Two trees are said to be identical if elements of the arrays have the same value and orientation.ExampleAs both of the trees have the same values and position of elements both trees are identical.To check if two trees are identical, we will go from node node to each node of the and check for their equality step by step and if at any point to nodes are not equal return -1, denoting the tree are not ... Read More

Write your own memcpy() and memmove() in C++

sudhir sharma
Updated on 17-Apr-2020 13:07:34

997 Views

memcpy() function is an inbuilt function that is used to copy data from source location to destination location.Prototype of memcpy function −void * memcpy(void *destination_location, void *source_location, size_t size)We will character by character copy data from source to destination.Program to show the implementation of the solution,Example Live Demo#include #include void MemcpyFunc(void *dest, void *src, size_t n){    char *dataS = (char *)src;    char *dataD = (char *)dest;    for (int i=0; i

Write your own strcmp that ignores cases in C++

sudhir sharma
Updated on 17-Apr-2020 14:31:26

785 Views

Here, we have to create a strcmp (string compare) function that compares two string but ignores cases of the characters of the string. The function will return -1 if string1 < string2, 0 if string1 = string2, 1 if string1 > string2.Let’s take an example to understand the problem, Inputstring1 = “Hello” , string2 = “hello”Output0To create our own strcmp function that ignores cases while comparing the strings. We will iterate through all characters of both the strings, if characters at ith index are the same i.e. string1[i] == string2[i], continue. If string1[i] > string2[i], return 1. If string1[i] < ... Read More

Writing C/C++ code efficiently in Competitive programming

sudhir sharma
Updated on 17-Apr-2020 13:17:18

423 Views

In competitive programming, the most important thing is an effective code. Optimized and faster code is important and can make a difference in the ranks of the programmer.To write an effective c/c++ code in competitive programming, here are some effective tools for writing c/c++ code efficiently, First, let’s recall some basic terms, Template is writing code that does not depend on a particular type.Macro is a named code fragment.Vectors are like automatically resizable dynamic arrays that update size with insertion and deletion of the element.Now, let’s see some basic updates in code that can make in increasing efficiency of code, ... Read More

Writing OS Independent Code in C/C++

sudhir sharma
Updated on 17-Apr-2020 12:42:29

413 Views

A program that can interact with the operating system irrespective of the OS on which it runs.Most of the compilers of c/c++ have the power to define macros that detect OS.Some Macros of GCC compiler are −_WIN32: macros for 32 bit and 64-bit Windows OS._WIN64: macros for 64-bit Windows OS._UNIX: macros for UNIX OS._APPLE_: macros for macOS.Based on these macros defined, let’s create a program that will work irrespective of the OS −Example Live Demo#include using namespace std; int main() {    #ifdef _WIN32     system("dir");    #else     system("ls");    #endif     ... Read More

Written version of Logical operators in C++

sudhir sharma
Updated on 17-Apr-2020 12:38:22

185 Views

In c++ programming language, there are keywords that can be used in place of logical operators. The keywords are initially used in c when the keyboards didn’t support symbols like &&, !, ||, etc. Now, here are some written version of logical operators in c++.Operators and their written versions are −OperatorSymbolsWritten versionAnd operator&&andOr operator||orNot operator!notNot equal to operator!=not_eqBitwise and operator&bitandBitwise or operator|bitorBitwise XOR operator^And equal to operator&=and_eqOr equal to operator|=or_eqXOR equal to operator^=Program to show the implementation of our programExample Live Demo#include using namespace std; int main(){    int x=1, y=0;    coutRead More

XOR counts of 0s and 1s in binary representation in C++

sudhir sharma
Updated on 17-Apr-2020 15:37:35

226 Views

In this problem, we are given a number. Our task is to find the XOR of the count of 0s and 1s in the binary representation of the number.Let’s take an example to understand the problem, Inputn = 9Output0Explanationbinary = 1001 Count of 0s = 2 Count of 1s = 2 2 ^ 2 = 0To solve this problem, we will first convert the number of its binary equivalent and then iterating over each bit of the number, count 0s, and 1s and then find XOR of the count of 0s and count of 1s.Program to illustrate the above solution, ... Read More

XOR Cipher in C++

sudhir sharma
Updated on 17-Apr-2020 12:28:55

3K+ Views

XOR cipher or XOR encryption is a data encryption method that cannot be cracked by brute-force method.Brute-force method is a method of random encryption key generation and matching them with the correct one.To implement this encryption method, we will define an encryption key(random character) and perform XOR of all characters of the string with the encryption key. This will encrypt all characters of the string.Program to show the implementation of encryption −Example Live Demo#include #include using namespace std; void XORChiper(char orignalString[]) {    char xorKey = 'T';    int len = strlen(orignalString);    for (int i = 0; i < len; ... Read More

XOR of a subarray in C++

sudhir sharma
Updated on 17-Apr-2020 12:14:06

140 Views

In this problem, we are given an arr[] and some queries that are range between L to R in the array. Our task is to print the XOR of the subarray between L to R.Let’s take an example to understand the problem, Input − array = {1, 4, 5, 7, 2, 9} L = 1 , R = 5Output −Explanation − 4^5^7^2^9To solve this problem, we will create an array, based on the following observation, We will XOR multiple bits, if there are odd number of 1s, the result will be 1 otherwise the result is 0.Now, we will create ... Read More

XOR of a submatrix queries in C++

sudhir sharma
Updated on 17-Apr-2020 12:41:02

167 Views

In this problem, we are given a N x N matrix and some queries, each query contains the top-left and bottom-right corner of the submatrix created from this matrix. Our task is to find the XOR of all elements of the submatrix defined by the querries.Let’s take an example to understand the problem, Inputarr[][] = {{1, 2, 3} {4, 5, 6} {7, 8, 9}} Querries: {0, 0, 1, 2} , {1, 2, 2, 2}Output1 15Explainationquerry 1 : 1^2^3^4^5^6 querry 2 : 6^9To solve this problem, we will find a prefix-XOR matrix to solve queries. The value of the matrix at ... Read More

Advertisements