Server Side Programming Articles - Page 1603 of 2646

Computing index using pointers returned by STL functions in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:39:36

208 Views

In this section we will see how to generate index using pointers that are returned by STL in C++. As we know many inbuilt functions in C++ return the pointers to the position in memory which provides the address of desired number, but it has no relation with the actual index in the container of the returned value. As an example, to determine maximum element in a code, we use std::max_element() function, this does not return the index of the desired element, but return the address in memory. But sometimes, we need to get the index from that address. Here ... Read More

Check if a number is an Achilles number or not in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:38:56

316 Views

Suppose we have a number n; we have to check whether n is an Achilles number or not. As we know a number is Achilles number when a number that is powerful (A number N is called Powerful Number when for every prime factor p of it, p^2 also divides it) but not a perfect power. Some of the examples of Achilles numbers are: 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125.So, if the input is like 108, then the output will be True, as 6 and 36 both divide it and it is ... Read More

How to make a C++ class whose objects can only be dynamically allocated?

Arnab Chakraborty
Updated on 27-Aug-2020 13:37:16

383 Views

In this problem we will see how we can make one class for which we can only create objects through dynamic memory allocation, no direct object creation is permitted.The idea is simple. We have to create private destructor for that class. When the destructor is private, the compiler would produce a compiler error for non-dynamically allocated objects because compiler need to remove them from stack segment once they are not available for use. For dynamically allocated objects, the programmer is responsible for deleting object, but the compiler is not responsible for that, so it permits creating objects dynamically.For avoiding memory ... Read More

Why is the size of an empty class not zero in C++?

Ravi Ranjan
Updated on 15-Apr-2025 17:31:12

2K+ Views

The size of an empty class is not zero in C++ as it allocates one unique address to the object in the memory. The size can not be 0, because the two classes can not have the same memory address. So, the size of an empty class is 1 byte which does not hold any data, it is just for memory allocation. In this article, we will see an example of checking the size of an object of an empty class in C++. Demonstrating Size of an Empty Class In this approach, we have two C++ classes. One class ... Read More

Check if a number is a Trojan Numbers in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:35:01

180 Views

Suppose we have a number n, we have to check whether n is a Trojan Number or not. As we know that the Trojan Number is a number that is a strong number without a perfect power. A number n is a strong number when for every prime divisor or factor p of n, p^2 is also a divisor. In other words, every prime factor appears at least twice. Trojan numbers are strong. But the reverse is not true. So it means, not all strong numbers are Trojan numbers: only those that cannot be represented as a^b.So, if the input ... Read More

Where is an object stored if it is created inside a block in C++?

Arnab Chakraborty
Updated on 27-Aug-2020 13:30:13

1K+ Views

In this section we will discuss about where the variables and objects are stored in the memory when a C++ program is compiled. As we know, there are two parts of memory in which an object can be stored −Stack − All members that are declared inside block of memory, it stores inside the stack section. The main function is also a function, so elements inside it will be stored inside the stack.Heap − When some objects are allocated dynamically, then that is stored inside the heap section.The scope of the object declared inside a block or a function is ... Read More

Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:26:52

431 Views

Suppose we have one infinite chessboard with the same rules as that of chess and if there are N knights coordinates on the infinite chessboard and the king’s coordinate, we have to check whether the King is checkmate or not. The coordinate of infinite board is bounded by large value like (-10^9

Check if a key is present in every segment of size k in an array in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:23:50

194 Views

Suppose we have an array A with N elements, we have another value p and a segment size k, we have to check whether key p is present in every segment of size k in A.So, if the input is like A = [4, 6, 3, 5, 10, 4, 2, 8, 4, 12, 13, 4], p = 4 and k = 3, then the output will be TrueTo solve this, we will follow these steps −i := 0while i < n is non-zero, doj := 0while j < k, doif arr[j + i] is same as p, thenbreakj := j ... Read More

Check if a given string is a valid number in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:20:44

791 Views

Suppose we have a string, that holds numeric characters and decimal points, we have to check whether that string is representing a number or not. If the input is like “2.5”, output will be true, if the input is “xyz”, output will be false.To solve this, we will follow these steps −To solve this, we will use the string parsing technique of our programming language. We will try to convert string to number, if there is no exception, then that will be a number, otherwise not a number.ExampleLet us see the following implementation to get better understanding − Live Demodef isNumeric(s): ... Read More

Check if a given Binary Tree is Heap in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:13:42

641 Views

Suppose we have a binary tree; we have to check whether it is heap or not. The heap has following property: Heap will be a binary tree That tree should be a complete tree (So. all levels except last should be full). Every nodes value of that tree should be greater than or equal to its child node (max-heap).So, if the input is likethen the output will be trueTo solve this, we will follow these steps −Define a function number_of_nodes() . This will take rootif root is null, thenreturn 0otherwise, return(1 + number_of_nodes(root.left) + number_of_nodes(root.right))Define a function has_heap_property() . This ... Read More

Advertisements