Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Chris Christoff
- // Current Build: 4.2.3
- // Last build time: 8:55 A.M/ 6/12/2012
- #include "assign5.h"
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- void QuickTest(double,double,double);
- int main(int argc, char *argv[])
- {
- if(argc=1)
- {
- cout << "Execution: prog_name epsilon"<<endl;
- exit(5); //execution failure, no epsilon defined
- }
- else
- {
- int i = 0; //counter for roots array
- double epsilon = atof(argv[1]); //epsilon
- double xRight = -9.0; //define right x interval
- double xLeft; //declare left x interval
- double root; //declare temporary variable to store roots
- bool error; //declare boolean for bisect function
- quadratic qTest; //new quadratic object
- quadratic::fStruct fVars; //new fStruct
- quadratic::rootStruct rootStruct; //new rootStruct
- fVars = qTest.readCoeffs();
- double a=fVars.coeffs[0];
- double b=fVars.coeffs[1];
- double c=fVars.coeffs[2];
- if (a==0.0)
- {
- QuickTest(a,b,c);
- }
- else{
- //do nothing
- }
- if(fVars.coeffs[0]==0.0) //case for coefficient a=0 meaning equation is linear
- {
- while(xRight <= 10.0) //loop for iterations of bisect function
- {
- xLeft = xRight - 1.0; //set left x interval, use 1.0 increments for 20 total iterations until no roots found
- if ((root = qTest.bisect(xLeft, xRight, epsilon, fVars, error))!=-999.0) //see if bisect function returns error value or root value
- break; //break loop if not error value
- xRight += 1.0; //increment x right interval
- }
- qTest.linear(fVars, root); //call special function for linear equation, print to screen and to file.
- }
- else //case for regular quadratic equation
- {
- while(xRight <= 10.0) //loop for iterations of bisect function
- {
- xLeft = xRight - 1.0; //set left x interval, use 1.0 increments for 20 total iterations until no roots found or left root found
- if ((root = qTest.bisect(xLeft, xRight, epsilon, fVars, error))!=-999.0) //see if bisect function returns error value or root value
- {
- rootStruct.roots[i] = root; //set first root equal to approximated root
- i++; //increase array index
- break; //break loop if not error value
- }
- xRight += 1.0; //increment x right interval
- }
- if(i>0) //this should only execute if first root is found
- {
- xLeft = root+epsilon; //set x left interval equal to first root + error tolerance
- xRight = 10.0; //set x right interval to max value
- if ((root = qTest.bisect(xLeft, xRight, epsilon, fVars, error))!=-999.0) //see if bisect function returns error value or root value
- {
- rootStruct.roots[i] = root; //set second root equal to approximated root
- i++; //increase array index for error checking
- }
- }
- if(i<2) //make sure i has been increased at least twice - 2 roots
- qTest.toScreen(fVars); //show that no roots were found
- else //i has been increased at least twice
- qTest.toFile(fVars, rootStruct); //print roots to file
- }
- return 0; //execution success
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment