0

I am trying to call a function that I created. That will take a double then round them to two decimal places.

I have tried to put the parameter "x" in the argument of "roundnum" but it would result in an error.

#include <stdio.h>
#include <math.h>

double roundnum(double x);

int
main(void)
{

    double n;
    int roundedn;
    printf("Enter the number:\n");
    scanf("%lf", &n);

    printf("Rounded %.2f\n", roundnum(n));


    return 0;
}

double
roundnum(double x)
{
    double n;
    int number = (int)(n + .5);
    return number;
}

If I scan in 3.145, the expected output should be 3.15000.

Dre_04
  • 1
  • 1
    You are returning `int` from a function which is supposed to return `double`? – Eugene Sh. Jun 10 '19 at 18:41
  • No; the output should be `3.00` because you forgot to multiply by `100.0` before rounding and then dividing by `100.0`. And getting `3.15` might still be problematic. Your solution isn't general enough for `double` values big enough that they don't have an integer representation. Negative numbers present another problem. – Jonathan Leffler Jun 10 '19 at 18:50
  • 1
    Possible duplicate of [Rounding Number to 2 Decimal Places in C](https://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c) – fassn Jun 10 '19 at 19:12

3 Answers3

0

It's hard to help if you don't give us the error message you're receiving. Without flags this code compiles just fine.

A couple of mistakes I noticed:

  1. You are not using the argument passed to roundnum(). I bet you wanted to write:
double
roundnum(double x)
{
    return (x + .5);
}
  1. As corrected in my code above as I simply got rid of it, double n isn't initialized.

  2. int roundedn is unused therefore useless.

fassn
  • 299
  • 1
  • 5
  • 15
  • You haven't coerced `(x + 0.5)` to an `int` — which is the crucial step in the processing. Otherwise, good. (Formatting: you can indent the code by 8 spaces instead of 4 to have it indented at the same level as the bullet points.) – Jonathan Leffler Jun 10 '19 at 18:58
  • @fassn, (1) this question is not about 'error message' , it is about rounding of a double variable (2) returning (x+0.5) doesn't round to 2 decimal places (3) if x is a double, and the function is returning a double, then even adding a fraction doesn't actually mean rounding. for e.g. if x = 0.05123123123.., and we add 0.5 to it, it results in 0.55123123123 which would be returned. This is not the rounded-off resul. Please edit your code to reflect these problems, else it is not the correct solution. – ARD Jun 10 '19 at 19:02
0

I believe that in your roundnum() function, you never assigned the incoming parameter value n to the variable x that is used in roundnum(). So x is never assigned and defaults to zero.

Logically, once the value of x is assigned to n the function works:

double
roundnum(double x)
{
    double n;
    n=x;
    int number = (int)(n + .5);
    return number;
}

And to simplify the function for better efficiency:

double
roundnum(double x)
{
    return((int)(x + .5));
}
vezunchik
  • 3,669
  • 3
  • 16
  • 25
0

To begin with, there are multiple mistakes in this function:

double roundnum(double x);
  • That function shall return a double and you return an intinstead
  • It is clear that you haven't understood how parameters are used. When you call roundnum(n)you are passing a local copy of n stated inside main's body to variable x which is the parameter of roundnum() . The double n of main() has nothing to do with the double n of roundnum(). Basically in roundnum(), you are using an uninitialized variable which is a logical error. The number you want to round is basically x.

This is the function with the above mistakes corrected:

double roundnum(double x)
{
    double number = (x + .5);
    return number;
}

Now let's talk about why this rounding function is wrong by terms of logic. You want a 2-decimal round but in reality you are just increasing any number with 0.5. So what if I put as input the number 3.150 ? It's already rounded but the function would return 3.65 which is a mistake. I won't tell you how to fix this issue , consider it homework ;)

NickDelta
  • 3,697
  • 3
  • 15
  • 25