Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I want to list data by alphabetic

Reply
Thread Tools

I want to list data by alphabetic

 
 
emre esirik(hacettepe computer science and engineering)
Guest
Posts: n/a
 
      12-02-2007
I used a structer in this program.
typedef struct _guitar {
int serial; :serial of item at the guitar store
int price; rice 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?
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      12-02-2007
On Dec 2, 12:35 pm, "emre esirik(hacettepe computer science and
engineering)" <(E-Mail Removed)> 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().
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      12-02-2007
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; rice 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

$

 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      12-02-2007

santosh <(E-Mail Removed)> wrote in message
news:fiu7v0$vfa$(E-Mail Removed)...

> 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



 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      12-02-2007

Bill Reid <(E-Mail Removed)> wrote in message
news:goG4j.190468$(E-Mail Removed)...
>
> santosh <(E-Mail Removed)> wrote in message
> news:fiu7v0$vfa$(E-Mail Removed)...
>
> > 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



 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-03-2007
Bill Reid wrote:

>
> santosh <(E-Mail Removed)> wrote in message
> news:fiu7v0$vfa$(E-Mail Removed)...
>
>> 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...

 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      12-17-2007
On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"
<(E-Mail Removed)> 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
 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      04-09-2008

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

David Thompson <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"
> <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open & Save dialog box shows files in reverse alphabetic order (Z-A) John Stubbs Computer Support 17 11-25-2009 02:30 PM
Efficient way of generating original alphabetic strings like unix file "split" py_genetic Python 6 06-19-2007 12:23 AM
How to: Windows Quick Tip - How to Force Alphabetic Sorting Silverstrand Reviews & How-To's 3 08-23-2005 08:59 AM
MAJOR problem with alphabetic paging =?Utf-8?B?VGltOjouLg==?= ASP .Net 0 07-26-2005 08:51 AM
list in alphabetic order steve HTML 7 09-27-2004 05:18 PM



Advertisments