#### C++ Vector Manipulation code example

Beginner level code example

Objective of the Code:
• To test a class and its functions.
• To create a vector.
• To modify the values of a given vector.
• To multiply the vector by a scalar.
• To find the dot product of two vectors.
• To find the cross product of two vectors.
• To count the number of vectors present in the main program.
• To display the vector in the form like [10 20 30…. ]

Algorithm Used:

• We initialize a vector containing an array: ‘coef’ to hold the coefficients of vectors, a variable ‘t_no’ to hold the name of positions of the components of the vectors and a static variable ‘no’ just to show the usage of a static members.
• As we require maximum three vectors to meet any of our stipulated goals, so we initialize only three instance (objects) of vector class. Numbers of vector in the program before cross or dot product operation will be 2, and after that operation it will be exactly 3.
• We have define different functions like
o Getdata (changes value of coef array)
o Extractdata (return value of coef array)
o Putdata (print values of coef array on screen)
o Scalar_multi(for scalar multiplication of two vectors)
to encapsulate the data by not directing accessing member variables.
• To find dot product of the two vectors we multiplied corresponding coefficients values and add them up.
• To find cross product of the two vectors we followed the standard vector multiplication formula and put the resultant values as coefficient of third vector.
• Cross or dot product will cause the creation of third vector.

```/*
All rights reserved (Copyrighted 2012) code.ssingh.in
One can reuse this code for non-commercial purposes.
*/

#include
#include
#include
#include
#include
#include
#include

#ifdef __cplusplus
//Macro for "max" function:to find maximum of two arguments
int max (int value1, int value2);

int max(int value1, int value2)
{
return ( (value1 > value2) ? value1 : value2);
}
#endif

class vector
{
//Direct access to this variable is not being allowed
float coef;
static int no;
public:
int t_no;
void getdata(float coef1)//Enters the elements of coef array
{
coef[t_no]=coef1;
t_no++;
no++;
}
float extract_data(int x)//Extract the elements of coef array
{
return coef[x];
}

void putdata()//Prints the contents of coef array
{
cout<<"  [";
for(int i=0;i>n1;
if(n1>30)
{
cout<<"INVALID INPUT!!";
getch();
break;
}
cout<<"Coefficients of the first vector:-"<>coef1;
v1.getdata(coef1);
no1+=1;
}
no1=0;
}

if(temp2==1)
{
cout<<"Please enter the Dimension for the Second vector:";
cin>>n2;
if(n2>30)
{
cout<<"Dimension is too big!!";
getch();
break;
}

cout<<"Coefficients of the Second vector:-"<>coef1;
v2.getdata(coef1);
no1+=1;
}
no1=0;
}

clrscr();
cout<>ch;
clrscr();
cout<>ch1;
if(ch1==1)
{
temp1=1;
temp2=0;
continue;
}
else if(ch1==2)
{
temp1=0;
temp2=1;
continue;
}
else
{
cout<-1)
{
sum=sum+v3.extract_data(xx-1);
xx--;
}
cout<3)
{
cout<>multi;

cout<>ch1;
if(ch1==1)
{
v1.scalar_multi(multi);
temp1=0;
temp2=0;
continue;
}
else if(ch1==2)
{
v2.scalar_multi(multi);
temp1=0;
temp2=0;
continue;
}
else
{
cout<(n1+n2)) cout<

No responses yet

Leave a Reply
Your CommentName
Email
Website

```