Wikia

C Plus Plus

Watchlist Recent changes

Another C++ Program

/*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;
}

Pages on C Plus Plus

Add a Page
15pages on
this wiki
Advertisement | Your ad here

Latest Photos

Add a Photo
3photos on this wiki
See more >

Recent Wiki Activity

See more >

Around Wikia's network

Random Wiki