First of all, you use the data type int
, which cannot hold fractional values (and will implicitly round them towards zero, even before the function is ever called.) You should use double
instead, since this is the proper datatype for fractional numbers.
You would also want to use the ceil(x)
function, which gives the nearest whole number larger than or equal to x
.
#include <math.h> double calculateCharges(double hours) { if (hours <= 3) { return 2.00; } else { // return $2.00 for the first 3 hours, // plus an additional $0.50 per hour begun after that return 2.00 + ceil(hours - 3) * 0.50; } }
Related Posts:
- strdup() – what does it do in C?
- strdup() – what does it do in C?
- How do you pass a function as a parameter in C?
- How do you pass a function as a parameter in C?
- Optional arguments in C function
- “Parameter” vs “Argument”
- What is the proper declaration of main in C++?
- ‘foo’ was not declared in this scope c++
- Help needed with Median If in Excel
- C error: undefined reference to function, but it IS defined
- PHP Fatal error: Using $this when not in object context
- error: expected primary-expression before ‘)’ token (C)
- How do you pass a function as a parameter in C?
- Using multiple .cpp files in c++ program?
- struct has no member named
- How do I use the filter function in Haskell?
- Can I define a function inside a C structure?
- Passing string to a function in C – with or without pointers?
- Parameter name omitted error?
- the functions (procedures) in MIPS
- warning: return makes pointer from integer without a cast but returns integer as desired
- How big can a 64 bit unsigned integer be?
- The difference between n++ and ++n at the end of a while loop? (ANSI C)
- What is a segmentation fault?
- How many spaces for tab character(\t)?
- Implementing Taylor Series for sine and cosine in C
- Working on code to calculate cosine with factorial sum
- How do you round UP a number?
- How do you round UP a number?
- How to use execvp()
- How to use execvp() to execute a command
- How does strtok() split the string into tokens in C?
- warning: implicit declaration of function
- warning: implicit declaration of function
- What is *(uint32_t*)?
- What does (~0L) mean?
- pthread_join() and pthread_exit()
- What is size_t in C?
- What is the difference between float and double?
- what is Segmentation fault (core dumped)? [duplicate]
- What causes a segmentation fault (core dump) to occur in C?
- uint8_t vs unsigned char
- How to use symbols of extended ASCII table in C?
- Two questions about basic C programs
- What is the difference between ++i and i++?
- How to round a number to n decimal places in Java
- Define a global variable in a JavaScript function
- Using boolean values in C
- How to send an email with Python?
- What does “collect2: error: ld returned 1 exit status” mean?
- How to convert an int to string in C?
- C++ — expected primary-expression before ‘ ‘
- typedef struct vs struct definitions [duplicate]
- How to print in C
- What is the effect of extern “C” in C++?
- Why should we typedef a struct so often in C?
- How to round to at most 2 decimal places, if necessary?
- Why are #ifndef and #define used in C++ header files?
- Arrow operator (->) usage in C
- What is a string of hexadecimal digits?
- Why should we typedef a struct so often in C?
- How to round to at most 2 decimal places, if necessary?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is the difference between ++i and i++?
- Define a global variable in a JavaScript function
- What can be the reasons of connection refused errors?
- What does “collect2: error: ld returned 1 exit status” mean?
- How to convert an int to string in C?
- typedef struct vs struct definitions [duplicate]
- What is newline character — ‘\n’
- When to use extern “C” in simple words? [duplicate]
- Floating point exception( core dump
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- Cannot figure out how to use getchar(); in C
- Floating point exception (core dumped)
- Undefined reference to pthread_create in Linux
- what is the difference between uint16_t and unsigned short int incase of 64 bit processor?
- makefile:4: *** missing separator. Stop
- Cannot read property ‘addEventListener’ of null
- Stack smashing detected
- Why am I getting “void value not ignored as it ought to be”?
- Correct format specifier for double in printf
- Return array in a function
- Compiler Error “void value not ignored as it ought to be” in C programming [duplicate]
- Stack smashing detected
- How do I determine the size of my array in C?
- Why am I getting “void value not ignored as it ought to be”?
- Mutex example / tutorial? [closed]
- Scanf/Printf double variable C
- Pointer Arithmetic
- dereferencing pointer to incomplete type
- 1 = false and 0 = true?
- c stack smashing detected
- What does “dereferencing” a pointer mean?
- Openssl : error “self signed certificate in certificate chain”
- How to convert a string to integer in C?
- What is the difference between C and embedded C?
- What is an unsigned char?
- What does “dereferencing” a pointer mean?