Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp_introduction
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cp1_ws19
cpp_introduction
Commits
e249ebe4
Commit
e249ebe4
authored
5 years ago
by
Kevin Höllring
Browse files
Options
Downloads
Patches
Plain Diff
Add sample with custom class and more complex operations
parent
9f574587
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/matrix.h
+65
-0
65 additions, 0 deletions
src/matrix.h
src/sample_4.cpp
+68
-0
68 additions, 0 deletions
src/sample_4.cpp
with
133 additions
and
0 deletions
src/matrix.h
0 → 100644
+
65
−
0
View file @
e249ebe4
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include
<vector>
#include
<iostream>
namespace
magic
{
using
std
::
vector
;
// This is a custom Matrix type to allow for matrix multiplication
class
Matrix
{
public:
Matrix
(
uint8_t
rows
,
uint8_t
columns
)
{
this
->
_rows
=
rows
;
this
->
_columns
=
columns
;
this
->
entries
.
assign
(
rows
,
vector
<
int32_t
>
(
columns
,
0
));
}
void
setValue
(
uint8_t
i
,
uint8_t
j
,
int32_t
value
)
{
entries
[
i
][
j
]
=
value
;
}
int32_t
getValue
(
uint8_t
i
,
uint8_t
j
)
{
return
entries
[
i
][
j
];
}
// Calculate the matrix product with another matrix
Matrix
multiply
(
Matrix
&
other
)
{
// Create the result matrix
Matrix
result
(
this
->
_rows
,
other
.
_columns
);
// Calculate all entries in the resulting matrix
for
(
uint8_t
r
=
0
;
r
<
this
->
_rows
;
r
++
)
{
for
(
uint8_t
c
=
0
;
c
<
other
.
_columns
;
c
++
)
{
for
(
uint8_t
link
=
0
;
link
<
this
->
_columns
;
link
++
)
{
int32_t
res
=
result
.
getValue
(
r
,
c
)
+
this
->
getValue
(
r
,
link
)
*
other
.
getValue
(
link
,
c
);
result
.
setValue
(
r
,
c
,
res
);
}
}
}
// Return the resulting matrix
return
result
;
}
// Output the matrix to screen
void
print
()
{
using
std
::
cout
;
using
std
::
endl
;
for
(
uint8_t
r
=
0
;
r
<
this
->
_rows
;
r
++
)
{
for
(
uint8_t
c
=
0
;
c
<
this
->
_columns
;
c
++
)
{
cout
<<
entries
[
r
][
c
]
<<
'\t'
;
}
cout
<<
endl
;
}
}
private
:
uint8_t
_rows
;
uint8_t
_columns
;
vector
<
vector
<
int32_t
>>
entries
;
};
}
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/sample_4.cpp
0 → 100644
+
68
−
0
View file @
e249ebe4
#include
"matrix.h"
#include
<iostream>
// A simple program to multiply to matrices read in from std input
int
main
(
int
argc
,
char
**
argv
)
{
using
std
::
cin
;
using
std
::
cout
;
using
std
::
cerr
;
using
std
::
endl
;
uint8_t
rows1
,
columns1
;
cout
<<
"Number of rows of the first matrix"
<<
endl
;
cin
>>
rows1
;
cout
<<
"Number of columns of the first matrix"
<<
endl
;
cin
>>
columns1
;
// Import our custom Matrix type
using
magic
::
Matrix
;
Matrix
matrix_1
(
rows1
,
columns1
);
cout
<<
"Please enter the entries of the matrix in a row-after-row fashion with spaces separating subsequent entries"
<<
endl
;
for
(
uint8_t
r
=
0
;
r
<
rows1
;
r
++
)
{
for
(
uint8_t
c
=
0
;
c
<
columns1
;
c
++
)
{
int32_t
val
;
cin
>>
val
;
matrix_1
.
setValue
(
r
,
c
,
val
);
}
}
cout
<<
"Matrix 1 was read:"
<<
endl
;
matrix_1
.
print
();
uint8_t
rows2
,
columns2
;
cout
<<
"Number of rows of the second matrix"
<<
endl
;
cin
>>
rows2
;
if
(
rows2
!=
columns1
)
{
cerr
<<
"The number of rows in the second matrix has to equal the number of columns in the first matrix"
<<
endl
;
// Declare an error and stop execution
exit
(
1
);
}
cout
<<
"Number of columns of the second matrix"
<<
endl
;
cin
>>
columns2
;
Matrix
matrix_2
(
rows2
,
columns2
);
cout
<<
"Please enter the entries of the matrix in a row-after-row fashion with spaces separating subsequent entries"
<<
endl
;
for
(
uint8_t
r
=
0
;
r
<
rows2
;
r
++
)
{
for
(
uint8_t
c
=
0
;
c
<
columns2
;
c
++
)
{
int32_t
val
;
cin
>>
val
;
matrix_2
.
setValue
(
r
,
c
,
val
);
}
}
cout
<<
"Matrix 2 was read:"
<<
endl
;
matrix_2
.
print
();
Matrix
product
=
matrix_1
.
multiply
(
matrix_2
);
cout
<<
"M1 * M2 ="
<<
endl
;
product
.
print
();
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment