Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F78932412
ch-03-ex-01-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, 20:20
Size
1 KB
Mime Type
text/x-c
Expires
Sun, Aug 25, 20:20 (2 d)
Engine
blob
Format
Raw Data
Handle
20124117
Attached To
R9360 PCSC_git
ch-03-ex-01-solution.cpp
View Options
/*
* chapter-03-exercise-01.cpp
*
* Print to file with a given precision
*
* Created on: Sep 2, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <fstream>
#include <iostream>
int
main
(
int
argc
,
char
*
argv
[])
{
double
x
[
4
]
=
{
0.0
,
1.0
,
1.0
,
0.0
};
double
y
[
4
]
=
{
0.0
,
0.0
,
1.0
,
1.0
};
std
::
ofstream
output_file
(
"x_and_y.dat"
);
// Check that file is properly openened.
if
(
!
output_file
.
is_open
())
{
std
::
cout
<<
"Error opening output file."
<<
std
::
endl
;
return
1
;
}
// Output in scientific format with 10 significant figures
// and always showing sign
output_file
.
setf
(
std
::
ios
::
scientific
);
output_file
.
setf
(
std
::
ios
::
showpos
);
output_file
.
precision
(
10
);
// Write x vector ending with a new line
for
(
int
i
=
0
;
i
<
4
;
++
i
)
{
output_file
<<
x
[
i
]
<<
" "
;
}
output_file
<<
"
\n
"
;
// Flush after writing vector x
output_file
.
flush
();
// Do the same for y vector
for
(
int
i
=
0
;
i
<
4
;
++
i
)
{
output_file
<<
y
[
i
]
<<
" "
;
}
output_file
<<
"
\n
"
;
// Flush after writing vector y
output_file
.
flush
();
// Close the file when finished
output_file
.
close
();
return
0
;
}
Event Timeline
Log In to Comment