Docsity

Vector Operations: Addition, Subtraction, Dot Product, and Cross Product, Exercises of Object Oriented Programming

Toggle sidebar
bg1
#include<iostream>
#include<math.h>
#include"Salman_Header_File.h"//including header file
using namespace std;
void main()
{
int num,choice;//'num' is used for no. of vectors entered by
user.'choice' takes option no chosen by the user
float ang,mag;///'ang' takes angle input.'mag' taes magnitude input
vector* operation;//operation is a pointer to take memory allocated
from new
vector* answer;//answer is a pointer to take memory allocated from
new
cout<<"WHICH OPERATION YOU WANT TO PERFORM?\n"<<"1- FOR
ADDITION\n"<<"2-FOR SUBTRACTION\n"<<"3-FOR DOT PRODUCT\n"<<"4-FOR CROSS
PRODUCT\n";
cin>>choice;
switch(choice)
{
case 1://this case is for addition
cout<<"How many vectors you want to add\n";
cin>>num;
operation=new vector[num] ;//array of vectors is created here
which has number of elements equal to enterd by the user
answer=new vector[num+1] ;//array for storing answers is
created here
for(int i=0;i<num;i++)//this loop takes input from the user
{
cout<<"enter magnitude\n";
cin>>mag;
operation[i].set_magnitude(mag);//this function sets
value of private data member magnitude
cout<<"enter angle\n";
cin>>ang;
operation[i].set_angle(ang);//this function sets value
of private data member angle
}
answer[0].set_magnitude(0);//first element of answer array
should be zero which is necesary in this logic
for(int j=0;j<(num);j++)//this loop adds first vector to
second then adds answer of first addition to third vector entered by the
user and so on..
{
answer[j+1]=answer[j]+operation[j];//here + and = are
over loaded
}
cout<<answer[num];//answer is stored in this last member of
array answer.here >> is over loaded
break;//all other cases will not be performed due to this
break
case 2:
cout<<"How many vectors you want to subtract\n";
cin>>num;
operation=new vector[num] ;
docsity.com
pf3

Partial preview of the text

Download Vector Operations: Addition, Subtraction, Dot Product, and Cross Product and more Exercises Object Oriented Programming in PDF only on Docsity!

#include #include<math.h> #include"Salman_Header_File.h"//including header file using namespace std; void main() { int num,choice;//'num' is used for no. of vectors entered by user.'choice' takes option no chosen by the user float ang,mag;///'ang' takes angle input.'mag' taes magnitude input vector* operation;//operation is a pointer to take memory allocated from new vector* answer;//answer is a pointer to take memory allocated from new cout<<"WHICH OPERATION YOU WANT TO PERFORM?\n"<<"1- FOR ADDITION\n"<<"2-FOR SUBTRACTION\n"<<"3-FOR DOT PRODUCT\n"<<"4-FOR CROSS PRODUCT\n"; cin>>choice; switch(choice) { case 1://this case is for addition cout<<"How many vectors you want to add\n"; cin>>num; operation=new vector[num] ;//array of vectors is created here which has number of elements equal to enterd by the user answer=new vector[num+1] ;//array for storing answers is created here for(int i=0;i<num;i++)//this loop takes input from the user { cout<<"enter magnitude\n"; cin>>mag; operation[i].set_magnitude(mag);//this function sets value of private data member magnitude cout<<"enter angle\n"; cin>>ang; operation[i].set_angle(ang);//this function sets value of private data member angle } answer[0].set_magnitude(0);//first element of answer array should be zero which is necesary in this logic for(int j=0;j<(num);j++)//this loop adds first vector to second then adds answer of first addition to third vector entered by the user and so on.. { answer[j+1]=answer[j]+operation[j];//here + and = are over loaded } cout<<answer[num];//answer is stored in this last member of array answer.here >> is over loaded break;//all other cases will not be performed due to this break case 2: cout<<"How many vectors you want to subtract\n"; cin>>num; operation=new vector[num] ;

answer=new vector[num+1] ; for(int i=0;i<num;i++)//this loop takes input from the user { cout<<"enter magnitude\n"; cin>>mag; operation[i].set_magnitude(mag);//this function sets value of private data member magnitude cout<<"enter angle\n"; cin>>ang; operation[i].set_angle(ang);//this function sets value of private data member angle } answer[num].set_magnitude(0);//last element of answer array should be zero which is necesary in this logic if(num==2 && operation[0].get_magnitude()==operation[1].get_magnitude() && operation[0].get_angle()==operation[1].get_angle())//if two vectors are same then their difference is zero.this caculation can not be done that's why it s printed { cout<<answer[num];//here >> is over loaded exit(1); } for(int j=num-1;j>=0;j--)//this loop subtracts last vector from second last then subtracts answer of firs subtraction from third last vector entered by the user and so on.. { answer[j]=operation[j]-answer[j+1];//here - and = are over loaded } cout<<answer[0];//answer is stored in this first member of array answer.here >> is over loaded break;//all other cases will not be performed due to this break case 3://this case performs dot product operation=new vector[2];//in this case only 2 vectors will be taken so array size is fixed float dot;//this variable will store answer of dot product for(int i=0;i<2;i++)//this loop takes input from the user { cout<<"enter magnitude\n"; cin>>mag; operation[i].set_magnitude(mag);//this function sets value of private data member magnitude cout<<"enter angle\n"; cin>>ang; operation[i].set_angle(ang);//this function sets value of private data member angle } dot=operation[0]*operation[1];//here * is overloaded cout<<dot<<endl;//here answer will be displayed break;//all other cases will not be performed due to this break case 4://this case performs cross product