Thursday, 10 November 2016

How to create Spiral matrix in C++

Hope you all know about spiral matrices. Here the given c++ program will gives spiral matrix as output. This is commonly asked in programming rounds of many interviews.

input : 4;
output is :
1      2     3     4 
12   13   14    5 
11   16   15    6 
10    9    8      7

input : 5;
output :
1       2     3     4    5 
16   17   18   19    6 
15   24   25   20    7 
14   23   22   21    8 
13   12   11   10    9

#include <iostream>
#include <iomanip>

#define SIZE 4

int main()
{
    int matrix[SIZE][SIZE] = {0};
    int count = 0;
    
    int up = 0;
    int down = SIZE - 1;
    while(count != SIZE * SIZE)
    {
        for(int i = 0; i < SIZE; ++i)
        {
            if(matrix[up][i] == 0)
            {
                matrix[up][i] = ++count;
            }
        }
        for(int i = 0; i < SIZE; ++i)
        {
            if(matrix[i][down] == 0)
            {
                matrix[i][down] = ++count;
            }

        }
        for(int i = SIZE - 1; i >= 0; --i)
        {
            if(matrix[down][i] == 0)
            {
                matrix[down][i] = ++count;
            }

        }
        for(int i = SIZE - 1; i >= 0; --i)
        {
            if(matrix[i][up] == 0)
            {
                matrix[i][up] = ++count;
            }

        }
        ++up;
        --down;
    }
    
    std::cout << "The Spiral Matrix of number : " << SIZE << std::endl;
    for(int i = 0; i < SIZE; ++i)
    {
        for(int j = 0; j < SIZE; ++j)
        {
            std::cout << std::setw(4) << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    } 
}

This is one of the method for creating spiral matrix. This may not be an efficient method you can also search for others methods.

Monday, 31 October 2016

Return different types using Boost Variant

Here the given program will explain how boost variant  will helpful when there is a situation like, we need to return multiple types. There may be situations like we cannot use templates. In such cases boost variant will be the best solution.


#include <iostream>
#include <string>
#include <boost/variant.hpp>

boost::variant<int,double,std::string> foo(int someValue)
{
    if(0 == someValue)
    {
        return 1;
    }
    else if(someValue < 0)
    {
        return 2.5;
    }
    else
    {
        return "hello";
    }
}

int main()
{
    //In this case the return value is an integer
    boost::variant<int,double,std::string> value1 = foo(0);
    int integerValue = boost::get<int>(value1);

    //In this case the return value is a double
    boost::variant<int,double,std::string> value2 = foo(-1);
    double doubleValue = boost::get<double>(value2);

    //In this case the return value is an std::string
    boost::variant<int,double,std::string> value3 = foo(1);
    std::string stringValue = boost::get<std::string>(value3);

    return 0;
}

Whenever you use boost::variant you should be very careful. If you are C++ beginner like me, definitely you may have a chance get an error message like this

 "terminate called after throwing an instance of 'boost::bad_get'   what():  boost::bad_get: failed value  get using boost::get Aborted (core dumped)"

This is because you need to specifically get (boost::get) the value from boost::variant variable with it's type. If you try to boost::get by wrong type then you will get the above error message.

How to know about type of value inside a boost variant variable


boost::variant<int, double, std::string> someVariable;
someVariable = "Hello World";

"someVariable.which()" will give index of the type as declared in variable. In this case it will give index as 2.

Thursday, 27 October 2016

C++ program to get difference between two dates using boost library

The following program will gives difference between two dates (time duration).

#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include "boost/date_time/posix_time/posix_time.hpp"

int main()
 {
  std::string date_1 = "2016-05-15 10:12:02";
  std::string date_2 = "2016-09-15 16:40:01";

  boost::posix_time::ptime t1(boost::posix_time::time_from_string(date_1));
  boost::posix_time::ptime t2(boost::posix_time::time_from_string(date_2));

  boost::posix_time::time_duration td = t2 - t1;

  std::cout <<"difference between dates :" <<  boost::posix_time::to_simple_string(td) << std::endl;
}

Note: compile using g++ filename.cpp -lboost_date_time

Function which returns number working days between 2 dates

           If you want to calculate number of working days between two specific dates you can use the given function. It will ignore all Saturday and Sunday during this period (may useful in financial field).  

int dateDifference(boost::gregorian::date startDate_,
                   boost::gregorian::date endDate_)
{
    int dayCount=0;
    for(boost::gregorian::day_iterator iter = startDate_; iter!= endDate_; ++iter)
    {
        if(iter->day_of_week() !=  boost::date_time::Saturday &&
           iter->day_of_week() !=  boost::date_time::Sunday)
            ++dayCount;
    }

    return dayCount;
}

Tuesday, 25 October 2016

c++ program to parse comma separated string

Input string is a comma separated string (s). output will be an std vector of string (vec).  


#include <iostream>
#include <boost/algorithm/string.hpp>
#include <vector>
int main()
{
    std::vector<std::string> vec;
    std::string s = "I,am,an,excellent,c++,programmer";

    boost::split(vec,s,boost::is_any_of(","));

    return 0;
}

C++ and BOOST

C++ is a middle-level, general-purpose, programming language developed by Bjarne Stroustrup in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. Boost is one of the most important set of libraries for the C++ programming language. It provides support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It contains over eighty individual libraries. Most of the Boost libraries are licensed under the Boost Software License.
          I would like to share simple c++ programs which uses boost library. It will be very useful for beginners.