Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Producing tables and outputting data to a text file :(
New Posts  All Forums:Forum Nav:

Producing tables and outputting data to a text file :(

post #1 of 11
Thread Starter 
I'm stumped. Worked on this assignment with my friend over Skype for a good 6 hours yesterday and I just can't focus right now.

1- I need ideas on how I can turn the results of mathematical algorithm that computes an approximation for Sin(x) and a bunch of variables along with it into a table.

2- I need someone to write me a snippet of code that I can paste into the [ int main() ] function so that I can output the generated table into a text file.

3- Here's how I need to make my table look:


Our teacher was away for the week and didn't explain file outputs. I've read up on file outputs, but they all output strings and I need to output a table that I generate out of calculated values.


-----------------------------------

P.S: Also, at this point (deadline is in 3 hours and 40 minutes), I'm willing to offer compensation to whoever helps me finish this damn project. I don't want you to write my home work. I already have, I'm just completely stumped at how to do the next part and don't have enough time to learn it as I go, like my teacher wants.




Guys helped me out with the tables. Just need pointers on how to output the table from a multi-function program into the txt file and not just strings.

Scroll down to the last post.

Sample file: https://www.courses.psu.edu/aersp/ae...file_help.html
Edited by MeeMoo220 - 3/19/11 at 1:07pm
post #2 of 11
It's been many years since I've coded C++, so forgive me if this is a "duh" reply:

I/O is of course done with things like inData.open() and outData.open(), inData >>, outData <<, just like you'd use a cout function.

Everything I've seen with tables is just written with a simple for loop to get it to be pretty. I stumbled across your question and I'm kind of challenging myself with trying to find a piece of code for you.

More to come. :-)
Rosemari
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ 4.0 GHz|1.26250v Asus P6T deluxe V2 XFX AMD Radeon 6950 2GB RF 12 GB G.Skill DDR3 1600 
Hard DriveOSMonitorPower
WD 1 TB Caviar Black 7200 RPM Win 7 64-bit Asus 24" 1080p Corsair TX 750W 
Case
Thermaltake Xaser VI 
  hide details  
Reply
Rosemari
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ 4.0 GHz|1.26250v Asus P6T deluxe V2 XFX AMD Radeon 6950 2GB RF 12 GB G.Skill DDR3 1600 
Hard DriveOSMonitorPower
WD 1 TB Caviar Black 7200 RPM Win 7 64-bit Asus 24" 1080p Corsair TX 750W 
Case
Thermaltake Xaser VI 
  hide details  
Reply
post #3 of 11
Well the easiest way would be to make two strings.

string header = "X sin(X) sin(2) error(2) sine(10) error(10)"
string separator = "================================================= ==="

Then, let's say you stored all your values in an array, then what I think you need is a for loop that looks like this

outFile << header << "\
";
outFile << separator << "\
";
for( int i = 0, i <= array.size()-6, i = i + 6){

outFile<<array[0+i]<<"\"<<array[1+i]>>"\"<<array[2+i]<<"\"<<array[3+i]<<"\"<<array[4+i]<<"\"<<array[5+i]<< endl;
}

Now I am not behind my laptop with all my coding stuff on it, so look at this more as psuedocode than actual code.

If the above works, then all you need to read in the file is to just read it in line by line until you reach eof.
Edited by joemaniaci - 3/19/11 at 12:10pm
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
post #4 of 11
Article on Output Formatting

Writing Formatted Output to a File

I was going to put in my two cents, but joemaniaci seems to have it covered more than I!
Rosemari
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ 4.0 GHz|1.26250v Asus P6T deluxe V2 XFX AMD Radeon 6950 2GB RF 12 GB G.Skill DDR3 1600 
Hard DriveOSMonitorPower
WD 1 TB Caviar Black 7200 RPM Win 7 64-bit Asus 24" 1080p Corsair TX 750W 
Case
Thermaltake Xaser VI 
  hide details  
Reply
Rosemari
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ 4.0 GHz|1.26250v Asus P6T deluxe V2 XFX AMD Radeon 6950 2GB RF 12 GB G.Skill DDR3 1600 
Hard DriveOSMonitorPower
WD 1 TB Caviar Black 7200 RPM Win 7 64-bit Asus 24" 1080p Corsair TX 750W 
Case
Thermaltake Xaser VI 
  hide details  
Reply
post #5 of 11
Thread Starter 
Quote:
Originally Posted by Aeschylus View Post
It's been many years since I've coded C++, so forgive me if this is a "duh" reply:

I/O is of course done with things like inData.open() and outData.open(), inData >>, outData <<, just like you'd use a cout function.

Everything I've seen with tables is just written with a simple for loop to get it to be pretty. I stumbled across your question and I'm kind of challenging myself with trying to find a piece of code for you.

More to come. :-)
Quote:
Originally Posted by joemaniaci View Post
Now I am not behind my laptop with all my coding stuff on it, so look at this more as psuedocode than actual code.

If the above works, then all you need to read in the file is to just read it in line by line until you reach eof.
Quote:
Originally Posted by Aeschylus View Post
Article on Output Formatting

Writing Formatted Output to a File

I was going to put in my two cents, but joemaniaci seems to have it covered more than I!

You guys are awesome. The table worked fine with all the commands on that second link. I used the set.precision and width stuff but needed the setw(N). That didn't work, but I remembered #include <iomanip>.

Just gotta figure out how to implement that outputfile stuff onto my program now! reps incoming. God bless u all!
post #6 of 11
Thread Starter 
Erm, sleight hiccup. I can't get the program to output my stuff into the txt file. I copied and pasted the sample code on the 2nd link into my program to test it out and see if I can figure out how it works, and so far, all I can make it do is store strings into the txt file.

Also, my program is a multi-function one. Nothing too complicated, but still, the implementation is extremely puzzling to me.

Anyone care to take a look and gimme some pointers?

Code:
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;

double sine( double x, double tolerance, int limit, int nTerms ){
       double term;
       double sum; 
       int i;
       if(limit<0){
                  nTerms = 0; 
                  return 0.0;
                  }
      term = x;
      sum = term;
      for(i = 1; i <= limit; i++){
            if( fabs(term) <= tolerance ){
                break;
                }
            term = (-term * pow(x,2))/(2*i*(2*i+1));
            sum += term;
            nTerms = i;
            }
      return sum; 
      }
      
int sineTable( double minX, double maxX, int nRows, double tolerance, int nTerms1,
int nTerms2, int limit){
    double sin1, sin2;
    double actValue, error1, error2; 
    int j;
    double temp;
    double diff;
    diff = (maxX - minX)/(nRows - 1);
    temp = minX;
    for( temp ; temp <= maxX; temp = temp+diff){
          sin1 = sine(temp, tolerance, limit, nTerms1);
          sin2 = sine(temp, tolerance, limit, nTerms2);
          actValue = sin(temp);
          error1 = actValue - sin1;
          error2 = actValue - sin2;
          cout.precision(4);
          cout.width(9);cout<< temp << "   " << actValue << "   "  << sin1 << "   "  << error1 << "   "  << sin2 << "   "  << error2 << "   " << "\
"; 
          }
          }
          
              
int main(void){
    double xMax, xMin; 
    double tolerance;
    int nTerms1, nTerms2;
    int limit;
    int rows;
    cin>> xMax;
    cin>> xMin;
    cin>> tolerance; 
    cin>> limit;
    cin>> rows;
    cout<<"        X    sin(x)    sine(2)   error(2)    sine(10)   error(10)" << "\
";
    cout.precision(4);
    cout << showpoint;
    cout << showpos;
    cout << setw(5);
    cout<<sineTable(xMin, xMax, rows, tolerance, nTerms1, nTerms2, limit); 
    system("pause");    
}


Test values for input:

3
-3
0.5
5
6
post #7 of 11
Thread Starter 
Here's what the assignment shows as an example:
post #8 of 11
I think those cout <<'s might need to be outfile <<'s

It looks to me like your void function is simply spitting out your data to the screen. I think (at first glance) all you need to do is change that to spitting it out to your text file
Rosemari
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ 4.0 GHz|1.26250v Asus P6T deluxe V2 XFX AMD Radeon 6950 2GB RF 12 GB G.Skill DDR3 1600 
Hard DriveOSMonitorPower
WD 1 TB Caviar Black 7200 RPM Win 7 64-bit Asus 24" 1080p Corsair TX 750W 
Case
Thermaltake Xaser VI 
  hide details  
Reply
Rosemari
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 @ 4.0 GHz|1.26250v Asus P6T deluxe V2 XFX AMD Radeon 6950 2GB RF 12 GB G.Skill DDR3 1600 
Hard DriveOSMonitorPower
WD 1 TB Caviar Black 7200 RPM Win 7 64-bit Asus 24" 1080p Corsair TX 750W 
Case
Thermaltake Xaser VI 
  hide details  
Reply
post #9 of 11
Thread Starter 
Quote:
Originally Posted by Aeschylus View Post
I think those cout <<'s might need to be outfile <<'s

It looks to me like your void function is simply spitting out your data to the screen. I think (at first glance) all you need to do is change that to spitting it out to your text file
I got it working thanks to u guys and DarkKnight512 . Thanks a bunch!
post #10 of 11
Simply the code with some printf and fprintf calls. That would take out 5-10 lines of code.
Labor of Love
(14 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 5.0 GHz/1.42v ASUS P8P67 Pro MSI GTX 670 OC 20 GB/1600/7-7-7-24 
Hard DriveHard DriveOptical DriveOS
120GB Vertex SSD Western Digital 1TB None! Ubuntu 12.04/Win8/OSX 10.8.2 
MonitorKeyboardPowerCase
27" 1440p X-Star OCN Mechanical - MX Browns Seasonic X-560 800D w/RX480 top mounted 
Mouse
Logitech USB Generic 
  hide details  
Reply
Labor of Love
(14 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 5.0 GHz/1.42v ASUS P8P67 Pro MSI GTX 670 OC 20 GB/1600/7-7-7-24 
Hard DriveHard DriveOptical DriveOS
120GB Vertex SSD Western Digital 1TB None! Ubuntu 12.04/Win8/OSX 10.8.2 
MonitorKeyboardPowerCase
27" 1440p X-Star OCN Mechanical - MX Browns Seasonic X-560 800D w/RX480 top mounted 
Mouse
Logitech USB Generic 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Producing tables and outputting data to a text file :(