-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn.cpp
More file actions
114 lines (98 loc) · 1.58 KB
/
Copy pathn.cpp
File metadata and controls
114 lines (98 loc) · 1.58 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
using namespace std;
struct element
{
int data;
int index;
element* next;
element(int ind,int val, element* e)
{
data=val;
index=ind;
next = e;
}
};
element* A = new element(0,0,NULL);
element* B = new element(0,0,NULL);
//int size =0;
void product(element* A,element* B)
{
element* ahead = A;
element* bhead = B;
int prod=0;
while(A!=0 && B!=0)
{
/**if(A->index== 0 && A->data==0)
A = ahead;
if(B->index == 0 && B->data==0)
B = bhead;
*/
if(A->index == B->index)
{
prod = prod + (A->data*B->data);
A = A->next;
B = B->next;
}
else if(A->index > B->index)
A = A->next;
else
B = B->next;
}
cout<<prod<<endl;
}
void print(element* head)
{
element* it = head; // it means iterator, visit all items one by one.
while (it != NULL)
{
cout << it->index << " "<< it->data << endl;
it = it->next;
}
}
element* add(element* head,int index,int data)
{
element* e = new element(index,data,NULL); // dynamically create anew struct item
if(head==NULL)
head=e;
else
{
e->next = head;
head = e;
}
return head;
/**
element* t = head;
while(t->next!=0)
{
t = t->next;
t->next = e;
}
return head;
*/
}
int main()
{
// unsigned short n =5;
int a_d,b_d,a_i,b_i;
cout<<"Enter nonzero elements in A"<<endl;
cout<<"Index val"<<endl;
while(a_i!=-1 && a_d!=-1)
{
cin>>a_i>>a_d;
if(a_i!=-1 && a_d!=-1)
A = add(A,a_i,a_d);
}
print(A);
cout<<"Enter nonzero elements in B"<<endl;
cout<<"Index val"<<endl;
while(b_i!=-1 && b_d!=-1)
{
cin>>b_i>>b_d;
if(b_i!=-1 && b_d!=-1)
B = add(B,b_i,b_d);
}
print(B);
//finding the dot product of A and B
cout<<"The dot product of A and B is "<< endl;
product(A,B);
}