Thursday, 29 January 2015

#include<iostream>
int main()
{
  for (int target=1; target < 1000; target++)
  {
  int sum=0;
  for(int i=1;i<target && sum < target+1; i++)
  {
    if(target%i == 0)
    { 
      sum += i;
    } 
  }
    if(sum == target)
    { 
      std::cout<<"\nperfect number"<<target;
    } 
  }

Saturday, 10 January 2015

#include<iostream>
#include<vector>
#include<map>
#include<utility>
class Vertex
{
public:
 int m_id;
 unsigned int m_cost;
 bool m_isVisited;
 std::map<int,unsigned int> m_edgeList; // map to store neighbour nodes and cost to reach them
 void findMinDistance(std::vector<Vertex>&);
 Vertex(int p_id)
 {
  m_id= p_id;
  if(p_id == 0)
  {
   m_cost=0;
  }
  else
  {
   m_cost=UINT_MAX; // Assigning Infinite cost
  }
  m_isVisited=false;
 }
 Vertex()
 {  

 }

};

void Vertex::findMinDistance(std::vector<Vertex> &p_vertex)
{
 m_isVisited= true;
 // Looping throgh the neighbour list and update cost

 for(std::map<int,unsigned int>::iterator iter = m_edgeList.begin();
 iter!=m_edgeList.end(); iter++)
 {

  //If cost of picked neighbour is greater than current_vertex+new cost then replace cost

  if ( p_vertex.at(iter->first).m_isVisited == false && p_vertex.at(iter->first).m_cost > m_cost+(iter->second))
  {
   p_vertex.at(iter->first).m_cost=m_cost+iter->second;

   std::cout<<"\nVertex Id : "<<p_vertex.at(iter->first).m_id;
   std::cout<<"\nExisting Cost  : "<<p_vertex.at(iter->first).m_cost;  
   std::cout<<"\nNew Cost : "<<p_vertex.at(iter->first).m_cost;
  }
 }
}

int main()
{
 std::vector<Vertex> l_vertex;
 for(int i=0; i<7;i++)
 l_vertex.push_back(Vertex(i)); // Push Vertex to vector container

 // updating the edges and cost to respective node

 // The below model is similar to priority queue.



 l_vertex.at(0).m_edgeList.insert(std::make_pair((l_vertex.at(5).m_id),14));

 l_vertex.at(0).m_edgeList.insert(std::make_pair((l_vertex.at(2).m_id),9));

 l_vertex.at(0).m_edgeList.insert(std::make_pair((l_vertex.at(1).m_id),7));





 l_vertex.at(1).m_edgeList.insert(std::make_pair((l_vertex.at(0).m_id),7));

 l_vertex.at(1).m_edgeList.insert(std::make_pair((l_vertex.at(2).m_id),10));

 l_vertex.at(1).m_edgeList.insert(std::make_pair((l_vertex.at(3).m_id),15));



 l_vertex.at(2).m_edgeList.insert(std::make_pair((l_vertex.at(0).m_id),9));

 l_vertex.at(2).m_edgeList.insert(std::make_pair((l_vertex.at(5).m_id),2));

 l_vertex.at(2).m_edgeList.insert(std::make_pair((l_vertex.at(3).m_id),11));

 l_vertex.at(2).m_edgeList.insert(std::make_pair((l_vertex.at(1).m_id),10));



 l_vertex.at(5).m_edgeList.insert(std::make_pair((l_vertex.at(0).m_id),14));

 l_vertex.at(5).m_edgeList.insert(std::make_pair((l_vertex.at(2).m_id),2));

 l_vertex.at(5).m_edgeList.insert(std::make_pair((l_vertex.at(4).m_id),9));



 l_vertex.at(4).m_edgeList.insert(std::make_pair((l_vertex.at(5).m_id),9));

 l_vertex.at(4).m_edgeList.insert(std::make_pair((l_vertex.at(3).m_id),6));



 l_vertex.at(3).m_edgeList.insert(std::make_pair((l_vertex.at(4).m_id),6));

 l_vertex.at(3).m_edgeList.insert(std::make_pair((l_vertex.at(2).m_id),11));

 l_vertex.at(3).m_edgeList.insert(std::make_pair((l_vertex.at(1).m_id),15));


 // updating cost of each vertex by calling findMinDistance

 for(std::vector<Vertex>::iterator iter = l_vertex.begin(); iter != l_vertex.end(); iter++)

 {

  (*iter).findMinDistance(l_vertex);

 }


 for(int i=0;i<6;i++)
 std::cout<<"\n\nshortest "<<i<<" "<<l_vertex.at(i).m_cost;
 return 0;

}

Friday, 9 January 2015

In school days we have cam across this query in data base to fetch the date in database.

select sysdate from dual;


What is that dual?

Dual is one dummy table in oracle database. It is created by default while installing the oracle database.

It contains one field and one column.

Field name: DUMMY VARCHAR(1);

It contains the value as x.

To know more just execute the following query.

select * from dual;


The output will be

DUMMY
-------------------
X


What is the need for dual table?

when one want to perform some arithemetic operations,selecting the pseudo columns can use the dummy dual table because oracle do not allow to perform arithemetic operations without from clause

I.e you cannnot run "select 5*4" in oracle database. From clause required error will be thrown.


For eg:

select  5*5 from dual;
select  sysdate  from dual;
select sysuser from dual;