Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F90593447
ch-01-ex-03-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
Sun, Nov 3, 01:26
Size
1 KB
Mime Type
text/x-c
Expires
Tue, Nov 5, 01:26 (2 d)
Engine
blob
Format
Raw Data
Handle
22105355
Attached To
R9360 PCSC_git
ch-01-ex-03-solution.cpp
View Options
/*
* exercise-03.cpp
*
* Declares two vectors of floating point values, computes the scalar product
* and the Euclidean norm of both vectors
*
* Created on: Jul 26, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <iostream>
/*
* We are using the functions pow and sqrt from the C/C++ standard library.
* We need to include the "cmath" header
*/
#include <cmath>
int
main
(
int
argc
,
char
*
argv
[])
{
// Declare the vector v1 and assign values
double
v1
[
3
];
v1
[
0
]
=
1.0
;
v1
[
1
]
=
-
1.0
;
v1
[
2
]
=
2.0
;
// Declare the vector v2 and initialize it at the same time
double
v2
[
3
]
=
{
3.0
,
4.5
,
5.0
};
// Calculate the scalar product of the two vectors and print it on
// the screen
double
scalar_prod
=
v1
[
0
]
*
v2
[
0
]
+
v1
[
1
]
*
v2
[
1
]
+
v1
[
2
]
*
v2
[
2
];
std
::
cout
<<
"The scalar product is: "
<<
scalar_prod
<<
std
::
endl
;
// Calculate the Euclidean norm of the two vectors and print it
double
norm1
=
std
::
sqrt
(
v1
[
0
]
*
v1
[
0
]
+
v1
[
1
]
*
v1
[
1
]
+
v1
[
2
]
*
v1
[
2
]);
// Let's calculate the second norm using the pow function
double
norm2
=
std
::
sqrt
(
std
::
pow
(
v2
[
0
],
2
)
+
std
::
pow
(
v2
[
1
],
2
)
+
std
::
pow
(
v2
[
2
],
2
));
std
::
cout
<<
"The Euclidean norm of the first vector is: "
<<
norm1
<<
std
::
endl
<<
"The Euclidean norm of the second vector is: "
<<
norm2
<<
std
::
endl
;
return
0
;
}
Event Timeline
Log In to Comment