-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_array_17.cpp
84 lines (78 loc) · 1.91 KB
/
func_array_17.cpp
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
#include <iostream>
int input(int[], int, char);
void display(int[], int);
int multiply(int[], int[], int[], int);
using namespace std;
int main()
{
int m;
cout << "Enter the size of the array: ";
cin >> m;
int a[m], b[m], result[m];
char arr1 = 'a';
char arr2 = 'b';
int size = sizeof(a) / sizeof(a[0]);
cout << "Enter the elements of 1st Matrix: " << endl;
input(a, size, arr1);
cout << "Displaying the Matrix: " << endl;
display(a, size);
cout << "Enter the elements of 2nd Matrix: " << endl;
input(b, size, arr2);
cout << "Displaying the Matrix: " << endl;
display(b, size);
int size2 =multiply(a, b, result, size);
cout << "Displaying the Multplication of 2 Square 1-D Matrix: " << endl;
display(result, size2);
return 0;
}
int input(int a[], int size, char arr)
{
if (arr == 'a')
{
for (int i = 0; i < size; i++)
{
cout << arr << "[" << i << "] = ";
cin >> a[i];
}
}
else
{
for (int i = 0; i < size; i++)
{
cout << arr << "[" << i << "] = ";
cin >> a[i];
}
}
cout << endl;
return a[size];
}
void display(int a[], int size)
{
cout << "[";
for (int i = 0; i < size; i++)
{
cout << a[i] << " ";
}
cout << "]";
cout << endl;
}
int multiply(int a[], int b[], int result[], int size)
{
int sum = 0, count = 0, temp;
for (int i = 0; i < size; i++)
{
sum = sum + (a[i] * b[i]);
}
temp= sum;
while(temp!=0){
temp=temp/10;
count++;
}
result[temp] = sum;
return temp+1;
}
/*******************************
* result[0] stores the result of multiplication, hence we return temp+1
* As loop will run only once.
*i.e i = 0 and i < 1
* ********************/