chriscct1

File with main

Jun 13th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. // Author: Chris Christoff
  2. // Current Build: 4.2.3
  3. // Last build time: 8:55 A.M/ 6/12/2012
  4.  
  5. #include "assign5.h"
  6. #include <iostream>
  7. #include <stdlib.h>
  8. using namespace std;
  9.  
  10. void QuickTest(double,double,double);
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     if(argc=1)
  15.     {
  16.         cout << "Execution: prog_name epsilon"<<endl;
  17.         exit(5); //execution failure, no epsilon defined
  18.     }
  19.  
  20.     else
  21.     {
  22.         int i = 0;                          //counter for roots array
  23.         double epsilon = atof(argv[1]);     //epsilon
  24.         double xRight = -9.0;               //define right x interval
  25.         double xLeft;                       //declare left x interval
  26.         double root;                        //declare temporary variable to store roots
  27.         bool error;                         //declare boolean for bisect function
  28.         quadratic qTest;                    //new quadratic object
  29.         quadratic::fStruct fVars;           //new fStruct
  30.         quadratic::rootStruct rootStruct;   //new rootStruct
  31.  
  32.         fVars = qTest.readCoeffs();
  33.         double a=fVars.coeffs[0];
  34.         double b=fVars.coeffs[1];
  35.         double c=fVars.coeffs[2];
  36.         if (a==0.0)
  37.         {
  38.             QuickTest(a,b,c);
  39.         }
  40.         else{
  41.         //do nothing
  42.         }
  43.         if(fVars.coeffs[0]==0.0)                                                            //case for coefficient a=0 meaning equation is linear
  44.         {
  45.             while(xRight <= 10.0)                                                           //loop for iterations of bisect function
  46.             {
  47.                 xLeft = xRight - 1.0;                                                        //set left x interval, use 1.0 increments for 20 total iterations until no roots found
  48.  
  49.                 if ((root = qTest.bisect(xLeft, xRight, epsilon, fVars, error))!=-999.0)    //see if bisect function returns error value or root value
  50.                     break;                                                                  //break loop if not error value
  51.  
  52.                 xRight += 1.0;                                                              //increment x right interval
  53.             }
  54.             qTest.linear(fVars, root);                                                      //call special function for linear equation, print to screen and to file.
  55.         }
  56.         else                                                                                //case for regular quadratic equation
  57.         {
  58.             while(xRight <= 10.0)                                                               //loop for iterations of bisect function
  59.             {
  60.                 xLeft = xRight - 1.0;                                                           //set left x interval, use 1.0 increments for 20 total iterations until no roots found or left root found
  61.  
  62.                 if ((root = qTest.bisect(xLeft, xRight, epsilon, fVars, error))!=-999.0)        //see if bisect function returns error value or root value
  63.                 {
  64.                     rootStruct.roots[i] = root;                                                 //set first root equal to approximated root
  65.                     i++;                                                                        //increase array index
  66.                     break;                                                                      //break loop if not error value
  67.                 }
  68.  
  69.                 xRight += 1.0;                                                                  //increment x right interval
  70.             }
  71.  
  72.             if(i>0)                                                                             //this should only execute if first root is found
  73.             {
  74.                 xLeft = root+epsilon;                                                           //set x left interval equal to first root + error tolerance
  75.                 xRight = 10.0;                                                                  //set x right interval to max value
  76.  
  77.                 if ((root = qTest.bisect(xLeft, xRight, epsilon, fVars, error))!=-999.0)        //see if bisect function returns error value or root value
  78.                 {
  79.                     rootStruct.roots[i] = root;                                                 //set second root equal to approximated root
  80.                     i++;                                                                        //increase array index for error checking
  81.                 }
  82.             }
  83.  
  84.             if(i<2)                                 //make sure i has been increased at least twice - 2 roots
  85.                 qTest.toScreen(fVars);              //show that no roots were found
  86.             else                                    //i has been increased at least twice
  87.                 qTest.toFile(fVars, rootStruct);    //print roots to file
  88.         }
  89.         return 0; //execution success
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment