In: Computer Science
2. A vector is an ordered collection of values in mathematics.
An array is a very straightforward way to implement a vector on a
computer. Two vectors are multiplied on an entry-by-entry basis,
e.g. (1, 2, 3) * (4, 5, 6) = (4, 10, 18).
Write a program that include the following functions.
void multi_vec (int v1[], int v2[], int v3[], int n);
int comp_vec(int v1[], int v2[], int n);
The multi_vec function multiplies vectors v1 and v2 and stores the
result in v3. n is the length of the vectors.
The comp_vec function compares v1 and v2, return 1 if vectors v1
and v2 are equal (their corresponding components are equal), and 0
otherwise. n is the length of the vectors.
In the main function, ask the user to enter the length of the
vectors, declare two arrays with the length, read in the values for
two vectors, and call the two functions to compute the
multiplication and comparison of them. The main function should
display the result.
Enter the length of the vectors: 5
Enter the first vector: 3 4 9 1 4
Enter the second vector: 5 7 2 6 8
Output:
The multiplication of the vectors is: 15 28 18 6 32
The vectors are not the same.
Hi, you can compile this code in Dev C++
#include<iostream>
using namespace std;
void multi_vec(int a[],int b[], int c[],int n)
{
int i;
for(i=0;i<n;i++)
{
c[i]=a[i]*b[i];
}
}
int comp_vec(int a[],int b[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return 1;
}
return 2;
}
int main()
{
int n,i;
cout<<"Enter the size of vector :";
cin>>n;
int a[n],b[n],c[n];
cout<<"Enter the first vector :";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the second vector :";
for(i=0;i<n;i++)
cin>>b[i];
multi_vec(a,b,c,n);
cout<<"The multiplication of vectors is :";
for(i=0;i<n;i++)
cout<<c[i]<<" ";
int same=comp_vec(a,b,n);
if(same==1)
cout<<"\nthe vectors are not same";
else cout<<"\nthe vectors are same";
}