Counting sets of 1s and 0s in a binary matrix can be achieved using nested for loop with conditional statement. A binary matrix is a matrix that consists of only two digits as its name suggests and those two digits are 1 and 0. In this article, we are having a 2D binary matrix having 0 and 1 only. Our task is to write a JavaScript program for counting the sets of 1s and 0s in a given binary matrix. Formula: Let us a consider a set as {a, b} having 2 elements i.e, n = 2. Total number ... Read More
To get maximum and minimum in a Square Matrix in JavaScript, we will be discussing two different approaches, their complexities, and example codes. First approach make use of nested loop while second approach uses Math function. In this article we are having a 2D square matrix, our task is to write a JavaScript program for maximum and minimum in a square matrix. Example Input: Matrix: [ [1, 3, 7], [5, 2, 9], [2, 5, 1] ] Output: Maximum = 9, Minimum = 1 ... Read More
To get frequencies of even and odd numbers in a matrix, we will be discussing three different approaches. We will discuss each approach with code examples and solution of the algorithm being used. In this article we are having a 2D matrix, our task is to write a JavaScript program for frequencies of even and odd numbers in a matrix. Users must be familiar with conditional statement, nested loops and javascript operators. Example Input: Matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; Even numbers: {2, 4, 6, 8} Odd numbers: {1, 3, 5, 7, ... Read More
To efficiently compute sums of diagonals of a matrix, we will be discussing two different approaches. We will calculate the sum of the elements present in the left-to-right and right-to-left diagonal i.e primary and secondary diagonal respectively. In this article we are having a square matrix, our task is to write a JavaScript program to efficiently compute sums of diagonals of a matrix. Example Input: Matrix: [[1, 2, 3], [4, 5, 6], [9, 8, 9]] Sum of primary diagonal elements = 1+5+9 Sum of secondary diagonal elements = 3+5+9 Output: Sum of ... Read More
Diagonally dominant matrix refers to square matrix having magnitude of diagonal element in a row greater than or equal to the sum of magnitude of non-diagonal elements in that row. In this article we are having a square matrix, our task is to write JavaScript program for diagonally dominant matrix. Users must be familiar with 2D matrix, nested for loop and conditional statement. Formula: $$\mathrm{|\:a_{ii}\:|\:\geq\:\displaystyle\sum\limits_{j ≠ i}\:|\:a_{ij}|}$$ for all i Where aij denotes the entry in the ith row and jth column Example Input: [A] = ... Read More
Single quotes in C++ are used to denote characters, not strings. When you use single quotes around multiple characters or strings, C++ compiler will treat it as a multi-character literal, which will be stored as an integer value, representing the combined ASCII values of those characters. In this article, we will learn all about single quotes in C++ and how they behave when used with multiple characters. Single Quotes in C++ In C++, single quotes are used to represent a single character. For example, 'a' is a character literal representing the english alphabet 'a'. For each character, C++ assigns a ... Read More
When using std::getline after a formatted extraction (like std::cin >>), you might see an unexpected behavior, where std::getline is skipping the next input line. In this article, we will understand why this happens and how to handle it. Getline skipping input after formatted extraction The code below shows how getline() is skipping input after a formatted extraction: #include #include using namespace std; int main() { string name; string city; cin
Here we will see how to map enum type data to a string in C++ There is no such direct function to do so. But we can create our own function to convert enum to string. We shall create a function that takes an enum value as the argument, and we manually return the enum names as a string from that function. First of all, let us understand what is an enum in C++. What is an Enum in C++? Enum is a user-defined datatype in C++ that is used to assign names to integer values. For example, you ... Read More
Enum is a user-defined datatype in C++ that is used to assign names to integer values. For example, you can use an enum to represent the days of the week from 0 to 6, where 0 represents Sunday, 1 represents Monday, and so on. When you try to access a value of an enum, it will only return an integer value. In this article, we will learn how to convert an enum type variable to a string type variable in C++, so that we can easily display the enum value as a string. Example of Enum: enum color ... Read More
The lldiv() function is used to perform division operation on long long integers and return both quotient and remainder. In this article, we will learn how to use the lldiv() function from the Standard Template Library (STL) in C++. What is lldiv()? The lldiv() is a in-built function in C++ that is used to divide one long long integer by another and return the result as a structure that contains both the quotient and remainder. This function is useful when both values are needed after a division operation. The return type of this function is a structure called lldiv_t, ... Read More