diff --git a/DeitelC/Chapter5/carRental.c b/DeitelC/Chapter5/carRental.c new file mode 100644 index 0000000..7b4c506 --- /dev/null +++ b/DeitelC/Chapter5/carRental.c @@ -0,0 +1,29 @@ +#include + +double calculateCharges (int hours); + +int main (void) { + printf("%5s\t%5s\t%5s\n", "Car", "Hours", "Charge"); + printf("%5s\t%5d\t%5.2f\n", "1", 22, calculateCharges(22)); + printf("%5s\t%5d\t%5.2f\n", "2", 12, calculateCharges(12)); + printf("%5s\t%5d\t%5.2f\n", "3", 34, calculateCharges(34)); + printf("%5s\t%5d\t%5.2f\n", "4", 48, calculateCharges(48)); + printf("%5s\t%5d\t%5.2f\n", "5", 94, calculateCharges(94)); +} + +double calculateCharges (int hours) { + int dayMod = hours % 24; + int days = hours / 24; + + if (days >= 1) { + return ((days * 50)+(hours * 0.5)); + } else { + if (hours <= 8) { + return ((hours * 0.5) + 25); + } else if (hours <= 13) { + return (((hours - 8) * 5) + (hours * 0.5) + 25); + } else { + return (50 + (hours * 0.5)); + } + } +} diff --git a/DeitelC/Chapter5/exercise5-8.c b/DeitelC/Chapter5/exercise5-8.c new file mode 100644 index 0000000..257879a --- /dev/null +++ b/DeitelC/Chapter5/exercise5-8.c @@ -0,0 +1,14 @@ +#include +#include + +int main (void) { + double x; + x = fabs(10.85); + x = floor(10.85); + x = fabs(-0.678); + x = ceil(9.234); + x = fabs(0.0); + x = ceil(-34.87); + x = ceil(-fabs(-12 - floor(-9.5))); + printf("%.5f\n", x); +} diff --git a/DeitelC/Chapter5/math b/DeitelC/Chapter5/math index 83e9c4e..6ed021f 100644 Binary files a/DeitelC/Chapter5/math and b/DeitelC/Chapter5/math differ diff --git a/DeitelC/Chapter5/math.c b/DeitelC/Chapter5/math.c index 12aa95e..f8c57ce 100644 --- a/DeitelC/Chapter5/math.c +++ b/DeitelC/Chapter5/math.c @@ -2,6 +2,8 @@ #include double hypotenuse (double side1, double side2); +int smallest (int x, int y, int z); +int intToFloat (double number); int main (void) { printf("%.2f\n", cbrt(27.0)); @@ -18,9 +20,24 @@ int main (void) { printf("%.5f\n", cos(0.0)); printf("%.5f\n", tan(0.0)); printf("%.5f\n", hypotenuse(3.0,4.0)); + printf("%d\n", smallest(3,3,4)); + printf("%d\n", intToFloat(3.4)); } double hypotenuse (double side1, double side2) { double final = (side1*side1) + (side2*side2); return sqrt(final); + } + +int smallest (int x, int y, int z) { + if (x <= y && x <= z) { + return x; + } else if (y <= z && y <= x) { + return y; + } else { + return z; + } + } +int intToFloat (double number) { + return number; } diff --git a/DeitelC/Chapter5/roundingNumbers.c b/DeitelC/Chapter5/roundingNumbers.c new file mode 100644 index 0000000..61b52d9 --- /dev/null +++ b/DeitelC/Chapter5/roundingNumbers.c @@ -0,0 +1,10 @@ +#include +#include + +int main (void) { + double num; + printf("%s\n", "Enter a number!"); + scanf("%lf", &num); + int y = floor(num + .5); + printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", y); +}