Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 27th, 2006, 8:33 AM   #1
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
Smile Numerical data output to a file

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.
can342man is offline   Reply With Quote
Old Jan 27th, 2006, 9:58 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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;
}
If not, you might want to flush it with sync(), but it should work with 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
nnxion is offline   Reply With Quote
Old Jan 27th, 2006, 11:11 AM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Jan 27th, 2006, 12:13 PM   #4
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
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.
can342man is offline   Reply With Quote
Old Jan 27th, 2006, 4:21 PM   #5
Jerkstore
Newbie
 
Join Date: Dec 2005
Posts: 12
Rep Power: 0 Jerkstore is on a distinguished road
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
Jerkstore is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC