-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr_func_eg_11.cpp
70 lines (67 loc) · 1.58 KB
/
ptr_func_eg_11.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
//Addition of two Matrix
#include <iostream>
int input(int [], int, char);
void display(int [], int);
int add(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);
add(a, b, result, size);
cout << "Displaying the Addition of 2 Square Matrix: " << endl;
display(result, size);
return 0;
}
int input(int a[], int size, char arr)
{
if (arr == 'a')
{
for (int i = 0; i < size; i++)
{
cout << "a[" << i << "]: ";
cin >> *(a + i);
}
}
else
{
for (int i = 0; i < size; i++)
{
cout << "b[" << 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 add(int arr1[], int arr2[], int res[], int size)
{
for (int i = 0; i < size; i++)
{
*(res + i) = *(arr1 + i) + *(arr2 + i);
}
return *(res + size);
}