fork download
  1. % Car Rental Cost Calculation Script
  2.  
  3. % Function to calculate the cost of renting a car
  4. function calculateCarRentalCost()
  5. % Prompt user for car type
  6. carType = input('Enter the type of car (Sedan/SUV): ', 's');
  7.  
  8. % Prompt user for the number of days
  9. numDays = input('Enter the number of days rented: ');
  10.  
  11. % Prompt user for the number of miles driven
  12. numMiles = input('Enter the number of miles driven: ');
  13.  
  14. % Initialize cost variables
  15. dailyRate = 0;
  16. freeMiles = 0;
  17. costPerMile = 0;
  18.  
  19. % Determine the daily rate and free miles based on car type and rental duration
  20. switch lower(carType)
  21. case 'sedan'
  22. if numDays >= 1 && numDays <= 6
  23. dailyRate = 79;
  24. freeMiles = 80;
  25. costPerMile = 0.69;
  26. elseif numDays >= 7 && numDays <= 29
  27. dailyRate = 69;
  28. freeMiles = 100;
  29. costPerMile = 0.59;
  30. elseif numDays >= 30
  31. dailyRate = 59;
  32. freeMiles = 120;
  33. costPerMile = 0.49;
  34. else
  35. fprintf('Invalid number of days.\n');
  36. return;
  37. end
  38. case 'suv'
  39. if numDays >= 1 && numDays <= 6
  40. dailyRate = 84;
  41. freeMiles = 80;
  42. costPerMile = 0.74;
  43. elseif numDays >= 7 && numDays <= 29
  44. dailyRate = 74;
  45. freeMiles = 100;
  46. costPerMile = 0.64;
  47. elseif numDays >= 30
  48. dailyRate = 64;
  49. freeMiles = 120;
  50. costPerMile = 0.54;
  51. else
  52. fprintf('Invalid number of days.\n');
  53. return;
  54. end
  55. otherwise
  56. fprintf('Invalid car type. Please enter Sedan or SUV.\n');
  57. return;
  58. end
  59.  
  60. % Calculate the total cost
  61. totalCost = numDays * dailyRate;
  62.  
  63. % Calculate additional miles cost if applicable
  64. if numMiles > freeMiles
  65. additionalMiles = numMiles - freeMiles;
  66. totalCost = totalCost + additionalMiles * costPerMile;
  67. end
  68.  
  69. % Display the cost rounded to two decimal places
  70. fprintf('The cost of the rent is %.2f $.\n', totalCost);
  71. end
  72.  
  73. % Run the function three times for different cases
  74. fprintf('Case (a):\n');
  75. calculateCarRentalCost(); % User input needed for Sedan, 10 days, 769 miles
  76.  
  77. fprintf('\nCase (b):\n');
  78. calculateCarRentalCost(); % User input needed for SUV, 32 days, 4,056 miles
  79.  
  80. fprintf('\nCase (c):\n');
  81. calculateCarRentalCost(); % User input needed for Sedan, 3 days, 511 miles
  82.  
Success #stdin #stdout #stderr 0.1s 47756KB
stdin
Standard input is empty
stdout
Case (a):
Enter the type of car (Sedan/SUV): 
stderr
error: input: reading user-input failed!
error: called from
    calculateCarRentalCost at line 6 column 13
    /home/r67HKA/prog.octave at line 75 column 1