Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jan 27th, 2006, 9: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
 

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 4:11 AM.

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