wind_data << timer << ‘ ‘ << windspeed << endl;
}
/* Close file and exit program. */
wind_data.close();
return 0;
}
/*——————————————————————–*/
/* (rand_float function from page 257) */
/*——————————————————————–*/
/*——————————————————————–*/
const int MIN_PROB = 0;
const int MAX_PROB = 1;
const int STORM = 1;
const int NO_STORM = 0;
const int MAX_DURATION = 300;
double rand_float(double a, double b);
int main()
/* Compute wind speeds. */
for (timer=START_TIME; timer<=STOP_TIME; timer+=DELTA_TIME)
{
if (storm_flag == STORM)
/* There is a storm, is it time to stop? */
if (storm_duration < MAX_DURATION)
storm_duration += DELTA_TIME;
else
storm_flag = NO_STORM;
else
/* No storm raging, is it time for another? */
}
/* 0.5% possiblity of encountering a small storm at each time step. */
/* The average wind speed is increased by 10 mph for a user-entered */
/* duration when a storm is encountered. */
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
const int DELTA_TIME = 10;
const int START_TIME = 0;
{
/* Define variables. */
double ave_wind=0.0, gust_min=0.0, gust_max=0.0, windspeed = 0.0;
int max_storm_length = 300;
int timer=START_TIME, storm_duration=0, storm_flag=NO_STORM;
ofstream wind_data;
{
else
/* No storm raging, is it time for another? */
if (rand_float(MIN_PROB,MAX_PROB) <= STORM_PROB)
{
storm_flag = STORM;
storm_duration = 0;
}
}
/* Close file and exit program. */
wind_data.close();
return 0;
}
/*——————————————————————–*/
/* (rand_float function from page 257) */
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter6_36 */
using namespace std;
const int DELTA_TIME = 10;
const int START_TIME = 0;
int rand_int(int a, int b);
int main()
/* Generate the length of the storm */
max_storm_length = rand_int(180,300);
/* Compute wind speeds. */
for (timer=START_TIME; timer<=STOP_TIME; timer+=DELTA_TIME)
{
{
storm_flag = STORM;
storm_duration = 0;
}
windspeed = rand_float(ave_wind+gust_min,ave_wind+gust_max);
Roots of Functions
/*——————————————————————–*/
/* Problem chapter6_37 */
/* */
int main()
{
/* Declare variables. */
double a, b, c, discriminant, root1,root2;
/* Prompt user for equation. */
cout << “Enter a,b,c for equation ax^2 + bx + c: “;
/* Exit program. */
return 0;
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter6_38 */
/* */
/* This program determines the roots of a quadratic equation. */
#include <iostream>
#include <cmath>
cin >> a >> b >> c;
cout << “Equation is: ” << a << “x^2 + ” << b << “x + “
<< c << ” = y\n”;
/* Are the roots complex? */
cout << “Roots are: x1=” << root1 << “, x2=” << root2 << endl;
}
/* Exit program. */
return 0;
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter6_39 */
/* */
int main()
{
/* Declare variables. */
int n, k;
double a, b, step, left, right;
/* Get user input. */
}
/*——————————————————————–*/
/* This function checks a subinterval for a root. */
void check_roots(double left, double right)
{
/* Declare variables and function prototypes. */
double f_left, f_right;
double f(double x);
/* Evaluate subinterval endpoints and test for roots. */
f_left = f(left);
}
/*——————————————————————–*/
/* This functions evaluates a mathematical function given */
/* in problem 28. Be sure not to call with x < 0. */
double f(double x)
{
/* */
/* This program finds the roots of this function in a */
/* user-specified interval: f(x) = sinc(x) */
#include <iostream>
#include <cmath>
int main()
/* Check subintervals for roots. */
n = ceil((b a)/step);
for (k=0; k<=n-1; k++)
{
left = a + k*step;
/* Exit program. */
return 0;
}
/*——————————————————————–*/
/* This function checks a subinterval for a root. */
void check_roots(double left, double right)
{
/* Void return. */
return;
}
/*——————————————————————–*/
/* This function evaluates a sinc function. */
double sinc(double x)
{
/* Return function value. */
if (x == 0)
return 1;
else
return sin(x)/x;
}
/*——————————————————————–*/
int main()
{
/* Declare variables. */
int n, k;
double a0, a1, a2, a3, a, b, step, left, right;
/* Get user input. */
cout << “Enter coefficients a0, a1, a2, a3: \n”;
cin >> a0>> a1>> a2>> a3;
cout << “Enter interval limits a, b (a<b): \n”;
cin >> a >> b;
cout << “Enter step size: \n”;
cin >> step;
}
/* Void return. */
return;
}
/*——————————————————————–*/
/* (poly function from page 231) */
/*——————————————————————–*/
/*————————————————————————-*/
/* Program chapter6_42 */
/* */
/* This program finds the real roots of a polynomial using the */
/* Newton-Raphson method using functions that evaluate the polynomial */
/* and its derivative. */
#include <iostream>
#include <cmath>
using namespace std;
}
else
cout << “Did not converge after 100 iterations\n”;
return 0;
}
/*————————————————————————-*/
/* This function calculates the value of a polynomial function given */
/* coefficients. */
double polynomial(double a, double b, double c, double d, double x)
{
return a*pow(x,3) + b*x*x + c*x + d;
}
/* */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. */
#include <iostream>
#include <cmath>
using namespace std;
// Function prototypes.
double integrate(double a, double b, int n);
double f(double x);
int main()
{
// Declare objects
int num_trapezoids;
double a, b, area;
return 0;
}
/*————————————————————————*/
double integrate(double a, double b, int n)
{
// Declare objects.
double sum = 0, x, base, area;
base = (b-a)/n;
for(int k=2; k<=n; k++)
}
/*———————————————————————–*/
double f(double x)
{
return (3*x 2*pow(x,2));
}
/*———————————————————————–*/
/*————————————————————————*/
/* Program chapter6_44 */
/* */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. The xand y-coordinates of the */
int num_trapezoids;
}
/*———————————————————————–*/
double integrate(double a, double b, int n)
{
// Declare objects.
double sum = 0, x, base, area;
ofstream out;
//Open output file
out.open(“plotTrapezoids.txt”);
if (out.fail()){
cerr << “Output file did not open.”;
exit(1);
}
{
x = a + base*(k-1);
sum = sum + f(x);
out << x << ” ” << f(x) << endl;
}
out << b << ” ” << f(b) << endl;
/* This program estimates the area under a curve */
/* using trapezoids. The curve is defined by a set of */
/* experimental data. The spacing of the independent */
/* variable across the interval is not uniform. */
#include<iostream>
exit(1);
}
area = integrate(fin);
cout << “The area under the curve is ” << area << endl;
return 0;
}
/*——————————————————-*/
}
return area;
}
/*——————————————————————–*/
Value Returning Functions
/*——————————————————————–*/
/* Function chapter6_46 */
int n_fact(int n)
{
int rValue=1;
for(int i=n; i>1; –i)
{
rValue *= i;
}
return rValue;
}
return (int)(n_fact(n)/(n_fact(k)*n_fact(n-k)) + 0.5 );
}
/*——————————————————————–*/
/* (n_fact function from Problem 6_46) */
/*——————————————————————–*/
/*——————————————————————–*/
const int SERIES_LENGTH = 5;
int n_fact(int n);
double s_cosine(double x);
int main()
{
/* Define variables. */
/* This function calculates the series for cosine */
/* for a specified number of terms. */
double s_cosine(double x)
{
/* Declare variables. */
int i;
/* Problem chapter5_49 */
/* */
/* This program compares the cosine of an angle using the library */
/* function with the value computed from terms > 0.0001 */
/* of this series: cos x = 1 x^2/2! + x^4/4! ….. */
double x;
/* Get x from user. */
cout << “\nEnter x in radians: “;
cin >> x;
/* Get series cosine. */
double sum=0, term=1;
/* Determine cosine sum. */
while (fabs(term) > MAX_TERM)
{
sum += term;
i++;