Concept explainers
C++ Fix the broken code, Fix the function that is being used It does not charge the full hour for fractional portions. If you enter 4.5 it will return 27.50 as opposed to 30.
Instructions to fix the code
#include<iostream>
#include<iomanip>
using namespace std;
/* This is a function that calculates the charge of parking for the given amount of time (in hours) */
double calculateCharges(double hours) {
if (hours <= 3.0) { // If the time is less than 3 hours.
return 20.0; // Charge a fixed amount of $20
}
else {
double charge = 20.0 + (hours - 3.0) * 5; // Calculate the charge, which is $20 + ($5/hour for excess of 3 hours)
if (charge >= 50.0) { // If the charge is greater than $50
return 50.0; // Charge the maximum amount of $50
}
return charge;
}
}
int main() {
// Declare the variables
double hoursParked[3], // For storing the number of hours parked
parkingCharges[3], // For storing the parking charges
totalHours = 0, // For storing the total hours all cars were parked
totalCharges = 0; // For storing the total charges of all cars
for (int i = 1; i <= 3; i++) { // Prompt the user for input of hours a car was parked
cout << "Enter the hours Car " << i << " was parked: ";
cin >> hoursParked[i - 1];
parkingCharges[i - 1] = calculateCharges(hoursParked[i - 1]); // Calculate the parking charges
totalCharges += parkingCharges[i - 1]; // Add charges to total
totalHours += hoursParked[i - 1]; // Add hours to total
}
// Print the information header
// Use setw() to set the width of the text. We have used a width of 10 in our case
cout << setw(5) << "Car" << setw(10) << "Hours" << setw(10) << "Charge" << endl;
for (int i = 1; i <= 3; i++) {
// Display the values for each car
cout << fixed << setw(5) << i << setw(10) << setprecision(1) << hoursParked[i - 1] << setw(10) << setprecision(2) << parkingCharges[i - 1] << "\n";
}
// Display the total values
cout << fixed << setw(5) << "TOTAL" << setw(10) << setprecision(1) << totalHours << setw(10) << setprecision(2) << totalCharges << "\n";
}
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images
- C++ Programming: I need help writing a void function that prints sales details using 3 for loops. Please see my code below ... not sure how to write the function. But this is what i've got so far. Please leave main alone. What the output should look like is posted below the code. Thanks :) #include <iostream>#include <string>#include <iomanip> using namespace std; int const LOCATION_DIM_SZ = 2;int const TIME_DIM_SZ = 2;int const PRODUCT_DIM_SIZE = 3; string Location[] = { "FL", "TX" };string Period[] = { "Jan", "FEB" };string Product[] = { "iPhone","iPad","MacBookPro" }; /*1*/void printSalesDetails(double sales[LOCATION_DIM_SZ][TIME_DIM_SZ][PRODUCT_DIM_SIZE]) { int i(0), j(0), k(0);int sum = 0; cout << "Sales Details per Location per Period per Product" << endl;cout << "=================================================" << endl;for (i = 0; i < 2; i++){double salesPerLocation = 0;sum = 0;for (j = 0; j < 2; j++){double salesPerPeriod =…arrow_forwardWarning ⚠️ Don't post AI generated answer or plagiarised answer. If I see these things I'll give you multiple downvotes and will report immediately.arrow_forwardc++ question Write the definition for a value-returning function named ticketRate that has 1 parameter named category (a character) and returns the cost of a ticket (a floating-point). Your function should return 12.95 i f the category is 'A', 25.00 if the category is 'B' and return 0.0 otherwise.arrow_forward
- In C++ just do the problem in simple, not complicated code. Even if a problem does not directly tell you to use a function, you should aim to do so regardless. The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 9003? Create a function called Nevada where you pass in the number as an argument and return the largest factor.arrow_forward2. Code the given function and test it inside the main function. int Length(char* str){ // return the length of string str //write your code here}arrow_forwardWrite a complete C++ program that helps the teacher to calculate the result of students in the test of Programming Technique. The program should perform the following tasks: Task 1: Write a function named getInput. a) This is a non-returning function. b) It takes the score of question 1, score of question 2, and score of question 3 as input parameters. c) The function should ask the user to enter the score (per 100) for each question. It sends all the values entered by the user in (c) back to the calling module through the use of reference parameters. d)arrow_forward
- answer in c++arrow_forwardC++ CODEarrow_forward*You need to have at least one user define function *Language: C programming Write a C Program to print half pyramid using numbers shown below. This example is for height 6. Ask user for a height and depending on user input, draw the half pyramid. Input: Key in the height: 6 Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education