![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
Hi everyone,
I have written a MC numerical integration program for solving indefinite integrals. The problem I am facing is trying to dump the output of the numbers generated to a file so I can plot it using Excel or other spreadsheet. I have a function for the output but it doesn't seem to work properly. It only gives me one set of number. I need the entire set of numbers generated (>10000) by the program to analyze and plot in a spreadsheet. Also is there a particular format I should output the file so that it can be easily read by any spreadsheet program such as (.csv) format?
#include <iostream>
#include <fstream>
#include <cstdlib> // random function rand() and srand()
#include <cmath> // pow() and sqrt() functions
using namespace std;
float fileout(double resultF4, double resultErr4);
main() {
double x, func = 0.0, func2 = 0.0, resultF, errorF, resultErr;
int itry, ntry;
cout << "Input the number of MC trials" << "\n";
cin >> ntry;
// seed rand() function with void srand( (unsigned) time((int *)0) ); dependant on time program run
srand((unsigned)time((long *)0));
for (itry=0; itry<ntry; itry++) {
// random generate to largest number (2^31 - 1) RAND_MAX
x = rand()/(float)RAND_MAX;
func = func + pow(x,2); // function to be evaluated is parabola of x^2
func2 = func2 + pow(x,4);
errorF = sqrt(func - func2);
resultF = func/ntry;
resultErr = errorF/ntry;
fileout(resultF, resultErr);
}
}
float fileout(double resultF2, double resultErr2)
{
ofstream out("output.dat");
if (!out) {
cout << "Cannot open file." << "\n";
return 1;
}
out << "Function = " << resultF2 << " " << " Error = " << resultErr2 << "\n";
out.close();
}I am using a Debian Linux with GNU C/C++. Anyone can help I really appreciate it. Thank you. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Does it work with the following?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
// prototypes
int fileout(double resultF4, double resultErr4);
int main()
{
double x, func = 0.0, func2 = 0.0, resultF, errorF, resultErr;
int itry, ntry;
cout << "Input the number of MC trials:" << endl;
cin >> ntry;
srand((unsigned)time(0));
for (itry=0; itry<ntry; itry++)
{
x = rand()/(float)RAND_MAX;
func = func + pow(x,2);
func2 = func2 + pow(x,4);
errorF = sqrt(func - func2);
resultF = func/ntry;
resultErr = errorF/ntry;
fileout(resultF, resultErr);
}
return 0;
}
int fileout(double resultF2, double resultErr2)
{
ofstream out("output.dat");
if (!out)
{
cout << "Cannot open file." << endl;
return 1;
}
out << "Function = " << resultF2 << " " << " Error = " << resultErr2 << endl;
out.close();
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Ohh I reread what you said, and if it's supposed to outputting it all, then you might want to put it in a loop instead of just once doing:
out << "Function = " << resultF2 << " " << " Error = " << resultErr2 << endl;
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
Thanks nnxion for the tip, putting in the for loop around the out statement works and prints out all the data into the output file.
However, there is a problem, all the data is identically the same (it is supposed to be different), it seems that when the function "fileout" is called each time during the for loop in the main section, it passes the same data to the subroutine. Do you or anyone else know how to fix this part. Really appreciate it. Thank you. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2005
Posts: 12
Rep Power: 0
![]() |
the way you have it set up right now is that it opens the file every time the output function is called and then rewrites the file with the new data.
you can either open the file and append the new data to the end, or open the file once and write a vector or list of data to it after all the calculations are done. ...or you could open the file before your for loop and close it after, but it's generally not good practice to have your file get opened outside of your actual output method |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|