Having trouble with this assignment and my instructor has not got back to me and unfortunately I have to turn it in soon, wondering if somebody can see why this code is not working.
Instructions:
The function floor may be used to round numbers to a specific decimal place. The statement:
y = floor(x + .5 ) ; will round the number x to the nearest integer and assign the value to y
y = floor(x * 10 + .5 ) ; will round the number x to the nearest tenth position and assign the value to y
y = floor(x * 100 + .5 ) ; will round the number x to the nearest hundredth position and assign the value to y
Write a program that defines four functions to round a number x in various ways:
a) roundToInteger(number )
b) roundToTenths(number )
c) roundToHundredths(number )
d) roundToThousands(number )
For each value read your program should print the original value. The program should give the option to the user what operation wants to perform.
Problem: I use the code he gave me to do the calculations, and they are not returning the decimal, I'm not sure if it is the code he sent that is wrong or what? If anybody can steer me in the right direction it would be much appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <stdio.h>
#include <stdlib.h>
double roundToInteger(double number) {
double roundedNum;
roundedNum = floor(number + .5);
} roundedNum;
double roundToTenths(double number) {
double roundedNum;
roundedNum = floor(number * 10 + .5);
} roundedNum;
double roundToHundreths(double number) {
double roundedNum;
roundedNum = floor(number * 100 + .5);
} roundedNum;
double roundToThousandths(double number) {
double roundedNum;
roundedNum = floor(number * 1000 + .5);
} roundedNum;
main() {
double userInput = 0.0, originalVal = 0.0;
int selection = 0;
printf("Enter a double value: ");
scanf("%lf", &userInput);
originalVal = userInput;
printf("(1) Round to nearest integer.\n(2) Round to nearest tenth.\n");
printf("(3) Round to nearest hundreth.\n(4) Round to nearest thousandth.\nMake a selection: ");
scanf("%i", &selection);
switch (selection) {
case 1:
userInput = roundToInteger(userInput);
break;
case 2:
userInput = roundToTenths(userInput);
break;
case 3:
userInput = roundToHundreths(userInput);
break;
case 4:
userInput = roundToThousandths(userInput);
break;
default:
printf("Invalid selection.");
break;
}
printf("The number %lf was rounded to %lf", originalVal, userInput);
printf("\n");
system("Pause");
}
|