20141231

How to make a simple and engaging GUI application for windows | SLIDE 1

Welcome  back , 

Have you ever wondered how to make a standalone windows application with a decent GUI (Graphical User Interface) like the one shown in the picture ! if yes , you have landed on the right place .



We will use Adobe Flash Professional 13    For this , if you have never used Flash before don't be scared its easy and very intuitive even for beginners .

Download and install a copy of Flash professional 13 from appropriate source.


This time we will try to make a simple calculator application for windows

before you start goto upper right hand corner of the window and change your workspace perspective to developer



Step 1  Project Setup

Open Flash Professional  goto File -> New  in new window dialog box goto templates and click on AIR window sample in sample file



window will look something like this 



 you can run this file as it is to see if everything is woking

Step 2 :First Run 

 Before you run your application for the first time you will have to configure publish settings for AIR  windows settings  , AIR or Adobe Integrated Runtime is just like java virtual machine for now you can think of it as software over which flash applications can run . AIR is a virtual cross-platform technology using which your application will be able to run under any platform be it Windows or OS-X  .

click on File->  AIR 3.6 For Desktop Settings   

In this AIR settings dialog box

in general Tab  change all the settings under Output as  , Windows style , Profiles as shown in the picture below you can name your output file anything you like .


Now goto Signature tab


You will have to create a new certificate for your application click on <create> button next to browse button

Fill in the appropriate information and remember the password and save location after saving this certificate , browse for the certificate and write the password you used , tick the remember for this session and click OK .

Now to check the result click on Control -> Test or press (ctrl + Enter)  
If you have done everything correctly a window would show up



 Try clicking on the maximize button of this window :)

If everything works Congrats you are one step closer to make your own calculator application for windows

Step 3 :Setting up components For calculator  

visit How to make a simple and engaging GUI application for windows  |  SLIDE 2 


20140919

Gauss Jordan Method to Find Solution to simultaneous linear equation

C++ program for Finding Solution to Simultaneous linear equation using Gauss Jordan Method
#include<iostream>
#include<fstream>


using std::cin;
using std::cout;
using std::endl;


//make a1 -> a2
double factor(double a1,double a2){
    return (a2/a1);
}

void prog(){
    int var;
    cout<<"Enter number of variables/number of equations : ";
    cin>>var;
    cout<<"Enter equations \na1x+b1x=z1\na2x+b2x=z2 \n -- in the form -- \n  a1 b1 z1 \n  a2 b2 z2\n ";
    double arr[var+1][var+2];
    for(int i=1;i<=var;i++){
        for(int j=1;j<=var+1;j++){
            cin>>arr[i][j];
        }
    }
    int elem=1;
    for(int i=1;i<=var;i++){
        int div=arr[i][i];
        for(int j=1;j<=var+1;j++){
            if(div!=0)
            arr[i][j]=arr[i][j]/div;
            cout<<"==>"<<arr[i][j]<<"   "<<div<<endl;
        }
        if(i==var)
            break;
        for(int j=i+1;j<=var;j++){

            if(arr[j][elem]==0)
                continue;
            double rat=factor(arr[i][elem],arr[j][elem]);
            for(int k=elem;k<=(var+1);k++){
                arr[j][k]=arr[j][k]-arr[i][k]*rat;
            }
        }
        elem++;
    }



    /*for(int i=1;i<=var;i++){
        for(int j=1;j<=var+1;j++){
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }*/

    double ans[100]={0};
    int skip=var;
    for(int i=var;i>0;i--){
        double temp_ans=arr[i][var+1];
        for(int j=i;j<=var;j++){
            if(j==skip)
                continue;
            temp_ans-=ans[j]*arr[i][j];
        }
        if(arr[i][skip]!=0)
            ans[skip]=temp_ans/arr[i][skip];
        else
            ans[skip]=0;
        skip--;
    }
    cout<<endl;
    for(int i=1;i<=var;i++){
        cout<<"variable "<<i<<" = "<<ans[i]<<endl;
    }
}


int main(){
    //freopen("inps.txt","r",stdin);
    prog();
}

Gauss Elimination Method For Solution To simultaneous linear equation

C++ program for Gauss Elimination method to finding solutions to simultaneous linear equation in n variable.

    #include<iostream>
    #include<fstream>
     
     
    using std::cin;
    using std::cout;
    using std::endl;
     
     
    //make a1 -> a2
    double factor(double a1,double a2){
        return (a2/a1);
    }
     
    void prog(){
        int var;
        cout<<"Enter number of variables/number of equations : ";
        cin>>var;
        cout<<"Enter equations \na1x+b1x=z1\na2x+b2x=z2 \n -- in the form -- \n  a1 b1 z1 \n  a2 b2 z2\n ";
        double arr[var+1][var+2];
        for(int i=1;i<=var;i++){
            for(int j=1;j<=var+1;j++){
                cin>>arr[i][j];
            }
        }
        int elem=1;
        //converting to upper triangular form
        for(int i=1 ;i<var; i++){
            for(int j=i+1; j<=var; j++){
                if(arr[j][elem]==0)
                    continue;
                double rat=factor(arr[i][elem],arr[j][elem]);
                for(int k=elem; k<=(var+1); k++){
                    arr[j][k]=arr[j][k]-arr[i][k]*rat;
                }
            }
            elem++;
        }
        //back substitution
        double ans[100] = {0};
        int skip=var;
        for(int i=var;i>0;i--){
            double temp_ans=arr[i][var+1];
            for(int j=i;j<=var;j++){
                if(j==skip)
                    continue;
                temp_ans-=ans[j]*arr[i][j];
            }
            if(arr[i][skip]!=0)
                ans[skip]=temp_ans/arr[i][skip];
            else
                ans[skip]=0;
            skip--;
        }
        cout<<endl;
        for(int i=1;i<=var;i++){
            cout<<"variable "<<i<<" = "<<ans[i]<<endl;
        }
    }
     
     
    int main(){
        //freopen("inps.txt","r",stdin);
        prog();
    }

       
 

20140722

An Easy Implementation of Dijkstra's Shortest Path Algorithm.

Dijkstra's algorithm is an very easy and intuitive shortest path algorithm , it is a greedy algorithm greedy simply meaning it picks the best choices(Intermediate Shortest path here) first but this is not all about  Dijkstra's algorithm Think of it as thousands of ants dropped at a point in a maze the ants move outwards from the point in all the directions assuming they all move at same speed the first one to move out will have taken the shortest path possible .

It is interesting to note that one can see Dijkstra's Algorithm in action during lightning while the bolt searches for the path of least resistance .The Bolt finds a locally optimal choice at every point and it actually fails to find the most optimal path .



This animation from Wikipedia provides a decent explanation of how the algorithm works


This is an implementation of Dijkstra's algorithm based on following algorithm
  1. Min_Heap H /make a min heap to store vertex
  2. /returns the vertex with least weight
  3. visited[0...V] <- 0 /mark all the vertex un-visited
  4. weight[1...V] <- Infinity /path length is infinity for all nodes
  5. weight[source] <- 0
  6. previous[1...V] <- Infinity /to store actual path
  7. H.push(<weight,source>)/push source vertex in heap
  8. while(!H.isEmpty()):
  9.     u <- H.pop()
  10.     for each neighbor v of u:
  11.         new_dist <- weight[u] + graph[u-v]
  12.         /weight of path from u to v + shortest weight to reach u
  13.         if( !visited[v] ):
  14.             H.push(<v.weight,v>)
  15.             if(new_dist < weight[v])
  16.                 previous[v] <- u
  17.             weight[v] <- min(weight[v] , new_dist)
  18.            
  19.     visited[u] <- 1 /mark node visited
  20.     if(== destination_vertex):
  21.         return weight[destination_vertex]
  22. return INFINITY //if there is no way to reach destination

Here's an implementation in c++

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<vector>
  5. #define INF 1<<31-1
  6. using namespace std;
  7. class min_heap{
  8.     //can also be implemented by make_heap(v.begin(), v.end(), comp);
  9.     int leaf;
  10.     vector <pair<int,int>> arr;//<weight,vertex>
  11. public:
  12.     min_heap(){
  13.         leaf=1;
  14.         arr.push_back(make_pair(0,0));
  15.     }
  16.     void push(int weight,int node){
  17.         int pvt=leaf;
  18.         if(arr.size()<=pvt)
  19.             arr.push_back(make_pair(weight,node));
  20.         else
  21.             arr[pvt]=make_pair(weight,node);
  22.         while(pvt>1){
  23.             if(arr[pvt].first<arr[pvt/2].first){
  24.                 swap(arr[pvt],arr[pvt/2]);
  25.                 pvt=pvt/2;
  26.             }
  27.             else
  28.                 break;
  29.         }
  30.         leaf++;
  31.     }
  32.     int pop(){
  33.         int ans=arr[1].second;
  34.         arr[1]=arr[leaf-1];
  35.         int elm=arr[1].first,x=1;
  36.         while(x*2+1<=leaf-1){
  37.             int exc=1,temp=arr[x].first;
  38.             //exchanging parent with the child with smaller value
  39.             if(arr[x*2].first>arr[x*2+1].first && (elm>arr[x*2].first || elm>arr[x*2+1].first )){
  40.                 swap(arr[x],arr[x*2+1]);
  41.                 x=x*2+1;
  42.                 continue;
  43.             }
  44.             if(arr[x*2].first<=arr[x*2+1].first && (elm>arr[x*2].first || elm>arr[x*2+1].first)){
  45.                 swap(arr[x],arr[x*2]);
  46.                 x=x*2;
  47.                 continue;
  48.             }
  49.             break;
  50.         }
  51.         if(leaf-1>=x*2  &&  arr[x*2].first<arr[x].first ){
  52.             swap(arr[x],arr[x*2]);
  53.         }
  54.         leaf--;
  55.         return ans;
  56.     }
  57.     bool isempty(){
  58.         if(leaf<=1)
  59.             return true;
  60.         return false;
  61.     }
  62. };
  63. class dijkstra{
  64. public:
  65.     vector <int> visited;//to mark if a vertex has been visited
  66.     vector <int> weight;//Current minimum weight to n'th node from source
  67.     vector <int> previous;//to store shortest path information
  68.     min_heap heap;
  69.     bool calculated;
  70.     int vertex;
  71. //public:
  72.     dijkstra(int vertex_cnt){
  73.         visited.resize(vertex_cnt+1,0);
  74.         weight.resize(vertex_cnt+1,INF);
  75.         previous.resize(vertex_cnt+1,INF);
  76.         calculated=false;
  77.         vertex=vertex_cnt;
  78.     }
  79.     // graph[i][0] contains the no of edges to i'th vertex
  80.     // graph[i][1...graph[i][0]] contains the vertex information connected to i'th vertex
  81.     // information structure <weight,vertex>
  82.     int shortest_path(vector <vector<pair<int,int>>> graph,int first_node,int end_node){
  83.         weight[first_node]=0;
  84.         heap.push(first_node,first_node);
  85.              
  86.         while(heap.isempty()==false){
  87.             int p_vertex=heap.pop();
  88.             for(int i=1;i<=graph[p_vertex][0].first;i++){
  89.                 int new_vertex=graph[p_vertex][i].second;//
  90.                 int new_weight=weight[p_vertex]+graph[p_vertex][i].first;
  91.                 if(visited[new_vertex]==0){
  92.                     heap.push(new_weight,new_vertex);
  93.                     if(new_weight<weight[new_vertex])
  94.                         previous[new_vertex]=p_vertex;
  95.                         //updating path if a path with less weight is found
  96.                     weight[new_vertex]=min(weight[new_vertex],new_weight);//update
  97.                 }
  98.             }
  99.             visited[p_vertex]=1;
  100.             if(p_vertex==end_node){
  101.                 calculated=true;
  102.                 return weight[end_node];
  103.             }
  104.         }
  105.         return INF;
  106.     }
  107.     //to return the actual shortest path taken in reverse
  108.     void shortest_path(vector <int> &act_path,int source,int destination){
  109.         if(calculated=false)
  110.             return;
  111.         act_path.push_back(destination);
  112.         while(destination!=source){
  113.             act_path.push_back(previous[destination]);
  114.             destination=previous[destination];
  115.         }
  116.     }
  117. };
  118. /*
  119. format of graph is an adjacency list implemented through a vector or vectors
  120. the first element of the vector contains number of edges adjacent to a node
  121. */
  122. int main()
  123. {
  124.     //freopen("input.txt","r",stdin);
  125.     vector <vector<pair<int,int>>> graph1;//A vector of vector list to contain adjacency list
  126.     graph1.push_back(vector<pair<int,int>> (1,make_pair(0,0)));
  127.     int N;
  128.     cin>>N;//number of vertex
  129.     for(int i=1;i<=N;i++){
  130.         int E;
  131.         cin>>E;//number of vertex to which i'th vertex is connected
  132.         vector <pair<int,int>> info;
  133.         info.push_back(make_pair(E,0));
  134.         for(int j=1;j<=E;j++){
  135.            
  136.             int vtx,weight;
  137.             cin>>vtx>>weight;//vertex connection and weight of that edge
  138.             info.push_back(make_pair(weight,vtx));
  139.            
  140.         }
  141.         graph1.push_back(info);
  142.     }
  143.    
  144.     int source,destination;
  145.     cin>>source>>destination;//destination
  146.     dijkstra short1(N);
  147.     cout<<"shortest distance ="<<short1.shortest_path(graph1,source,destination);
  148.     vector <int> path1;//to store actual path in reverse
  149.     short1.shortest_path(path1,source,destination);
  150.     for(int i=0;i<path1.size();i++)
  151.         cout<<path1[i]<<endl;
  152. }

Here you can find a sample input file for the above program tested in gnu's  g++ 11 compiler