473,383 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

I want to list data by alphabetic

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?
Dec 2 '07 #1
7 1543
On Dec 2, 12:35 pm, "emre esirik(hacettepe computer science and
engineering)" <emreesi...@gmail.comwrote:
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 '07 #2
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

$

Dec 2 '07 #3

santosh <sa*********@gmail.comwrote in message
news:fi**********@registered.motzarella.org...
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

Dec 2 '07 #4

Bill Reid <ho********@happyhealthy.netwrote in message
news:go********************@bgtnsc04-news.ops.worldnet.att.net...
>
santosh <sa*********@gmail.comwrote in message
news:fi**********@registered.motzarella.org...
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

Dec 2 '07 #5
Bill Reid wrote:
>
santosh <sa*********@gmail.comwrote in message
news:fi**********@registered.motzarella.org...
>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...

Dec 3 '07 #6
On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"
<ho********@happyhealthy.netwrote:
<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
Dec 17 '07 #7

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

David Thompson <da************@verizon.netwrote in message
news:ul********************************@4ax.com...
On Sun, 02 Dec 2007 22:32:44 GMT, "Bill Reid"
<ho********@happyhealthy.netwrote:
<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

Apr 9 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: pdb | last post by:
I want to make something so I can make a grocery list easy and just print it off. I am guessing I would have a drop down list, which would be a list of tables, then select item from the table. For...
33
by: junky_fellow | last post by:
Consider a singly-linked list. Each node has a structure, struct node { char c; struct node *next; }; Each of the nodes contain an alphabetic character. Can anybody suggest a way to print...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
6
by: Samuel Shulman | last post by:
Hi I wander how can I get the a list with all countries and the 2 characters country alphabetic code etc. Thank you, Samuel Shulman
33
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In...
1
by: gyap88 | last post by:
I m currently using sql server management studio express to manage my database. I wish to sort the data in the database table. Firstly i wish to sort column 1 in according to alphabetic order and...
3
by: PulkitZery | last post by:
Hi all, I need some help in my project (VB.NET) here is what I need: I need to convert Alphabetic number (a. b. c. d. …..) into the integers (for example a=1, b=2, c=3 and so on). Can anyone...
8
crystal2005
by: crystal2005 | last post by:
Hi guys, Just like the title, i'm looking for the example of C program that translates an alphabetic phone number into numeric. The idea as the following output Enter phone number:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.