I want to list data by alphabetic

Discussion in 'C Programming' started by emre esirik(hacettepe computer science and enginee, Dec 2, 2007.

  1. I used a structer in this program.
    typedef struct _guitar {
    int serial; :serial of item at the guitar store
    int price; :price of item at the guitar store
    char builder[18]; :builder of item at the guitar store
    char type[18]; :type of item at the guitar store
    char model[18]; :model of item at the guitar
    store
    }guitar;


    I want to list according to alphabetic by model. How can I do, do you
    know any algorithm?
     
    emre esirik(hacettepe computer science and enginee, Dec 2, 2007
    #1
    1. Advertising

  2. emre esirik(hacettepe computer science and enginee

    Guest

    On Dec 2, 12:35 pm, "emre esirik(hacettepe computer science and
    engineering)" <> wrote:
    > I used a structer in this program.
    > <invalid C>
    >
    > I want to list according to alphabetic by model. How can I do, do you
    > know any algorithm?


    you can use qsort() and strcmp().
     
    , Dec 2, 2007
    #2
    1. Advertising

  3. emre esirik(hacettepe computer science and enginee

    santosh Guest

    emre esirik(hacettepe computer science and engineering) wrote:

    > I used a structer in this program.
    > typedef struct _guitar {


    I recommend not using an underscore here, as identifiers beginning with
    an underscore (or double underscores) may be used by the
    implementation.

    > int serial; :serial of item at the guitar store
    > int price; :price of item at the guitar store


    unsigned would be a better choice as both values are not going to be
    negative.

    > char builder[18]; :builder of item at the guitar store
    > char type[18]; :type of item at the guitar store
    > char model[18]; :model of item at the guitar store


    These arrays are fine if you are absolutely certain that the data you
    are going to put into them will never exceed their bounds. Otherwise
    you need to either declare them of sufficient size or, instead of
    arrays, make 'builder', 'type' and 'model' to be pointers to char and
    allocate memory dynamically with malloc(). This allows any arbitrary
    sized data to be stored with either buffer overflow or memory wastage.
    Nor do you need to know these details in advance.

    > }guitar;
    >
    >
    > I want to list according to alphabetic by model. How can I do, do you
    > know any algorithm?


    I presume you have an array of these structures? You can easily sort
    them based on the value of 'model' by using the Standard library
    function qsort(). You need to provide it a "comparison function"
    though. This function will be called by qsort() with two elements of
    the array to be sorted, and it must return an int value less than,
    equal to, or greater than zero according to whether it's first argument
    is less than, equal to, or greater than it's second argument. How
    exactly you do this comparison will of course depend on the exact
    format of the array members and your sorting criteria.

    Example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define NUM_GUITARS 3

    struct guitar {
    int serial;
    int price;
    char builder[18];
    char type[18];
    char model[18];
    };

    int sort_guitar(const void *n, const void *m) {
    return strcmp(((struct guitar *)n)->model,
    ((struct guitar *)m)->model);
    }

    void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    unsigned ctr;

    fprintf(s, "Guitars:\n========\n\n");
    for (ctr = 0; ctr < num; ctr++) {
    fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    "%s\nModel:\t\t%s\n\n", (arr+ctr)->serial,
    (arr+ctr)->price, (arr+ctr)->builder, (arr+ctr)->type,
    (arr+ctr)->model);
    }
    return;
    }

    int main(void) {
    struct guitar arr[NUM_GUITARS] = {
    { 1, 1000, "Michealangelo", "foo", "MM111" },
    { 2, 920, "Leonardo", "bar", "ML112" },
    { 3, 1500, "Raphael", "baz", "MR123" } };

    print_guitars(arr, NUM_GUITARS, stdout);
    qsort(arr, NUM_GUITARS, sizeof arr[0], sort_guitar);
    print_guitars(arr, NUM_GUITARS, stdout);
    return 0;
    }

    $ gcc -Wall -Wextra -ansi -pedantic -o guitars guitars.c
    $ ./guitars
    Guitars:
    ========

    Serial: 1
    Price: 1000
    Builder: Michealangelo
    Type: foo
    Model: MM111

    Serial: 2
    Price: 920
    Builder: Leonardo
    Type: bar
    Model: ML112

    Serial: 3
    Price: 1500
    Builder: Raphael
    Type: baz
    Model: MR123

    Guitars:
    ========

    Serial: 2
    Price: 920
    Builder: Leonardo
    Type: bar
    Model: ML112

    Serial: 1
    Price: 1000
    Builder: Michealangelo
    Type: foo
    Model: MM111

    Serial: 3
    Price: 1500
    Builder: Raphael
    Type: baz
    Model: MR123

    $
     
    santosh, Dec 2, 2007
    #3
  4. emre esirik(hacettepe computer science and enginee

    Bill Reid Guest

    santosh <> wrote in message
    news:fiu7v0$vfa$...

    > void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    > unsigned ctr;
    >
    > fprintf(s, "Guitars:\n========\n\n");
    > for (ctr = 0; ctr < num; ctr++) {
    > fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    > "%s\nModel:\t\t%s\n\n", (arr+ctr)->serial,
    > (arr+ctr)->price, (arr+ctr)->builder, (arr+ctr)->type,
    > (arr+ctr)->model);
    > }
    > return;
    > }


    Hmmm...I always do this slightly differently, because I believe
    (rightly or wrongly) that I save a few cycles by not repeating the
    pointer arithmetic:

    void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    unsigned ctr;
    struct guitar *current_guitar=arr;

    fprintf(s, "Guitars:\n========\n\n");
    for (ctr = 0; ctr < num; ctr++) {
    current_guitar+=ctr;
    fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    "%s\nModel:\t\t%s\n\n", current_guitar->serial,
    current_guitar->price, current_guitar->builder,
    current_guitar->type,current_guitar->model);
    }

    return;
    }

    Even it it does save a few cycles, it only saves a FEW...but
    does it? I don't know...

    ---
    William Ernest Reid
     
    Bill Reid, Dec 2, 2007
    #4
  5. emre esirik(hacettepe computer science and enginee

    Bill Reid Guest

    Bill Reid <> wrote in message
    news:goG4j.190468$...
    >
    > santosh <> wrote in message
    > news:fiu7v0$vfa$...
    >
    > > void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    > > unsigned ctr;
    > >
    > > fprintf(s, "Guitars:\n========\n\n");
    > > for (ctr = 0; ctr < num; ctr++) {
    > > fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    > > "%s\nModel:\t\t%s\n\n", (arr+ctr)->serial,
    > > (arr+ctr)->price, (arr+ctr)->builder, (arr+ctr)->type,
    > > (arr+ctr)->model);
    > > }
    > > return;
    > > }

    >
    > Hmmm...I always do this slightly differently, because I believe
    > (rightly or wrongly) that I save a few cycles by not repeating the
    > pointer arithmetic:
    >
    > void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    > unsigned ctr;
    > struct guitar *current_guitar=arr;
    >
    > fprintf(s, "Guitars:\n========\n\n");
    > for (ctr = 0; ctr < num; ctr++) {
    > current_guitar+=ctr;
    > fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    > "%s\nModel:\t\t%s\n\n", current_guitar->serial,
    > current_guitar->price, current_guitar->builder,
    > current_guitar->type,current_guitar->model);
    > }
    >
    > return;
    > }
    >
    > Even it it does save a few cycles, it only saves a FEW...but
    > does it? I don't know...
    >

    Of course, what I MEANT to say was:

    void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    unsigned ctr;
    struct guitar *current_guitar;

    fprintf(s, "Guitars:\n========\n\n");
    for (ctr = 0; ctr < num; ctr++) {
    current_guitar=arr+ctr;
    fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    "%s\nModel:\t\t%s\n\n", current_guitar->serial,
    current_guitar->price, current_guitar->builder,
    current_guitar->type,current_guitar->model);
    }

    return;
    }

    Or SOMETHING like that...sheesh...

    ---
    William Ernest Reid
     
    Bill Reid, Dec 2, 2007
    #5
  6. emre esirik(hacettepe computer science and enginee

    santosh Guest

    Bill Reid wrote:

    >
    > santosh <> wrote in message
    > news:fiu7v0$vfa$...
    >
    >> void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    >> unsigned ctr;
    >>
    >> fprintf(s, "Guitars:\n========\n\n");
    >> for (ctr = 0; ctr < num; ctr++) {
    >> fprintf(s, "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    >> "%s\nModel:\t\t%s\n\n", (arr+ctr)->serial,
    >> (arr+ctr)->price, (arr+ctr)->builder, (arr+ctr)->type,
    >> (arr+ctr)->model);
    >> }
    >> return;
    >> }

    >
    > Hmmm...I always do this slightly differently, because I believe
    > (rightly or wrongly) that I save a few cycles by not repeating the
    > pointer arithmetic:
    >
    > void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    > unsigned ctr;
    > struct guitar *current_guitar=arr;
    >
    > fprintf(s, "Guitars:\n========\n\n");
    > for (ctr = 0; ctr < num; ctr++) {
    > current_guitar+=ctr;
    > fprintf(s,
    > "Serial:\t\t%d\nPrice:\t\t%d\nBuilder:\t%s\nType:\t\t"
    > "%s\nModel:\t\t%s\n\n", current_guitar->serial,
    > current_guitar->price, current_guitar->builder,
    > current_guitar->type,current_guitar->model);
    > }
    >
    > return;
    > }
    >
    > Even it it does save a few cycles, it only saves a FEW...but
    > does it? I don't know...


    For an optimising compiler, both code snippets probably generate
    identical code. Maybe I should test them here...
     
    santosh, Dec 3, 2007
    #6
  7. On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"
    <> wrote:
    <snip>
    > Hmmm...I always do this slightly differently, because I believe
    > (rightly or wrongly) that I save a few cycles by not repeating the
    > pointer arithmetic:
    >
    > void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    > unsigned ctr;
    > struct guitar *current_guitar=arr;
    >
    > fprintf(s, "Guitars:\n========\n\n");
    > for (ctr = 0; ctr < num; ctr++) {
    > current_guitar+=ctr;


    Corrected to current_guitar = arr+ctr;

    <snip stuff using current_guitar->various>

    > Even it it does save a few cycles, it only saves a FEW...but
    > does it? I don't know...
    >

    Unless you are using a compiler more than 20 years old or a toy, it
    probably doesn't make any difference in the compiled code; this is one
    of the oldest and simplest known optimizations.

    But I often do it this way, or close, for CLARITY. Especially in more
    complicated cases where I have perhaps 2D or 3D subscripts, or
    sometimes several (different) subscripts into the same or different
    arrays. This is a little easier to _write_ correctly, but more
    importantly easier to _read_ and see that I wrote what I meant.

    Similar approaches include ones like:
    for( ptr = base, i = 0; i < num; ++ptr, ++i ) ...
    for( ptr = base, i = num; i; ++ptr, --i ) ...
    for( ptr = base, end = base + num; ptr < end; ++ptr ) ...
    (with more specific names as appropriate of course).

    - formerly david.thompson1 || achar(64) || worldnet.att.net
     
    David Thompson, Dec 17, 2007
    #7
  8. emre esirik(hacettepe computer science and enginee

    Bill Reid Guest

    I've decided to make this my day for replying to months-old
    posts...I wonder if anybody will troll this one...

    David Thompson <> wrote in message
    news:...
    > On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"
    > <> wrote:
    > <snip>
    > > Hmmm...I always do this slightly differently, because I believe
    > > (rightly or wrongly) that I save a few cycles by not repeating the
    > > pointer arithmetic:
    > >
    > > void print_guitars(struct guitar *arr, unsigned num, FILE *s) {
    > > unsigned ctr;
    > > struct guitar *current_guitar=arr;
    > >
    > > fprintf(s, "Guitars:\n========\n\n");
    > > for (ctr = 0; ctr < num; ctr++) {
    > > current_guitar+=ctr;

    >
    > Corrected to current_guitar = arr+ctr;
    >
    > <snip stuff using current_guitar->various>
    >
    > > Even it it does save a few cycles, it only saves a FEW...but
    > > does it? I don't know...
    > >

    > Unless you are using a compiler more than 20 years old or a toy, it
    > probably doesn't make any difference in the compiled code; this is one
    > of the oldest and simplest known optimizations.


    What about if I routinely turn off ALL optimizations in most of my
    code because I'm so paranoid about floating-point errors? It's an
    option, and I kind of absent-mindedly use it a lot, probably a lot of
    times without compelling reason...strangely, it SEEMS my code
    runs faster, and even more strangely, SEEMS smaller, when I turn
    off both "speed" and "size" optimizations...and also, I routinely turn
    off all the fancy-schmancy options for pre-compiling and caching
    headers that is supposed to speed up compiling, and it SEEMS
    like that dramatically DECREASES the compile time of at least
    some of my code (but DEFINITELY increases other parts)...go figure...

    > But I often do it this way, or close, for CLARITY. Especially in more
    > complicated cases where I have perhaps 2D or 3D subscripts, or
    > sometimes several (different) subscripts into the same or different
    > arrays. This is a little easier to _write_ correctly, but more
    > importantly easier to _read_ and see that I wrote what I meant.
    >
    > Similar approaches include ones like:
    > for( ptr = base, i = 0; i < num; ++ptr, ++i ) ...


    Ah yes, this is actually what I REALLY use the most for the
    actual example, a structure that has a list of items and a reliable
    count of the items in the list...

    > for( ptr = base, i = num; i; ++ptr, --i ) ...
    > for( ptr = base, end = base + num; ptr < end; ++ptr ) ...
    > (with more specific names as appropriate of course).


    Much more specific in some cases, in some cases I've tried
    to use generic library functions with callbacks to reduce the
    redundancy of doing this kind of stuff over and over again...

    ---
    William Ernest Reid
     
    Bill Reid, Apr 9, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    MAJOR problem with alphabetic paging

    =?Utf-8?B?VGltOjouLg==?=, Jul 26, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    495
    =?Utf-8?B?VGltOjouLg==?=
    Jul 26, 2005
  2. steve

    list in alphabetic order

    steve, Sep 26, 2004, in forum: HTML
    Replies:
    7
    Views:
    611
    steve
    Sep 27, 2004
  3. py_genetic
    Replies:
    6
    Views:
    325
    py_genetic
    Jun 19, 2007
  4. Sandhya Prabhakaran
    Replies:
    6
    Views:
    583
    alex23
    Aug 3, 2009
  5. Ian Richardson

    true alphabetic sort...

    Ian Richardson, Apr 24, 2004, in forum: Javascript
    Replies:
    13
    Views:
    920
    Thomas 'PointedEars' Lahn
    May 5, 2004
Loading...

Share This Page