Wednesday, September 17, 2014

Objective:
Write a program to find.
XA + YB
A. B
Test the program using A= (16, -6, 7), B= (4, 2,-3), X= 2, Y= -5



#include<iostream>
using namespace std;
class Vectors
{
public:
       void GetVectorAB(int x, int y, int A[3], int B[3])
       {
              int XA[3];
              int YB[3];
              int ANS[3]={0};
       int i;
       for(i=0; i<3; i++)
       {
       XA[i]=x*A[i];
       }
       for(i=0; i<3; i++)
       {
       YB[i]=y*B[i];
       }
       for(i=0; i<3; i++)
       {
       ANS[i]=XA[i]+YB[i];
       }

       cout<<"XA+YB=(";
       for(i=0; i<3; i++)
       {
              cout<<ANS[i]<<" ";
       }
       cout<<")"<<endl;
       }
      

       void GetVectorAdotB(int A[3], int B[3])
       {
       int dot[3];
       int ab=0;
       int i;
       for(i=0; i<3; i++)
       {
       dot[i]=A[i]*B[i];
       }

       for(i=0; i<3; i++)
       {
              ab=ab+dot[i];
       }

       cout<<"A.B = "<<ab<<endl;
      
       }
};

void main()
{
       int n;
       Abc:
cout<<"What expression you want to do???"<<endl;
cout<<"1:     XA+YB???"<<endl;
cout<<"2:     A.B????"<<endl;
cin>>n;

if(n==1)
{
       int a,b;
       int A[3]={0};
       int B[3]={0};
       cout<<"Enter vector A"<<endl;
       for(int i=0; i<3; i++)
       {
       cin>>A[i];
       }

       cout<<"Enter vector B"<<endl;
       for(int i=0; i<3; i++)
       {
       cin>>B[i];
       }
       cout<<"Enter a constant value for X"<<endl;
       cin>>a;
       cout<<"Enter a constant value for Y"<<endl;
       cin>>b;
       Vectors v1;
       v1.GetVectorAB(a,b,A,B);
}
else
       if(n==2)
       {
              int A[3];
              int B[3];
              cout<<"Enter vector A"<<endl;
              for(int i=0; i<3; i++)
              {
              cin>>A[i];
              }
              cout<<"Enter vector B"<<endl;
              for(int i=0; i<3; i++)
              {
              cin>>B[i];
              }
              Vectors v2;
              v2.GetVectorAdotB(A,B);
       }
       else
       {
       cout<<"Enter a valid number\n\n";
       goto Abc;
       }
}


No comments:

Post a Comment