-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr_func_eg_6.cpp
152 lines (130 loc) · 4.02 KB
/
ptr_func_eg_6.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
void rules();
int inputA(int*, int);
void displayA(int*, int);
int inputB(int*, int);
void displayB(int*, int);
void displayRes(int*, int, int);
int multiplication(int*, int*, int*, int, int);
using namespace std;
int main()
{
rules();
int m, n;
cout << "Enter row for Row Vector: " << endl;
cin >> m;
cout << "Enter columns for Matrix B: " << endl;
cin >> n;
int a[m][1], b[1][n], c[m][n];
cout << "Enter the elements of Column Vector: " << endl;
inputA((int *)a, m);
cout << "Enter the elements of Row Vector: " << endl;
inputB((int *)b, n);
cout << "Displaying the Matrix A(Column Vector): " << endl;
displayA((int *)a, m);
cout << "Displaying the Matrix B(Row Vector): " << endl;
displayB((int *)b, n);
multiplication((int *)a, (int *)b, (int *)c, m, n);
cout << "Displaying the Matrix C: " << endl;
displayRes((int *)c, m, n);
return 0;
}
int inputA(int *p, int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < 1; j++)
{
cout << "a[" << i << "][" << j << "] = ";
cin >> *(p + i * 1 + j);
}
}
cout << endl;
return p[rows * 1];
}
void displayA(int *p, int rows)
{
for (int i = 0; i < rows; i++)
{
cout << "|";
for (int j = 0; j < 1; j++)
{
cout << *(p + i * 1 + j) << " ";
}
cout << "|";
cout << endl;
}
}
int inputB(int *p, int cols)
{
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "b[" << i << "][" << j << "] = ";
cin >> *(p + i * cols + j);
}
}
cout << endl;
return p[1 * cols];
}
void displayB(int *p, int cols)
{
cout << "[";
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < cols; j++)
{
cout << *(p + i * cols + j) << " ";
}
}
cout << "]";
cout << endl;
}
void displayRes(int *p, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
cout << "|";
for (int j = 0; j < cols; j++)
{
cout << *(p + i * cols + j) << " ";
}
cout << "|";
cout << endl;
}
}
int multiplication(int *p, int *q, int *r, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
*(r + i * cols + j) = *(p + i * 1 + 0) * *(q + 0 * cols + j);
}
}
return r[rows * cols];
}
void rules()
{
cout << "\tRule: Matrix A of (m x n ) must be multiplying with Matrix B of (n x m ) . " << endl;
cout << "\tWhere m is the number of rows and n is the number of columns. " << endl;
cout << "\tHence Condition is : n(no. of columns) of Matrix A = n(no. of rows) of Matrix B." << endl;
cout << "\tTherefore,Column Vector can multiply with Row Vector i.e. (m x 1) x (1 x m) is always possible,"
<< "\n";
cout << "\tAs column of Column Vector = rows of Rows Vector = 1 ." << endl;
cout << "\tWhere as multiplication of Row Vector is only possible with Column Vector when," << endl;
cout << "\tColumns of Row Vector(m) = rows(m) of Column Vector." << endl;
cout << "\tAnd, Result = Singleton Matrix[where m= n = 1] when Row Vector[1xm] x Column Vector[mx1],"
<< "\n";
cout << "\tAs row of Matrix A = column of Matrix B= 1 producing 1 x 1 matrix"
<< "\n";
cout << "\tWhere as ,Column Vector(m x 1) x Row Vector(1 x n) produces m x n matrix as result,"
<< "\n";
cout << "\tWhere m= No. of Rows of Column Vector and n = No. of Columns in Row Vector "
<< "\n";
cout << "*********************************************************" << endl;
cout << "Column Vector x Row Vector [(m x 1) x (1 x n)] " << endl;
cout << " m1 (Column of Row Vector) and m2(Row of Column Vector) Must Be equal" << endl;
cout << "\n";
}