-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_Array.cpp
More file actions
47 lines (40 loc) · 732 Bytes
/
Copy pathvector_Array.cpp
File metadata and controls
47 lines (40 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// DOT PRODUCT OF VECTORS USING ARRAY STRUCTURES
#include<iostream>
using namespace std;
struct vector
{
int index;
int val;
};
int main()
{
vector A[10];
vector B[10];
int i=0;
int j=0;
int n=5;
int product;
cout<<"enter the non zero elements into A:"<<endl;
cout<<"index val"<<endl;
for (int i=0;i<n;i++)
cin>>A[i].index>>A[i].val;
cout<<"enter the non zero elements into B:"<<endl;
cout<<"index val"<<endl;
for (int j=0;j<n-1;j++)
cin>>B[j].index>>B[j].val;
//dot product
while (A[i].index!= -1 && B[j].index!= -1)
{
if(A[i].index==B[j].index)
{
product+=A[i].val * B[j].val;
i++;
j++;
}
else if (A[i].index<B[j].index)
i++;
else
j++;
}
cout<<"The dot product of A and B is "<<product;
}