Help in converting float instead of int in quicksort program

This is a discussion on Help in converting float instead of int in quicksort program within the C Programming forums, part of the General Programming Boards category; for example you are asked to input 10 numbers e.g. 1.23, 7.453, 45.7, 345 etc how are you going to ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Help in converting float instead of int in quicksort program

    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!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Why do you need to convert them to int? Shouldn't your implementation of quicksort handle these numbers as float or double?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    cause the program i made was using int and i have problems in converting it where floats are involved

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by Bebi
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    laserlight is the solution proposed by adak acceptable? just looking for a 2nd opinion. thanks a lot

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by Bebi
    laserlight is the solution proposed by adak acceptable?
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    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;
            }
        }
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    You are reading as an integer here:
    Code:
    scanf("%i", &ES26[i]);
    Use %f instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with this question:
    By marcuspax in forum C Programming
    Replies: 16
    Last Post: 08-16-2010, 08:12 AM
  2. linker 2019 issues
    By werdy666 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2009, 03:12 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21