post #11 of 11
Quote:
Originally Posted by MeeMoo220 View Post
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
Bug 01 - The variables, nTerms1 and nTerms2 are uninitialized.
    
CPUMotherboardGraphicsRAM
Intel® Core™ i5-4590 Processor  Asus Maximus VII Ranger Z97 MSI GTX 970 4G Corsair Vengeance® Pro Series - 8GB (2 x 4GB) D... 
Hard DriveOptical DriveCoolingOS
Seagate 250GB SATA drive 250GB SSD Corsair H105 Hydro Series Extreme Performance L... Windows 10 Pro x64 
OSMonitorPowerCase
Ubuntu 16.04 Samsung 2232GW Super Flower SF-750P14XE(HX) Golden Green HX 75... Coolermaster CM690III 
Mouse Pad
Steelseries mousepad 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel® Core™ i5-4590 Processor  Asus Maximus VII Ranger Z97 MSI GTX 970 4G Corsair Vengeance® Pro Series - 8GB (2 x 4GB) D... 
Hard DriveOptical DriveCoolingOS
Seagate 250GB SATA drive 250GB SSD Corsair H105 Hydro Series Extreme Performance L... Windows 10 Pro x64 
OSMonitorPowerCase
Ubuntu 16.04 Samsung 2232GW Super Flower SF-750P14XE(HX) Golden Green HX 75... Coolermaster CM690III 
Mouse Pad
Steelseries mousepad 
  hide details  
Reply