Fandom

C Plus Plus

Another C++ Program

16pages on
this wiki
Add New Page
Talk0 Share
/*Code compiled using Dev C++ from bloodshed.*/
 
 #include <fstream>
 #include <iostream>
 #include <iomanip>
 using namespace std;
 double f(double x)   //Function 
 {double func = (0.6*x*x) + 2; 
 return func;}
 int main()
 {
  double dx, a, b, n, m, dm;            //Variable declarations.
  double lram, rram, mram, trap;
  lram = rram = mram = trap = 0;
  cout<<"Program 14. Rectangular Approximation Methods. \n\n";
  char start, choice;
  cout<<"To start program, enter y, to end enter n: ";
  cin>>start;
 while(start=='y')
 {
 cout<<"Enter the lower limit (a), the upper limit (b) and divisions (n): ";
 cin>>a>>b>>n;
 dx = (b-a)/n;      //Delta x.
 for(m=0; m<n; m++){//Beginning of for loop.
 dm = (m+1)*dx;                           
 lram = lram + dx*f(a + m*dx); //Area using left rectangular approximation formula.
 rram = rram + dx*f(a + dm);  //Area using right rectangular approximation formula.
 mram = mram + dx*f(a+0.5*dx+m*dx); //Area using midpoint approximation formula.
 trap = trap + dx/2.0*(f(a+m*dx)+f(a+dm));} //Trapezodial approximation formula.
          cout<<"For a = "<<a<<", b = "<<b<<" and n = "<<n<<endl;
          cout<<"LRAM is = "<<lram<<endl;
          cout<<"MRAM is = "<<mram<<endl;
          cout<<"RRAM is = "<<rram<<endl;
          cout<<"TRAP is = "<<trap<<"\n\n";
 cout<<"Would you like to continue? (y/n): ";
 cin>>choice;
 if(choice=='y'){
 lram = rram = mram = trap = 0.0;}  //Sets all equations to zero before loop.
 else
 return 0;
 }					//End of while loop.
 if(start == 'n')    //Quits program if user inputs character n at beginning.
 return 0;
 else
 system("PAUSE");
 return 0;
 }

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.