Question

I had help with a program, it requires constants and 3 functions. Ask the user for...

I had help with a program, it requires constants and 3 functions.

  1. Ask the user for the number of miles on the route, the average speed and the time of day
  2. Randomly generate an increase in the route time based upon whether it’s rush hour or not
  3. Calculate the minutes in the route based upon miles, average speed and random traffic increase
  4. Print out the miles, minutes and a message

The program will continue asking the user for another trip’s miles, speed, and time until the user decides to quit.

Use the table below to determine the random chance of traffic

Rush Hour (and hour of 7, 8, 17 or 18)

Not Rush Hour

No traffic

5%

50%

Small traffic

10%

30%

Medium traffic

35%

15%

Large traffic

50%

5%

Use the table below to determine the time added depending upon the amount of traffic

Traffic Type

Time Added

No traffic

0

Small traffic

25%

Medium traffic

50%

Large traffic

100%

I must write and use the following functions:

  • int getUserData( double& miles, double& avgSpeed, int& hours): This function gets the miles, average speed and hour of the day from the user. It prints and error message for invalid data (see sample execution below). It returns 0 if the user decided to end the program, a -1 if the user entered invalid data or a 1 if the user entered valid data. Its post-condition is that the reference parameters contain the valid miles, average speed and hour that the user entered.
  • double calcTrafficIncrease( int hour ): This function randomly determines an amount of traffic based upon the hour and returns it. The function returns either 1 for no traffic, 1.25 for a small amount of traffic, 1.5 for a medium amount of traffic or 2.0 for a large amount of traffic (see table above).
  • void printOutput( double miles, double minutes, double trafficIncrease ): this function prints the miles, minutes and message (depending upon the amount of traffic). Output messages indicating traffic are:

No traffic.

A slight amount of traffic.

There is a lot of traffic. You are still on the fastest route.

Major traffic. You are not on the fastest route.

I must use the following main function, make any constants.

int main() {

   double miles, avgSpeed; // avgSpeed per hour

   double timeToSchool;    // in minutes

   double trafficIncrease; // increase in time due to traffic,

                           // ie. MEDUIM_TRAFFIC_ADDED

   int rv;                 // return value of getUserData

   int hour;               // starting hour in 24-hour clock

srand( time( 0 ) );

   cout.precision( 0 );

   cout.setf( ios::fixed );

   cout << "Welcome to Poogle Maps\n";

   rv = getUserData( miles, avgSpeed, hour );

   while ( rv != END ) {

      if ( rv != INVALID_DATA ) {

         trafficIncrease = calcTrafficIncrease( hour );

         timeToSchool = miles / avgSpeed * 60 * trafficIncrease;

         printOutput( miles, timeToSchool, trafficIncrease );

      } // end if valid data

      rv = getUserData( miles, avgSpeed, hour );

   }

   cout << "Exiting Poogle Maps\n";

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code:

#include<iostream>
#include<stdlib.h>
using namespace std;

#define END 0
#define INVALID_DATA -1

//function to take input from user
int getUserData( double& miles, double& avgSpeed, int& hours)
{
   cout<<"\nEnter distance (in miles) (0 to exit):";
   cin>>miles;
   if(miles==0)
       return(0);
   cout<<"Enter average speed (in miles/hour):";
   cin>>avgSpeed;
   cout<<"Enter time(24 hours format):";
   cin>>hours;
   if(hours>0 and hours<25)
   {
       return(1);
   }
   cout<<"\nInvalid data........\n";
   return(-1);
}

//function to calculate traffic randomly from given time
double calcTrafficIncrease(int hour)
{
   int n=rand()%100+1;//generate a random number between 1 and 100
   if(hour==7 || hour==8 ||hour==17 ||hour==18)
   {
       //in rush hpur
       //1-5(No traffic) 6-15(Small traffic) 16-50(Medium traffic) 51-100(Large Traffic)
       if(n<=5) //if n is [1,5] then No traffic as it's percentage is 5%
       {
           return(1);
       }
       else if(n>5 && n<=15) //for small traffic
       {
           return(1.25);
       }
       else if(n>15 && n<=50)   //for medium traffic
       {
           return(1.5);
       }
       else               //for large traffic
       {
           return(2);
       }
   }
   else
   {   //in No rush hour
       //1-50(No traffic) 51=80(Small traffic) 81-95(Medium traffic) 96-100(Large Traffic)
       if(n<=50) //if n is [1,50] then No traffic as it's percentage is 50%
       {
           return(1);
       }
       else if(n>51 && n<=80) //for small traffic
       {
           return(1.25);
       }
       else if(n>81 && n<=95)   //for medium traffic
       {
           return(1.5);
       }
       else                   //for large traffic
       {
           return(2);
       }
   }
  
}

//function to print output
void printOutput(double miles, double minutes, double trafficIncrease)
{
   cout<<"\nDistance covered: "<<miles<<" miles"<<endl;
   cout<<"Time : "<<minutes<<" minutes"<<endl;
   cout<<"Traffic increase: "<<trafficIncrease<<endl;
}


//main function
int main()
{
   double miles, avgSpeed; // avgSpeed per hour
double timeToSchool; // in minutes
double trafficIncrease; // increase in time due to traffic,
// ie. MEDUIM_TRAFFIC_ADDED
int rv; // return value of getUserData
int hour; // starting hour in 24-hour clock

//srand( time( 0 ) );
cout.precision( 0 );
cout.setf( ios::fixed );
cout << "Welcome to Poogle Maps\n";
rv = getUserData( miles, avgSpeed, hour );
while ( rv != END ) {
if ( rv != INVALID_DATA ) {
trafficIncrease = calcTrafficIncrease( hour );
timeToSchool = miles / avgSpeed * 60 * trafficIncrease;
printOutput( miles, timeToSchool, trafficIncrease );
} // end if valid data
rv = getUserData( miles, avgSpeed, hour );
}
cout << "Exiting Poogle Maps\n";
}

Output:

Add a comment
Know the answer?
Add Answer to:
I had help with a program, it requires constants and 3 functions. Ask the user for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • I need help with a Python program that has error checking loops. Hour must be a...

    I need help with a Python program that has error checking loops. Hour must be a number from 1 to 12. Minute and second must be numbers from 0 to 59. Also check whether “AM” or “PM” is entered. Every time an invalid value is entered, display an error message and ask the user to re-enter a valid value immediately. Functions are not covered yet and are not necessary. Thanks. The following is an example: Enter hour: 0 Hour must...

  • Goals: Practicing functions and parameter types. Problem: You are to create a program that will aid...

    Goals: Practicing functions and parameter types. Problem: You are to create a program that will aid in a farmer in monitoring the amount of corn to feed livestock each week. There is a template named "Labs1-5template.cpp" posted on Canvas which you should download and complete. Follow the instructions given in the template and follow the assignment guidelines for the course. Do not use global variables and do not use the same variable name in multiple functions. Your finished code should...

  • CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1...

    CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Utilizing Lab 2.2 code Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds.There are 2.2 pounds in one kilogram. Create an...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT