The input file contains the titles and prices of books, in this form:
<TITLE><space><PRICE><comma>
<TITLE><space><PRICE><comma>
...
for example, if it contained 3 books, it would look like this:
ABC 12.31,
KDASD 35,
OPI 93.58,
So, what I want to do is: there is an array of pointers where each book will be inserted, sorted by letter and in ascending order. So this array's size will be 26.
There is an array of pointers where each book will be inserted, sorted by price and in ascending order. Prices range from 0 (not included) to 100, and this array's size will be 10, as prices will be sorted from 0-10, 10-20, etc.
How am I supposed to do this without making the code unnecessarily large? Here is what I've done so far:
Would you have a better idea as to how to do this?Code:// Data read and call of function 'insert' for each book. for (i = 0, pos = 0; (feof(f) == 0); i++) { fseek(f, pos, SEEK_CUR); fscanf(f, "%s %lf", temp_title, &temp_price); insert(ptr_titles, ptr_prices, temp_title, temp_price); } return 0; } // Memory allocation for new node and insertion. void insert (char ptr_titles[26], int ptr_prices[10], char tmp_title[], double tmp_price) { booksT *newnode; newnode = (booksT*)malloc(sizeof(booksT*)); if (newnode == NULL) { printf("Memory allocation error.\n"); exit(1); } strcpy(newnode->title, tmp_title); newnode->price = tmp_price; switch (newnode->title[0]) { // According to the first letter of the tile...: case 'A': if (ptr_titles[0] == NULL) { ptr_titles[0] = newnode; } else do { if (strcmp(newnode->title, ptr_titles)) > 0) { case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': // and then do the same for prices