I want to list data by alphabetic

  • Thread starter emre esirik(hacettepe computer science and enginee
  • Start date
E

emre esirik(hacettepe computer science and enginee

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?
 
V

vippstar

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().
 
S

santosh

emre said:
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

$
 
B

Bill Reid

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...
 
B

Bill Reid

Bill Reid said:
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...
 
S

santosh

Bill said:
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...
 
D

David Thompson

On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"
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;

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
 
B

Bill Reid

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

David Thompson said:
On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"


Corrected to current_guitar = arr+ctr;


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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top