Pages

Sunday, September 7, 2014

Codes on requests


CODE TO FIND THE DIRECTION AND MAGNITUDE OF A VECTOR USING CLASS
 category: OOP c++
IDe used : MS visual studio 2010
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

class vector
{
private:
     int a,b,c,d;
public:
 vector()
     {
           int a=0;
           int b=0;
           int c=0;
           int d=0;
     }
vector(int q,int w,int e,int r)
     {
           a=q;
           b=w;
           c=e;
           d=r;
     }
     void setx(int q)
     {
           a=q;
     }
     void sety(int w)
     {
           b=w;
     }
     void setz(int e)
     {
        c=e;
     }
     void setp(int r)
     {
           d=r;
     }
     int getx()
     {
           return a;
     }
     int gety()
     {
           return b;
     }
     int getz()
     {
           return c;
     }
     int getp()
     {
           return d;
     }
     float magnitude()
     {
           float u,i;
           u =c-a;
           i =d-b;
           u =u*u;
           i =i*i;
           return sqrt((u+i));
     }
     float direction()
     {
           float o,m;
           o=(d-b);
           m=(c-a);
           float n=o/m;n=atan(n);
           int j=(180/3.1412)*n;
return j;

     }
     };

void main()
{

     int q,w,e,r;
     cout<<"enter the value of a""\n";
     cin >> q;
     cout<<"enter the value of b""\n";
     cin >> w;
     cout<<"enter the value of c""\n";
     cin >> e;
     cout<<"enter the value of d""\n";
     cin >> r;

     vector v(q,w,e,r);
     cout<<"your direction is""\n";
     cout << v.direction()<<"\n";
     cout<<"your magnitude is""\n";
     cout << v.magnitude();

     getch();

}

 category DSA c++

 searching in a tree

#include<iostream>
#include<conio.h>
using namespace std;
class tree

private:
int data,elem;
tree* lchild;
tree* rchild;
public:

tree*insert(tree*temp,int elem)
{
if(temp==NULL)
{
temp=new tree;
temp->rchild=NULL;
temp->lchild=NULL;
temp->data=elem;
cout<<temp->data;
return temp;
}
else if(elem>=temp->data)
{
temp->rchild=insert(temp->rchild,elem);

}
else if(elem<temp->data)
{
temp->lchild=insert(temp->lchild,elem);

}
return temp;
}
void preorder(tree* t)
{
if(t!=NULL)
{
cout<<t->data;
preorder(t->lchild);
preorder(t->rchild);
}
}
void posorder(tree* t)
{
if(t!=NULL)

posorder(t->lchild);
posorder(t->rchild);
cout<<t->data;
}
}
int search(tree * t, int elem)
{    
if (t!=NULL)
{
if(t->data==elem)
{
cout<<"ELEMENT FOUND :  ";
return elem;
}
else if(elem>=t->data)
{
search(t->lchild,elem);
}
else if(elem<t->data)
{
search(t->rchild,elem);
} }
else
{
cout<<"ELEMENT NOT FOUND";
}
}

};
tree*root=NULL;
tree*temp=NULL;
void main()
{
int e;
tree t;
cout<<"DATA PRESENT IN TREE IS"<<endl;
root=t.insert(NULL,9);
t.insert(root,6);
t.insert(root,11);
t.insert(root,5);
t.insert(root,2);

cout<<"\npre Order :  ";
t.preorder(root);
cout<<endl;
cout<<"Post order :  ";
t.posorder(root);
cout<<"enter the element you want to search";
cin>>e;

t.search(root,e);

getch();

}

 feel free to ask any question on our fb page :)



No comments:

Post a Comment