Posts

How to extend display for multiple monitors when connected to a remote computer

Image
The following blog post shows how to extend display for multiple monitors when connected to a remote computer: Open Remote Desktop Connection and enter the IP address of the remote computer. Click on Show Options. Click on Display tab. Checkbox "Use all my monitors for remote session".

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; } } ...

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...

Shell script to find factorial of a number using recursion

A shell script is a computer program designed to be run by the Unix shell, a command line interpreter. This blog is about a shell scripting program to find the factorial of a number using recursion. Logic The logic is simple. If the number ' n ' is 1, return 1. Else return ' n ' * ' factorial(n-1) '. Program #!/bin/sh factorial() { if [ "$1" -gt "1" ]; then a=`expr $1 - 1` b=`factorial $a` c=`expr $1 \* $b` echo $c else echo 1 fi } echo "Enter a number:" read x factorial $x Things to note: - After writing the script, change the mod of the script to enable execution. This is done by: chmod +x factorial.sh or chmod 755 factorial.sh Output of the program Enter a number: 4 24 Good to read 1) Java program to convert mobile number pad to characters 2) Difference between "diff" and "sdiff" command 3) Java program to return first non repeated character in a string

Java program to convert mobile number pad to characters

Image
This post is about Java program to convert mobile number pad to characters. Example ========= Here is an image of mobile phone num pad. For every numbers except 1, there are certain character value associated with that. # is used to separate the letters. If the user presses 44#44, it should print HH Logic of the program ================ A hashmap is used to store the number and its corresponding characters. If the user presses the number n times before pressing # , the nth character is selected from the hashmap. if n is greater than number of characters, select the character which is at the position (n mod number of characters) Program ======== The java program is as follows: import java.util.HashMap; public class MobilePhoneNumPadToCharacters { public static void main(String[] args) { System.out.println(returnWords( "222#2#8" )); //CAT System.out.println(returnWords( "5555#2#888888#2222" )); /...

Java program to return the first non repeated character in a string

Image
This post is about java program to return the first non repeated character in a string. Example: Given a string "hhello", It should return 'e' as the first non repeated character. Logic of the program: Using a linked hash map will solve this program. All the characters[a-z] can be stored in a linked hash map as key and value should be corresponding to be number of occurrence of each character.  To return the first non repeated char, just iterate the linked hash map and return first key having value as 1. Program: package com.techjourney; import java.util.LinkedHashMap; import java.util.Map; /** * Created by Sujith Mohan on 6/4/2015. */ public class FirstNonRepeatedChar { public static char returnFirstNonRepeatedChar(String str) { if (str== null ) throw new NullPointerException(); if (str.equals( "" )) return '\0' ; LinkedHashMap<Character, Integer> map = ...

Java program to check if a string is pangram

Hi everyone, in this blog, I will be writing about a java program to check whether a string is pangram or not. Pangrams are words or sentences containing every letter of alphabet at least once. Most commonly used example of pangram is " A quick brown fox jumps over the lazy dog. " Logic of the program Initialize a string testString as "abcdefghijklmnopqrstuvwxyz" Loop the input string for every character, and replace those character in testString with empty character. If testString is an empty string, then input string is Pangram The program for pangram is as follows: package com.blog.techjourney; public class Pangram { public static Boolean checkForPangram(String str) { int len = str.length(); //test string for testing all characters are present in the given string String testString = "abcdefghijklmnopqrstuvwxyz" ; for ( int i=0;i<len;i++) { char chars =str.charAt(i); //Converting the char to String a...

Difference between "diff" and "sdiff" commands in Unix

Image
Hi everyone, in this blog post, I will be discussing about the difference between "diff" and "sdiff" commands in Unix. This is my first post about Unix. diff command diff command will compare two files and prints the difference between the 2 files. The format of diff command is diff file1 file2 . sdiff command sdiff command will compare two files side by side. The format of sdiff command is sdiff file1 file2. The contents of files are displayed side by side with a marker denoting the difference between each files. Some of the important markers used in sdiff are as follows: | - denotes the 2 files differ in the line completely. < - only the first file contains the line. > - only the second file contains the line. The better way to depict the difference is by showing examples. Here, I am comparing 2 files which are named as " ipl_teams_2008.txt " and " ipl_teams_2015.txt ". Let's see the contents of the files. ...

Java program to check if two elements of an array adds to the given sum

Hi everyone, in this blog post, I will be discussing about a famous java program, which is to check if two elements of an array adds to the given sum. Problem Given an array with elements and a sum. Check whether any 2 elements of the array adds to the given sum. Return true if the sum is present, else return false. Example Given an array {1,2,4}. The array should return true for the sum 5 and false for 4. Solution The basic solution which comes to everyone's mind would be to check each element with each other elements for the given sum. But the total time complexity in doing that will be O(n^2). We will approach the problem in a much easier way. The logic for that is as follows: Sort the given array in ascending order. Create two variables, one which tracks from the first position of array ( pos_first ) and other which tracks from the last position of the array ( pos_last ). While pos_first is less than pos_last , do the below steps. Add the elements corr...

Java program to change the elements of the array with the product of remaining elements

Hi Everyone, In this blog post I will be discussing about an interesting program, which is to change the " elements of the array with the product of remaining elements ". This program is really interesting as well as tricky. If an array contains 0, the product of all the elements in the array will be 0. For eg: If an array is [0,1,2], after performing the algorithm, it will be [2,0,0]. Logic The logic of the program is as follows: Initialize an integer variable product = 1. (This variable is used to store the product of all elements of array) Initialize an integer variable countOfZeros =0 Find the product of all the non-zero elements in the array as well as count the number of zero's in the array. If countOfZeros is greater than or equal to 2, then change all the elements of array to 0. If countOfZeros is 1, then change all non zero elements to 0 and 0 to product . If countOfZeros is 0, then change all elements to product /(element). IDE used - Intel...

Java program to get the Next Greater Element in an array

Hi Everyone, it's been a long time since I have made a post. So here goes one. In this blog post, I will be writing about a Java program to get the next greater element in an array. Expected output : For every element in the array, print the number which is the next greater to the element and which occurs after the element in position. If greater element is not found print -1. Logic of the program : Assign an element next greater value, " ngv "as -1. For every element " i " in the array, loop from the next element till last element. In the loop, if an element is greater than " i " and " ngv " is -1 , assign " ngv " as the element. In the loop, if an element is greater than " i " and " ngv " is not -1, check whether " ngv " greater than that element. If so, assign " ngv " as the element. Return " ngv ". The java program is as follows: package  com.blog.techjourney;...