Question

In: Computer Science

Write a program for Bob’s Bus Company that allows a user check on ticket prices following...

  • Write a program for Bob’s Bus Company that allows a user check on ticket prices following the guidelines below:
                     Zones Crossed
             ---------------------------
Passengers     0      1      2      3
----------------------------------------
1             7.50  10.00  12.00  12.75
2            14.00  18.50  22.00  23.00
3            20.00  21.00  32.00  33.00
4            25.00  27.50  36.00  37.00
  • The user enters ticket data (number of passengers and number of zones crossed) continuously until a sentinel value, -1 for passengers, is entered. When -1 is entered for number of passengers, there is no prompt for number of zones.
  • After each set of ticket data is entered, display the price or the ticket(s).

Sample Run #1 (bold, underlined text is what the user types):

Passengers? 2

Zones? 3

$23
Passengers? 4

Zones? 0

$25
Passengers? -1

Done

c++

Solutions

Expert Solution

Program: In this program, we take user inputs from the user for the number of passengers and zones and depending on that we display the price of the ticket(s) usiing the information. We have implemented it in two ways:

1)Using switch case: For every passenger value we check the value of zones and print the price accordingly.

2)Using a 2D array: In this we store the prices in a 2D array and using the passenger and zones value as indices we access the ticket price.

Implementation using switch case:

Code:

#include <iostream>

using namespace std;

int main() {

    //Create variables
    int zones, passengers;

    //Take Inputs
    cout << "Passengers? ";
    cin >> passengers;

    //Run a loop while passengers is not equal to -1
    while (passengers != -1) {
        cout << "Zones? ";
        cin >> zones;

        //Make switch case for passengers
        switch (passengers) {
            case 1: {
                if (zones == 0) {
                    cout << "$7.5" << endl;
                } else if (zones == 1) {
                    cout << "$10" << endl;
                } else if (zones == 2) {
                    cout << "$12" << endl;
                } else if (zones == 3) {
                    cout << "$12.75" << endl;
                }
                break;
            }
            case 2: {
                if (zones == 0) {
                    cout << "$14" << endl;
                } else if (zones == 1) {
                    cout << "$18.5" << endl;
                } else if (zones == 2) {
                    cout << "$22" << endl;
                } else if (zones == 3) {
                    cout << "$23" << endl;
                }
                break;
            }
            case 3 : {
                if (zones == 0) {
                    cout << "$20" << endl;
                } else if (zones == 1) {
                    cout << "$21" << endl;
                } else if (zones == 2) {
                    cout << "$32" << endl;
                } else if (zones == 3) {
                    cout << "$33" << endl;
                }
                break;
            }
            case 4: {
                if (zones == 0) {
                    cout << "$25" << endl;
                } else if (zones == 1) {
                    cout << "$27.5" << endl;
                } else if (zones == 2) {
                    cout << "$36" << endl;
                } else if (zones == 3) {
                    cout << "$37" << endl;
                }
                break;
            }

            default: {
                cout << "Invalid option." << endl;
                break;
            }

        }
        cout << "Passengers? ";
        cin >> passengers;
    }

    cout << "Done" << endl;
    return 0;

}

Output:

Implementation using a 2D array:

Code:

#include <iostream>

using namespace std;

int main() {
    //Store the price of the tickets in a 2D array
    double price[4][4] = {{7.50,  10.00, 12.00, 12.75},
                          {14.00, 18.50, 22.00, 23.00},
                          {20.00, 21.00, 32.00, 33.00},
                          {25.00, 27.50, 36.00, 37.00}};

    //Create variables
    int zones, passengers;

    //Take Inputs
    cout << "Passengers? ";
    cin >> passengers;

    //Run a loop while passengers is not equal to -1
    while (passengers != -1) {
        cout << "Zones? ";
        cin >> zones;

        //Print for each entry, reduce in the passenger variable by 1 to match array index
        cout << "$" << price[passengers - 1][zones] << endl;
        cout << "Passengers? ";
        cin >> passengers;
    }

    cout << "Done" << endl;
    return 0;
}

Output:

#Please ask for any kind of doubts!. Thanks.


Related Solutions

Type or pa A study by Metro Bus Service on the effect of bus-ticket prices on...
Type or pa A study by Metro Bus Service on the effect of bus-ticket prices on the number of passengers produced the following results: Ticket Price:        25           30           35           40           45           50           55           60 Pssngrs/100 m   800         700         780         660         640         600         620         620 Plot these dat Develop the estimating equation that best describe these data. How can we ensure that the above model best used for provided data? Predict the number of passengers per 100 miles if the ticket...
Write in Python and as 2 seperate programs Write a program that allows the user to...
Write in Python and as 2 seperate programs Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number of data set. 2- Allows the user to exit the program at any time by entering zero. 3- Calculates and displays the following statistics for the entered data set: a- Count of positive, negative, and total numbers. b- Maximum and Minimum numbers. c- Sum and Average of positive numbers. d- Sum and Average of negative numbers. e- Overall Sum and overall average of all...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT