Printing an array

This is a discussion on Printing an array within the C Programming forums, part of the General Programming Boards category; I have an array who's values were inserted unsorted. Is it possible to print the values from least to greatest ...

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

    Printing an array

    I have an array who's values were inserted unsorted. Is it possible to print the values from least to greatest or do I need to use insertion sort?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    It can be done without sorting, though it's usually easier if you do sort it.

    Can't you use qsort() in stdlib.h ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    I'll try to see if I can go back and sort on insertion. It's just that I am using a structure that is filled with different types of data from multiple text files and I had finally gotten everything imputed correctly... then I remembered I needed to print it all out sorted by one of the part. I should have planned better. Thinking about touching that part of the code makes me want to bang my head against the wall...

    edit:

    I was looking through my book and found an example that uses sorts in descending order. However, when I try to modify it to sort in ascending order it I can't get it to work correctly...
    How can I modify this so it sorts from least to greatest?

    struct{
    char name [40];
    int number;
    float salary;
    }

    ......



    int main(void)
    {......
    data[100].........
    }

    void bubble(struct employee data[])
    {
    int i, dum;
    for (dum = 0; dum < sizeof(data-1); dum++)
    for (i = 0; i < sizeof(data-1); i++)
    if (data[i].number < data[i+1].number)
    swap(&data[i], &data[i+1]);
    }

    void swap(struct employee *data1, struct employee *data2)
    {
    struct employee temp;
    temp = *data2;
    *data2 = *data1;
    *data1 = temp;
    }
    Last edited by LoveTractor; 11-21-2010 at 02:24 PM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    I was looking through my book and found an example that uses sorts in descending order. However, when I try to modify it to sort in ascending order it I can't get it to work correctly...
    How can I modify this so it sorts from least to greatest?

    Code:
    struct{
        char name [40];
        int number;
        float salary;
    }
    
    ......
    
    
    
    int main(void)
    {......
         data[100].........
    }
    
    void bubble(struct employee data[]) 
    {
       int i, dum;
       for (dum = 0; dum < sizeof(data-1); dum++)
          for (i = 0; i < sizeof(data-1); i++)
             if (data[i].number < data[i+1].number)
                swap(&data[i], &data[i+1]);
    }
    
    void swap(struct employee *data1, struct employee *data2)
    {
    struct employee temp;
    temp = *data2;
    *data2 = *data1;
    *data1 = temp;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well the big problem at the moment is using sizeof() in an attempt to measure the length of the array.

    If that is in the book, then the book is wrong.
    Pass the actual length of the array as another parameter to your sort function, and use that parameter in your loop limits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Quote Originally Posted by Salem View Post
    Well the big problem at the moment is using sizeof() in an attempt to measure the length of the array.

    If that is in the book, then the book is wrong.
    Pass the actual length of the array as another parameter to your sort function, and use that parameter in your loop limits.
    Thanks, now that you pointed it out it seems obvious! I've been working on this so long my brain isn't working right. Having another set of eyes looking at the code is helpful, thanks!
    Last edited by LoveTractor; 11-21-2010 at 03:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing 2D Array
    By BB89 in forum C Programming
    Replies: 11
    Last Post: 11-16-2009, 04:33 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 01:11 AM

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