for example you are asked to input 10 numbers e.g. 1.23, 7.453, 45.7, 345 etc how are you going to convert these floats into int
I would like to use them with quicksort without using #include <stdlib.h>
Thanks!
for example you are asked to input 10 numbers e.g. 1.23, 7.453, 45.7, 345 etc how are you going to convert these floats into int
I would like to use them with quicksort without using #include <stdlib.h>
Thanks!
Why do you need to convert them to int? Shouldn't your implementation of quicksort handle these numbers as float or double?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I believe multiply the number by 100 or 1000 is the answer. Then divide by int 1 to get rid of any extra digits after the decimal place.
cause the program i made was using int and i have problems in converting it where floats are involved
Then you should resolve those problems instead of coming up with an incorrect program by trying to convert those numbers to int. So, what are the problems that you face?Originally Posted by Bebi
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
laserlight is the solution proposed by adak acceptable? just looking for a 2nd opinion. thanks a lot
Adak's solution is designed to work on your example input, but is your example input truly representative of the actual input to your program? For example, can 1.111111 and 1.111122 be entered as input? If so, would you consider them to be equal?Originally Posted by Bebi
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Generally speaking, you don't want to convert a float to an int - that's why the variable is a float in the first place - you need that string of digits after the decimal place.
You don't need to include stdlib to use Quicksort - quicksort is an algorithm apart from qsort() in the standard library.
post up your problem, and let's see about doing it right - my answer may be "correct", for the exact question you asked, but it's also, almost always wrong, as well.
I have this program and was wondering how to make it accept floating numbers instead of int
This is my first problem. can you have an array for floats?
Code:#include <stdio.h> float ES26[11]; int i, j, k, x=1, mean, median, mode; float *ptr, sum; int main () { int choice; printf("Input at most 10 floating point numbers, each entry followed by ENTER. \nInput 0 if you want to stop:\n"); for (i=1, j=1; j==1 || i==10; i++) { printf("%d) ", i); scanf("%i", &ES26[i]); if (ES26[i]==0) { x=i-1; j=2; } if (i==10) { x=i; j=2; } } }
You are reading as an integer here:
Use %f instead.Code:scanf("%i", &ES26[i]);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)