1. ## math.h question

I apologize in advance for being so new at this, and for any terminology errors. Feel free to point them out; I will not be insulted.

I have very recently started learning C, and I am trying to write a program using math.h. I need to round a float variable to a certain number of decimal places. I was under the impression that the round() function would serve my purposes adequately, but it seems to only work for rounding to the nearest whole number.

Does anyone know what I can do?

2. Search (the boards, google) for "round".

Alternatively, are you supposed to round the value of the variable itself, or just print it out to a certain number of places? (Not the same thing.)

3. Essentially, I need to check to see if a certain variable, already defined as the quotient of two earlier variables, is a whole number. There may be a simpler method, but what I was attempting to do was to store the variable, rounded to the nearest whole number with no decimal places, as another variable, then use an if/else function to check if they are equal to one another.

Hmmm... I think I may have just figured out a way to do it. Let me try it, and I will let you know momentarily.

Bingo! Thanks for the help!

4. All right, I retract my bingo. I was trying to use

while( round(k) == k)...

to mean "while k is a whole number". That appears to put the program into an infinite loop.

Any idea how I can restate it?

5. Post a more complete example of what you're trying to do.

6. Well, the program is not very long, so I suppose I can simply post it in its entirety. Please bear with me; this is my first time using code tags, and I am not entirely sure that I am posting correctly.

The intent of the program is to take the scanned number, q, and print its prime factors. The problem is apparently occurring in the while loop, which is intended to remove and print each factor.

Code:
```#include <stdio.h>
#include <math.h>

main()
{
int d = 1, q, p, e = 0;
float k;

scanf( "%d", &q);
for( d <= (sqrt(p)); ++d;) {
p = q;
k = p / d;
while( round(k) == k) {
e = d;
printf("%d ", e);
p = k;
k = p / d;
}
}
if( p == q )
printf( "%d ", q);
if( p != q && p != 1 )
printf( "%d ", p);
}```

7. Are you sure your for-loop isn't the infinite loop? Given that it has no way to stop until ++d equals zero.....

8. It very well could be. I just realized my "greater than or equal to" sign should be "less than or equal to". I am editing, but the same problem continues to occur.

9. 1) main returns an int.
2) 'p' is uninitialized
3) Your for-loop is set up incorrectly. It should be: for( /*initialization*/; /*comparison*/; /*increment*/ )
4) The sense of the for-loop comparison should be reversed, eg: loop until 'd' reaches sqrt(q) (note: not 'p'!)
5) Fix those problems, and then print the value of each variable each iteration of the loop so that you can spot the many logic errors in the program.

10. Thank you. I believe I can rectify all errors, save #1. To what does that refer?

11. >> To what does that refer?

The C standard mandates that 'main' return an int. Some compilers allow you to omit it, either by extension or simply because they're broken.

12. How do I repair the error?

int main() ?

13. You got it.

14. Well, I got tired of my ineffectual attempts to fix the problem, so i rewrote it, making the variables' names a bit easier to remember, and thereby work with. However, my infinite loop problem continues. Any number that I input that bypasses the while loop returns the "prime" result, and anything else prints a continual sequence of twos.

I believe the problem may lie in the highlighted line, but I am not sure.

Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int number, factor = 2;
float remain, valid = 1, test;

printf("Enter number\n");
scanf("%d", &number);
remain = number;

while(factor <= sqrt(number)) {
test = number / factor;
valid = round(test) - test;
if(valid == 0) {
remain = test;
printf("%d", factor);
remain = remain / factor;
}
else factor = factor + 1;
}

if(remain == number)
printf("prime\n");
if(remain != number && remain != 1)
printf("%.0f", remain);
}```
Just a side note: the reason I used only letters in the first draft was that I was transposing this program from another programming language, which only allows them.

15. The highlighted line is not a problem. Think about what's going on for six seconds: your loop depends on factor. If your if statement is true, how does factor change?