We know that the binary search algorithm is better than the linear search algorithm. This algorithm takes O(log n) amount of time to execute. Though most of the cases the implemented code has some problem. Let us consider one binary search algorithm function like below −Exampleint binarySearch(int array[], int start, int end, int key){ if(start key) return binarySearch(array, start, mid-1, key); return binarySearch(array, mid+1, end, key); } return -1; }This algorithm will work fine until the start and end reaches a large number. If the (start + end) ... Read More
The boost library has large range of functionalities. The any datatype is one of them. Any datatype is used to store any type of values in variable. Some other languages like javascripts, python, we can get this kind of datatypes. In C++ we can get this feature only using boost library.Example#include "boost/any.hpp" #include using namespace std; main() { boost::any x, y, z, a; //define some variable of any datatype x = 20; //Store x as integer cout >> "x : " >> boost::any_cast(x) >> endl; //display the value of x y = 'A'; //Store y ... Read More
In C++ or Java we can get the static keyword. They are mostly same, but there are some basic differences between these two languages. Let us see the differences between static in C++ and static in Java.The static data members are basically same in Java and C++. The static data members are the property of the class, and it is shared to all of the objects.Examplepublic class Test { static int ob_count = 0; Test() { ob_count++; } public static void main(String[] args) { Test object1 = new Test(); ... Read More
To insert the contents of a database to a collection, Connect to the database and retrieve the contents of the table into a ResultSet object using the SELECT Query.DriverManager.registerDriver(new com.mysql.jdbc.Driver()); String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from MyPlayers");Create a Java class to hold the contents of each record, with a variable and, setter and getter methods for each column (with suitable datatype).For example, if the table sample in the database has two fields with the details −Column name: ID, datatype: INT(11)Column name: Name, datatype VARCHAR(255)Then, the variable ... Read More
You can use below syntax for inserting an 8-byte integer in MongoDB through JavaScript shell −anyVariableName= {"yourFieldName" : new NumberLong("yourValue")};To display the above variable, you can use the variable name or printjson(). Following is the query to insert 8 byte integer in MongoDB −> userDetail = {"userId" : new NumberLong("98686869")}; { "userId" : NumberLong(98686869) }Let us display the above variable value using variable name. The query is as follows −> userDetailThis will produce the following output −{ "userId" : NumberLong(98686869) }Let us display the variable value using printjson() −> printjson(userDetail); This will produce the following output −{ "userId" : NumberLong(98686869) }
To change header background color, at first get the header background −JTableHeader tableHeader = table.getTableHeader();Now, set the background color using set Background() −tableHeader.setBackground(Color.black);Above, we have used the Color class to set the color.The following is an example to change the header background color of a JTable −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.JTableHeader; public class SwingDemo { public static void main(String[] argv) throws Exception { Integer[][] marks = { { 70, 66, 76, 89, 67, 98 }, { 67, 89, 64, ... Read More
The stripos() equivalent in MySQL is INSTR(), which returns the position of the first occurrence of a string in another string. Following is the syntax −select instr(yourColumnName, yourWord) As anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('MySQL is my favourite subject'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('MongoDB is not my favourite subject'); Query OK, 1 row affected (0.20 sec)Display ... Read More
Here we will see one program that can tell whether two strings are rotation of each other or not. The rotation of strings is like −Suppose two strings are S1 = ‘HELLO’, and S2 = ‘LOHEL’ So they are rotation of each other. By rotating HELLO three position to the left it will be LOHEL.To solve this problem, we will concatenate the first string with itself, then check whether the second one is present in the concatenated string or not. So for HELLO, it will be HELLOHELLO. Then this concatenated string contains the LOHEL. [HELLOHELLO].AlgorithmisRotation(str1, str2)begin if lengths of ... Read More
The feclearexcept() function is used to clear the supported floating point exceptions represented by the excepts.This function returns 0, if all exceptions are cleared, or the exception value is 0. And returns nonzero value for some exceptions.To use this function, we have to enable the FENV_ACCESS. This will give our program to access the floating point environment to test the exception raised.Example#include #include #include #pragma STDC FENV_ACCESS on using namespace std; main() { feclearexcept(FE_ALL_EXCEPT); sqrt(-5); if (fetestexcept(FE_INVALID)) cout >> "sqrt(-5) will generate FE_INVALID" >> endl; }Outputsqrt(-5) will generate FE_INVALIDRead More
In computer networks, when more than one station tries to transmit simultaneously via a shared channel, the transmitted data is garbled. This event is called collision. The Medium Access Control (MAC) layer of the OSI model is responsible for handling collision of frames. Collision – free protocols are devised so that collisions do not occur. Protocols like CSMA/CD and CSMA/CA nullifies the possibility of collisions once the transmission channel is acquired by any station. However, collision can still occur during the contention period if more than one stations starts to transmit at the same time. Collision – free protocols resolves ... Read More