Rounding Number Problem using Floor

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");
}
closed account (E0p9LyTq)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double roundToInteger(double number)
{
   double roundedNum;
   roundedNum = floor(number + .5);
   return roundedNum;
}

double roundToTenths(double number)
{
   double roundedNum;
   roundedNum = floor(number * 10.0 + .5) / 10.0;
   return roundedNum;
}

double roundToHundreths(double number)
{
   double roundedNum;
   roundedNum = floor(number * 100.0 + .5) / 100.0;
   return roundedNum;
}

double roundToThousandths(double number)
{
   double roundedNum;
   roundedNum = floor(number * 1000.0 + .5) / 1000.0;
   return roundedNum;
}

int 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\n", originalVal, userInput);

   /* printf("\n");
   system("Pause"); */
}


Enter a double value: 123.321
(1) Round to nearest integer.
(2) Round to nearest tenth.
(3) Round to nearest hundreth.
(4) Round to nearest thousandth.
Make a selection: 3
The number 123.321000 was rounded to 123.320000
Last edited on
Ty bud.

I thought that by leaving

function (){
} returnedNumber; <----- I thought this was good enough to return.
closed account (E0p9LyTq)
You have to explicitly declare a return.

If you were creating a struct then your way of defining it would create an instance of the struct.
Okay, thanks for the help.
Topic archived. No new replies allowed.