Posts

Tricky Situation: strtod function converting strings to inf or nan

Image
This blog post discuss about a tricky situation which can happen when using strtod function. Strtod function interprets the contents of string as a floating point number and return its value as a double. The declaration of strtod function is as shown below: Declaration double strtod(const char*str, char **end) str ->  It represents the value of the string which is to be converted. end -> It is the reference to an already allocated object of type char* Similar to the post about atof function , strtod function also converts the string to inf or nan if the string starts with "inf" or "nan". This can cause downstream impacts if the converted value is passed to database. The following program depicts this issue: Similar Posts: Tricky Situation when using atof function Anonymous classes in C++ Sets in C++ made easy

Tricky Situation: Atof function converting strings to inf or nan

Image
This blog post says about a tricky situation when using atof function. The atof function converts a string argument to a floating point number. If the string to be converted starts with "inf" or "nan", the atof function converts the string to inf/nan instead of converting to 0. inf represents "Positive Infinity" and nan represents a value which is "Not a Number". For eg: The following strings will convert to inf when using atof function. Information -> inf Infection -> inf Influenza -> inf etc... The following strings will convert to nan when using atof function. Nanotechnology -> nan Nanogram -> nan etc... This issue can cause downstream impacts if you are using the converted values to be saved to database or other application. The following C++ program shows the above issue: This issue can be corrected by using a simple which checks if the string starts with inf and if so

Using foreach method with list in Java 8

This blog post is about using foreach method with list in Java 8. Foreach method can be used to easily iterate a list. Check my previous post to see how to iterate foreach method with map in Java 8 . The program to iterate list using foreach method is as follows: import java.util.ArrayList; import java.util.List; /* * Java program to demonstrate the use of forEach method in List */ public class ForEachList { public static void main(String[] args) { List<String> usStates = new ArrayList<>(); usStates.add( "California" ); usStates.add( "New York" ); usStates.add( "New Mexico" ); usStates.add( "Pennsylvania" ); usStates.add( "New Jersey" ); // iterating the List through normal method System.out.println( "***Iterating the list through normal method***\n" ); for (String states:usStates) {

Foreach method in Java 8

This blog post is about foreach method in Java 8. This method is used to iterate over collections. The main use I found in this method is to iterate maps in Java. The normal syntax used in iterating a map in Java is as follows: for (Map.Entry<String, String> entry : usStates.entrySet()) Here 'usStates' is the name of the map. This syntax is slightly difficult to remember. Using foreach method, we can iterate the map as follows; usStates.forEach((k,v)-> { //Easy to iterate the map This is explained in a program as shown below: import java.util.HashMap; import java.util.Map; public class ForEachMap { public static void main(String a[]) { Map<String, String> usStates = new HashMap<>(); usStates.put( "California" , "Sacramento" ); usStates.put( "Kansas" , "Topeka" ); usStates.put( "New York" , "Albany" ); usStates.p

Java program to find the highest product of 3 elements in an array

This blog post is about java program to find the highest product of 3 elements in an array. Introduction: When this question is asked, the usual solution which comes to our mind will be multiplying each element with other elements and finding the maximum product out of those, which is the brute force solution. The function for brute force  approach is as follows: /* * Method to return the highest product of three elements. * return type: Integer * input: int arr[] */ public static Integer returnHighestProduct( int arr[]) { if ((arr== null )||(arr.length<3)) { return null ; } else { int product = Integer.MIN_VALUE; for ( int i=0;i<arr.length;i++) { for ( int j=i+1;j<arr.length;j++) { for ( int k=j+1;k<arr.length;k++) { int max_product = arr[i]*arr[j]*arr[k]; if (max_product>=product) { product = max_product; } } } } return product; } } Bu

Java program to check if an array is bitonic

This blog post is about a java program to check whether an array is bitonic or not. Introduction A bitonic array for this program is defined as an array of length 'N" and for an index 'K' in this array, where 0<K<N-1, the sequence from 0 to K should be increasing order and from K to N-1 should be in decreasing order. Eg: {1,5,3}, {1,2,3,10,9,8} Solution From starting position, find the element K , upto which the sequence is in increasing order. Check whether the sequence is in decreasing order from K to last element in the array. If not return false. Return true after execution of the method. Program package techjourney.programs; /** * Created by Sujith Mohan on 7/20/2015. * This java program is for checking whether a given array is bitonic or not. * A bitonic array for this program is defined as an array of length 'N' and * for an index 'K' in this array, where 0<K<N-1, the sequence from 0 to K should be * incre