/*******************************Libraries********************************/

#include <iostream.h>

#include<stdlib.h>

#include <math.h>

/************************************************************************/

 

/***************************Function Prototype***************************/

void speed(double []);

void light(double []);

double leaving();

/************************************************************************/

 

/*******************************Main Program*****************************/

void main()

{ //opens main program

double Velocity[10];

speed(Velocity);

for (int k = 1;k<=10;k++)

{ //opens for loop

cout << Velocity[k]<<endl;

} //closes loop

light(Velocity);

} //ends main program

/************************************************************************/

 

/******************************Function Speed****************************/

void speed(double Avg[])

{ //opens function

//Variables

double time_interval=0.1;

double acceleration=10.0;

float S[50];

float x[50];

float time[50];

int j= 3;

S[0]=0.0;

Avg[0]=0.0;

x[0]=0.0;

time[0]=0.0;

for (int n=1; n<=j; n++)

{ //opens loop

time[n]= time[n-1] + time_interval;

S[n]=time[n]*acceleration;

Avg[n]= (S[n]+S[n-1])/2.0;

x[n]=time_interval*Avg[n]+x[n-1];

cout<<n<<" "<<time[n]<<" "<<S[n]<<" "<<Avg[n]<<" "<<x[n]<<endl;

} //ends loop

} //ends function

/**********************************************************************/

 

/****************************Function Light****************************/

void light(double V[])

{ //opens function

double c; //the rate of the cars leaving the light

double v2; //velocity of cars leaving S.L.

double x; //distance between front bumpers of cars entering S.L.

double d; //distance between front bumpers of cars leaving S.L.

double t; //time the light is green

double c1; //The rate of cars arriving at the light

int i,j;

double speed2; //Speed of cars leaving in mph

double w; //The exponetial function for the light

double z; //the number of cars at the light

int count=0;

c = 1.0;

for( j=1; j<11; j++)

{ //opens loop

cout<<"Velocity equals "<<V[j]<<" "<<"ft/sec"<<endl;

x=50.0;

cout<<endl;

c1=V[j]/x;

cout<<"The rate of cars entering is "<<c1<<" cars/sec"<<endl;

cout<<endl;

//Allows for random speeds to be read into the program

speed2=leaving();

//Changes speed to feet per second

v2=(speed2*5280.0)/3600.0; //converts mph to ft/sec

cout<<"The velocity of the cars leaving is "<<v2<<" ft/sec"<<endl;

count++;

cout<<"Count = "<<count<<endl;

d=60.0;

cout<<endl;

t=1;

w=((V[j]/x)-(v2/d));

cout<<"w equals "<<w<<endl;

for( i=0; i<=8; i++)

{ //opens loop

if (c!=0.0)

{ //Opens the if statement

c=c1-(c1*(pow(2.718281828,(w*t))));

z=c1-c;

cout<<"At light "<<c<<" "<<"Leaving "<<z<<endl;

cout<<"The length of the green light was "<<t<<" sec"<<endl;

cout<<endl;

t=t++;

} //Closes the if statement

} //ends loop

} // Ends first for loop

} //ends function

 

/********************************************************************/

//function to find the speed2 -- speed of car leaving stop sign

/********************************************************************/

double leaving()

{ //opens function

const double a=5.0;

const double b=20.0;

double xx;

xx=a+double(rand())*((b-a)/RAND_MAX);

cout <<"speed2 = "<<xx<<" mph"<<endl;

return(xx);

} //ends function