How to print numbers?????

This is a discussion on How to print numbers????? within the C Programming forums, part of the General Programming Boards category; Anyone cares to give me some ideas.....?? I've an array of 4 values ==> array[0] = 3.40 array[1] = 4.00 ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    32

    How to print numbers?????

    Anyone cares to give me some ideas.....??

    I've an array of 4 values

    ==>
    array[0] = 3.40
    array[1] = 4.00
    array[2] = 1.30
    array[3] = 1.29

    I'm supposed to print the number of the arrays into a file according to their values in an ascendinng order....
    Code:
    What I'm expected to print into the file...
    i.e:  3
          2
          0
          1
    BUT....the problem is, when I sort the array, the array number will be gone and instead of printing 3,2,0,1......0,1,2,3 will be printed...

    __________

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You dont need to sort the array to do what you are doing. Just do the following:

    Find lowest number in the array, and print that index to the file.
    Fine next lowest number in the array, and print that index to the file.
    etc...

    As you can see, you never actually sort the array.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by AssistMe
    BUT....the problem is, when I sort the array, the array number will be gone and instead of printing 3,2,0,1......0,1,2,3 will be printed...
    You have your array. Make an array of pointers to each array element. Sort the array of pointers based on the value they point to. Then take the difference of each pointer value in the sorted pointer array from the start of the array.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int compare(const void *a, const void *b)
    {
       const float *const *x = a, *const *y = b;
       return **x > **y ? 1 : **x < **y ? -1 : 0;
    }
    
    int main(void)
    {
       float array[] = { 3.40, 4.00, 1.30, 1.29 };
       float *ptr [ sizeof array / sizeof *array ];
       size_t i;
       for (i = 0; i < sizeof ptr / sizeof *ptr; ++i )
       {
          ptr[i] = &array[i];
          printf("%d (%g)\n", ptr[i] - array, *ptr[i]);
       }
       puts("--qsort--");
       qsort(ptr, sizeof ptr / sizeof *ptr, sizeof *ptr, compare);
       for (i = 0; i < sizeof ptr / sizeof *ptr; ++i )
       {
          printf("%d (%g)\n", ptr[i] - array, *ptr[i]);
       }
       return 0;
    }
    
    /* my output
    0 (3.4)
    1 (4)
    2 (1.3)
    3 (1.29)
    --qsort--
    3 (1.29)
    2 (1.3)
    0 (3.4)
    1 (4)
    */
    Last edited by Dave_Sinkula; 03-04-2005 at 02:47 PM. Reason: [1] Applied annoying color thingy. [2] Changed ptr initialization. [3] Finally noticed typo 6 hours later.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. Print numbers by inorder..
    By NightWalker in forum C Programming
    Replies: 5
    Last Post: 09-15-2003, 10:24 AM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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