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
- The difference between n++ and ++n at the end of a while loop? (ANSI C)
- How many spaces for tab character(\t)?
- Working on code to calculate cosine with factorial sum
- warning: implicit declaration of function
- What is the effect of extern “C” in C++?
- How to convert a string to integer in C?
- What is the behavior of integer division?
- max value of integer
- Convert an int to ASCII character
- Awesomium sdk download
- Bad File Descriptor with Linux Socket write() Bad File Descriptor C
- What is the difference between char s[] and char *s?
- need help understanding the movzbl call in this function
- How to extract C source code from .so file?
- What is the difference between read and pread in unix?
- What does “control reaches end of non-void function” mean?
- connect Error: “No route to host”
- What is the printf format specifier for bool?
- warning: implicit declaration of function
- C dynamically growing array
- How to round a number to significant figures in Python
- lvalue required as left operand of assignment
- how to convert negative hexadecimal to decimal
- Why am I getting “undefined reference to sqrt” error even though I inclu de math.h header?
- How to initialize a struct in accordance with C programming language standards
- Expression preceding parentheses?
- Linux equivalent of I_PUSH
- Understanding INADDR_ANY for socket programming
- The Definitive C Book Guide and List[
- When is it a good idea to use strdup (vs malloc / strcpy)
- What is Innermost loop in imperfectly nested loops?
- What’s wrong with my code? What is argv[1]?
- What can cause a “Resource temporarily unavailable” on sock send() command
- How to get the current directory in a C program?
- What’s the best way to check if a file exists in C?
- How to solve error: expected identifier or ‘(‘
- error: “initializer expression list treated as compound expression”
- C compile : collect2: error: ld returned 1 exit status
- Split string with delimiters in C
- c array – warning: format not a string literal
- How to pause in C?
- Getting error: ISO C++ forbids declaration of with no type
- (.text+0x20): undefined reference to `main’ and undefined reference to function
- Why am I getting this error: “data definition has no type or storage class”?
- Error: initializer element is not computable at load time
- %i or %d to print integer in C using printf()?
- How can I get the list of files in a directory using C or C++?
- How to prevent multiple definitions in C?
- Using Math.round to round to one decimal place?
- How can you print multiple variables inside a string using printf?
- Multi-character constant warnings
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- Return a `struct` from a function in C
- C dynamically growing array
- Why should we check WIFEXITED after wait in order to kill child processes in Linux system call?
- Anagrams finder in javascript
- Python: Can a function return an array and a variable?
- How to return a boolean method in java?
- What is define([ , function ]) in JavaScript?
- Round *UP* to the nearest 100 in SQL Server
- Why can’t we pass arrays to function by value?
- How to parse in C
- Stopping a JavaScript function when a certain condition is met
- error: unknown type name ‘bool’
- Using ‘printf’ on a variable in C [closed]
- “warning: useless storage class specifier in empty declaration” in struct
- How to allocate array of pointers for strings by malloc in C?
- Is there a math nCr function in python? [duplicate]
- Why are there no hashtables in the C standard library?
- What do \t and \b do?
- “cannot be used as a function error”
- write() to stdout and printf output not interleaved?
- Write a program that asks the user to enter five test scores. Correspond it to a letter grade
- Difference between “while” loop and “do while” loop
- Swapping 2 Bytes of Integer
- GDB no such file or directory
- Level vs Edge Trigger Network Event Mechanisms
- How to convert integers to characters in C?
- variably modified array at file scope in C