Posts

How to get the range of acceptable value of arithmetic types.

numeric_limits from limits can be used to get the acceptable minimum, maximum numbers of the arithmetic types. #include <iostream> #include <limits> using namespace std ; int main ( ) { // integer range cout << " int : " << ( numeric_limits < int > :: min ( ) ) << " to " << ( numeric_limits < int > :: max ( ) ) << " \n " ; // long range cout << " long : " << ( numeric_limits < long > :: min ( ) ) << " to " << ( numeric_limits < long > :: max ( ) ) << " \n " ; // unsigned long range cout << " unsigned long : " << ( numeric_limits < unsigned long > :: min ( ) ) << " to ...

How to read array from delimited text

use readarray with -d option to read the delimited text as array in bash #!/usr/bin/env bash # User inputs echo -n "Enter Number of elements: " read -r no_of_elements echo -n "Enter delimiter: " read -r del echo -n "Enter array elements as delimited text: " readarray -n $ no_of_elements -d $ del elements # Output echo "" echo "Array elements: " for item in ${ elements [@] } ; do echo $ item done ; Output of the above snippet is, $ ./test.sh Enter Number of elements: 3 Enter delimiter: , Enter array elements as delimited text: 1,2,3, Array elements: 1, 2, 3, If we like to remove the delimiter captured as part of an array element we can pass -t option. Note: -t must be passed before supplying the -d option. Eg, readarray -t -n $no_of_elements -d $del elements -d option uses the delimiter to parse and split the ...

How integers are converted to boolean type

Anything other than 0 is coverted to true #include <iostream> int main ( ) { bool b1,b2,b3,b4,b5,b6 ; b1 = -1 ; b2 = 1 ; b3 = 0 ; b4 = 244 ; b5 = true ; b6 = false ; std :: cout << " b1 : " << b1 << " \n " ; std :: cout << " b2 : " << b2 << " \n " ; std :: cout << " b3 : " << b3 << " \n " ; std :: cout << " b4 : " << b4 << " \n " ; std :: cout << " b5 : " << b5 << " \n " ; std :: cout << " b6 : " << b6 << " \n " ; return 0 ; } Output $ ./a.out b1 : 1 b2 : 1 b3 : 0 b4 : 1 b5 : 1 b6 : 0 $