Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F78919758
ch-01-ex-04-solution.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Aug 23, 18:25
Size
1 KB
Mime Type
text/x-c
Expires
Sun, Aug 25, 18:25 (2 d)
Engine
blob
Format
Raw Data
Handle
20122086
Attached To
R9360 PCSC_git
ch-01-ex-04-solution.cpp
View Options
/*
* exercise04.cpp
*
* Declared four 2x2 floating point matrices A, B, C, D and computes:
* C = A + B and D = AB
*
* Created on: Jul 26, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <iostream>
int
main
(
int
argc
,
char
*
argv
[])
{
// Declare A and assign values to it
double
A
[
2
][
2
];
A
[
0
][
0
]
=
0.5
;
A
[
0
][
1
]
=
2.0
;
A
[
1
][
0
]
=
1.5
;
A
[
1
][
1
]
=
4.0
;
// Declare B and initialize it at the same time
double
B
[
2
][
2
]
=
{{
5.0
,
2.5
},
{
3.5
,
7.5
}};
double
C
[
2
][
2
];
double
D
[
2
][
2
];
// Compute the values of C
C
[
0
][
0
]
=
A
[
0
][
0
]
+
B
[
0
][
0
];
C
[
0
][
1
]
=
A
[
0
][
1
]
+
B
[
0
][
1
];
C
[
1
][
0
]
=
A
[
1
][
0
]
+
B
[
1
][
0
];
C
[
1
][
1
]
=
A
[
1
][
1
]
+
B
[
1
][
1
];
// Compute the values of D
D
[
0
][
0
]
=
A
[
0
][
0
]
*
B
[
0
][
0
]
+
A
[
0
][
1
]
*
B
[
1
][
0
];
D
[
0
][
1
]
=
A
[
0
][
0
]
*
B
[
0
][
1
]
+
A
[
0
][
1
]
*
B
[
1
][
1
];
D
[
1
][
0
]
=
A
[
1
][
0
]
*
B
[
0
][
0
]
+
A
[
1
][
1
]
*
B
[
1
][
0
];
D
[
1
][
1
]
=
A
[
1
][
0
]
*
B
[
0
][
1
]
+
A
[
1
][
1
]
*
B
[
1
][
1
];
// Print the results
std
::
cout
<<
"The matrix C is: "
<<
std
::
endl
<<
C
[
0
][
0
]
<<
" "
<<
C
[
0
][
1
]
<<
std
::
endl
<<
C
[
1
][
0
]
<<
" "
<<
C
[
1
][
1
]
<<
std
::
endl
;
std
::
cout
<<
"The matrix D is: "
<<
std
::
endl
<<
D
[
0
][
0
]
<<
" "
<<
D
[
0
][
1
]
<<
std
::
endl
<<
D
[
1
][
0
]
<<
" "
<<
D
[
1
][
1
]
<<
std
::
endl
;
return
0
;
}
Event Timeline
Log In to Comment