fork download
  1. /**************************************************
  2. // Function: calculateJarValue
  3. //
  4. // Description: Calculates the total monetary value of a jar containing
  5. // various coins (dollars, half-dollars, quarters, dimes,
  6. // nickels, and pennies).
  7. //
  8. // Parameters: dollars - number of dollar coins
  9. // halfDollars - number of half-dollar coins
  10. // quarters - number of quarter coins
  11. // dimes - number of dime coins
  12. // nickels - number of nickel coins
  13. // pennies - number of penny coins
  14. //
  15. // Returns: totalValue - total monetary value in dollars (float)
  16. // ***************************************************/
  17.  
  18. #include <stdio.h>
  19.  
  20. float calculateJarValue(int dollars, int halfDollars, int quarters, int dimes, int nickels, int pennies) {
  21. // Coin values in dollars
  22. const float VALUE_DOLLAR = 1.00;
  23. const float VALUE_HALF_DOLLAR = 0.50;
  24. const float VALUE_QUARTER = 0.25;
  25. const float VALUE_DIME = 0.10;
  26. const float VALUE_NICKEL = 0.05;
  27. const float VALUE_PENNY = 0.01;
  28.  
  29. // Calculate total value
  30. float totalValue = (dollars * VALUE_DOLLAR) +
  31. (halfDollars * VALUE_HALF_DOLLAR) +
  32. (quarters * VALUE_QUARTER) +
  33. (dimes * VALUE_DIME) +
  34. (nickels * VALUE_NICKEL) +
  35. (pennies * VALUE_PENNY);
  36.  
  37. return totalValue;
  38. }
  39.  
  40. int main() {
  41. // Test the calculateJarValue function with given values
  42. int dollars = 0;
  43. int halfDollars = 11;
  44. int quarters = 7;
  45. int dimes = 3;
  46. int nickels = 12;
  47. int pennies = 17;
  48.  
  49. float totalValue = calculateJarValue(dollars, halfDollars, quarters, dimes, nickels, pennies);
  50.  
  51. // Print the result
  52. printf("The total value in the jar is: $%.2f\n", totalValue);
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5268KB
stdin
0 dollars, 
11 half-dollars,
 7 quarters, 
3 dimes, 1
2 nickels,
17 pennies
stdout
The total value in the jar is: $8.32