Quickly Swap Two Arrays of the Same Size in C++

Ayush Gupta
Updated on 02-Mar-2020 10:41:17

241 Views

In this tutorial, we will be discussing a program to understand how to quickly swap two arrays of same size in C++.For this we will be using a quick method called std::swap() for swapping the elements of the two given arrays.Example Live Demo#include #include using namespace std;    int main (){    int a[] = {1, 2, 3, 4};    int b[] = {5, 6, 7, 8};    int n = sizeof(a)/sizeof(a[0]);    swap(a, b);    cout

Print a Semicolon Without Using Semicolon in C/C++

Ayush Gupta
Updated on 02-Mar-2020 10:38:18

330 Views

In this tutorial, we will be discussing a program to understand how to print a semicolon(;) without using a semicolon in /C++.This can be done in two possible ways, either by using the ascii value of semicolon or using user-defined macros for the same.Example Live DemoUsing putchar() method#include int main(){    //ASCII value of semicolon is equal to 59    if (putchar(59)){    }    return 0; }Output;Example Live DemoUsing Macros :#include #define POINT printf("%c",59) int main(){    if (POINT) {    } }Output;

Join Two Vectors Using STL in C++

Ayush Gupta
Updated on 02-Mar-2020 10:27:32

285 Views

In this tutorial, we will be discussing a program to understand how to join two given vectors using STL library in C++.To join two given vectors we would be using the set_union() method from the STL library.Example Live Demo#include using namespace std; int main(){    //collecting the vectors    vector vector1 = { 1, 45, 54, 71, 76, 12 };    vector vector2 = { 1, 7, 5, 4, 6, 12 };    sort(vector1.begin(), vector1.end());    sort(vector2.begin(), vector2.end());    cout

What are Ordered Dictionaries in Python

Jayashree
Updated on 02-Mar-2020 10:21:48

517 Views

An OrderedDict is a dictionary subclass that remembers the order in which its contents are added, supporting the usual dict methods.If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.>>> from collections import OrderedDict >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'mango': 2} >>> od=OrderedDict(d.items()) >>> od OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('mango', 2)]) >>> od=OrderedDict(sorted(d.items())) >>> od OrderedDict([('apple', 4), ('banana', 3), ('mango', 2), ('pear', 1)]) >>> t=od.popitem() >>> t ('pear', 1) >>> od=OrderedDict(d.items()) >>> t=od.popitem() >>> t ('mango', 2)

Difference Between Operator and Method on Python Set

Jayashree
Updated on 02-Mar-2020 10:21:19

537 Views

Python's set object represents built-in set class. Different set operations such as union, intersection, difference and symmetric difference can be performed either by calling corresponding methods or by using operators.Union by method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.union(s2) {1, 2, 3, 4, 5, 6, 7, 8} >>> s2.union(s1)  {1, 2, 3, 4, 5, 6, 7, 8}Union by | operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1|s2  {1, 2, 3, 4, 5, 6, 7, 8}Intersection by method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.intersection(s2) {4, 5} >>> s2.intersection(s1)  {4, 5}Intersection & operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1&s2 {4, 5} >>> s2&s1  {4, 5}Difference method>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.difference(s2) {1, 2, 3} >>> s2.difference(s1)  {8, 6, 7}Difference - operator>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1-s2 {1, 2, 3} >>> s2-s1  {8, 6, 7}

Find Value Closest to Negative Infinity in Python

Jayashree
Updated on 02-Mar-2020 10:18:38

194 Views

Although infinity doesn't have a concrete representation, the closest number to negative infinity is represented as return value of float() function with '-inf' as argument>>> a=float('-inf') >>> -inf

Find Value Closest to Positive Infinity in Python

Jayashree
Updated on 02-Mar-2020 10:18:04

226 Views

Although infinity doesn't have a concrete representation, the closest number to infinity is represented as return value of float() function with 'inf' as argument>>> a=float('inf') >>> a inf

Find Fractional and Integer Parts of x in a Two-Item Tuple in Python

Jayashree
Updated on 02-Mar-2020 10:16:43

151 Views

The method modf() returns the fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float. First item in tuple is fractional part>>> import math >>> math.modf(100.73) (0.730000000000004, 100.0) >>> math.modf(-5.55) (-0.5499999999999998, -5.0)

Find Power of a Number Using Recursion in Python

Jayashree
Updated on 02-Mar-2020 10:16:14

994 Views

Following program accepts a number and index from user. The recursive funcion rpower() uses these two as arguments. The function multiplies the number repeatedly and recursively to return power.Exampledef rpower(num,idx):     if(idx==1):        return(num)     else:        return(num*rpower(num,idx-1)) base=int(input("Enter number: ")) exp=int(input("Enter index: ")) rpow=rpower(base,exp) print("{} raised to {}: {}".format(base,exp,rpow))OutputHere is a sample run −Enter number: 10 Enter index: 3 10 raised to 3: 1000

Difference Between notify() and notifyAll() in Java

Nitin Sharma
Updated on 02-Mar-2020 10:15:56

7K+ Views

Both notify and notifyAll are the methods of thread class and used to provide notification for the thread.But there are some significant differences between both of these methods which we would discuss below.Following are the important differences between notify and notifyAll.Sr. No.KeynotifynotifyAll1NotificationIn case of multiThreading notify() method sends the notification to only one thread among the multiple waiting threads which are waiting for lock.While notifyAll() methods in the same context sends the notification to all waiting threads instead of single one thread.2Thread identificationAs in case of notify the notification is sent to single thread among the multiple waiting threads so ... Read More

Advertisements