A
Allie
How would I go about sorting this structure by title?
typedef struct {
char author[40];
char title[40];
char code[4];
int hold;
int loan;
} LIBRARY;
LIBRARY book[N];
This is what I've written:
void sortbytitle( LIBRARY *b, int n ) { /* n = number of books in stock
*/
LIBRARY temp[N];
int i;
for( i = 0; i < n; i++ ) {
if( strcmp( b.title, b[i + 1].title ) > 0 ) {
temp = b;
b = b[i + 1];
b[i + 1] = temp;
}
}
return;
}
I get no errors or warnings when I compile using Bloodshed Dev-C++.
However, my output then looks like this:
AUTHOR TITLE CODE #COPY #BORR #AVAIL
Golumbic Graph Theory G01 5 2 3
Jacobs Database Logic J01 3 1 2
Kanter Management Information K01 8 1 7
Kuo Numerical Methods K02 2 0 2
Habermann Operating Systems H01 10 5 5
Schroder C S01 7 2 5
Herzog Computing Structures H03 10 7 3
Holub Compiler Design H05 11 8 3
Galvin Operating Systems G02 15 2 13
Lane Data Communications L01 20 3 17
Kleinrock Queueing Systems K03 14 0 14
Kindred Data Systems K04 2 0 2
Horowitz Programming Languages H06 16 10 6
(Original output, before sort is called:
AUTHOR TITLE CODE #COPY #BORR #AVAIL
Habermann Operating Systems H01 10 5 5
Golumbic Graph Theory G01 5 2 3
Jacobs Database Logic J01 3 1 2
Kanter Management Information K01 8 1 7
Kuo Numerical Methods K02 2 0 2
Hughs Structured Programming H02 4 4 0
Schroder C S01 7 2 5
Herzog Computing Structures H03 10 7 3
Hunter Understanding C H04 6 6 0
Holub Compiler Design H05 11 8 3
Galvin Operating Systems G02 15 2 13
Lane Data Communications L01 20 3 17
Kleinrock Queueing Systems K03 14 0 14
Kindred Data Systems K04 2 0 2
Mano Computer Architecture M01 2 2 0
Horowitz Programming Languages H06 16 10 6
The sortbytitle() function is used in conjunction with
printbyavail()... which explains the three missing books in the output
produced by sortybytitle().)
The titles aren't sorted properly! What am I doing wrong?
Thank you muchly
--Allie
typedef struct {
char author[40];
char title[40];
char code[4];
int hold;
int loan;
} LIBRARY;
LIBRARY book[N];
This is what I've written:
void sortbytitle( LIBRARY *b, int n ) { /* n = number of books in stock
*/
LIBRARY temp[N];
int i;
for( i = 0; i < n; i++ ) {
if( strcmp( b.title, b[i + 1].title ) > 0 ) {
temp = b;
b = b[i + 1];
b[i + 1] = temp;
}
}
return;
}
I get no errors or warnings when I compile using Bloodshed Dev-C++.
However, my output then looks like this:
AUTHOR TITLE CODE #COPY #BORR #AVAIL
Golumbic Graph Theory G01 5 2 3
Jacobs Database Logic J01 3 1 2
Kanter Management Information K01 8 1 7
Kuo Numerical Methods K02 2 0 2
Habermann Operating Systems H01 10 5 5
Schroder C S01 7 2 5
Herzog Computing Structures H03 10 7 3
Holub Compiler Design H05 11 8 3
Galvin Operating Systems G02 15 2 13
Lane Data Communications L01 20 3 17
Kleinrock Queueing Systems K03 14 0 14
Kindred Data Systems K04 2 0 2
Horowitz Programming Languages H06 16 10 6
(Original output, before sort is called:
AUTHOR TITLE CODE #COPY #BORR #AVAIL
Habermann Operating Systems H01 10 5 5
Golumbic Graph Theory G01 5 2 3
Jacobs Database Logic J01 3 1 2
Kanter Management Information K01 8 1 7
Kuo Numerical Methods K02 2 0 2
Hughs Structured Programming H02 4 4 0
Schroder C S01 7 2 5
Herzog Computing Structures H03 10 7 3
Hunter Understanding C H04 6 6 0
Holub Compiler Design H05 11 8 3
Galvin Operating Systems G02 15 2 13
Lane Data Communications L01 20 3 17
Kleinrock Queueing Systems K03 14 0 14
Kindred Data Systems K04 2 0 2
Mano Computer Architecture M01 2 2 0
Horowitz Programming Languages H06 16 10 6
The sortbytitle() function is used in conjunction with
printbyavail()... which explains the three missing books in the output
produced by sortybytitle().)
The titles aren't sorted properly! What am I doing wrong?
Thank you muchly
--Allie