Hi can anyone help me doing this c++ program 1. Write a program named “program .
ID: 3874323 • Letter: H
Question
Hi can anyone help me doing this c++ program
1. Write a program named “program.cpp.”
2. This program should have a main function.
3. This program should have another function named “myRoudingFunction” and this function should round up or round down a float/double value to an integer value.
4. Rules
For positive values
Round up a value if its fraction part is greater or equal to “.5”
Round down a value if its fraction part is less than “.5”
For negative values
Round down a value if its fraction part is greater or equal to “.5”
Round up a value if its fraction part is less than “.5”
You are not allowed to use the “round” function provided by C++. But, you may compare the results obtained by your rounding function and the system provided “round” function.
5. Examples
0.4 should be rounded down to 0
0.6 should be rounded up to 1
-0.4 should be rounded up to 0
-0.6 should be rounded down to -1
Explanation / Answer
#include #include using namespace std; int myRoudingFunction(double toBeRounded){ auto floorValue = (int)toBeRounded; double fraction = ((toBeRounded - floorValue) * 10); if(toBeRounded > 0){ if(fraction >= 5){ return floorValue + 1; } return floorValue; } if(fraction > -5){ return floorValue; } return floorValue - 1; } int main() { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.