Thread: #define comparisons and feof.

  1. #16
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Ok then. Assuming the value is double, is in dollars (fractional dollars are cents) then try:
    Code:
    int index;
    ...
    index = (int)(newnode->price / 10.0);
    index -= index * 10.0 == newnode->price;

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Thank you guys, I implemented your ideas and they're working great!
    There's only two problems now.

    1) All titles and prices are read, but
    -when linked according to title, some (eg "BDD" and "BDE") seem to get neglected ("BDE", in this case).
    -when linked according to price, similar thing happens. Maybe there's something missing from the code?

    Code:
    void insert (booksT *titles[26], booksT *prices[10], char tmp_title[], double tmp_price) {
        booksT *newnode, *curr, *prev;
        int index;
        
        newnode = (booksT*)malloc(sizeof(booksT));
        if (newnode == NULL) {
            printf("Memory allocation error.\n");
            exit(1);
        }
        
        strcpy(newnode->title, tmp_title);
        newnode->price = tmp_price;
        
        // Placement according to title.
        index = newnode->title[0] - 'A';
        
        if (titles[index] == NULL) {
            titles[index] = newnode;
        }
        else {
            curr = titles[index];
            prev = NULL;
            
            while (curr != NULL) {
                if (strcmp(curr->title, newnode->title) > 0) {
                    if (prev) {
                        prev->nxt_title = newnode;
                    }
                    newnode->nxt_title = curr;
                    break;
                }
                prev = curr;
                curr = curr->nxt_title;
            }
            if (curr == NULL) {
                prev->nxt_title = newnode;
            }
        }
        
        // Placement according to price.
        index = (int)(newnode->price / 10.0);
        index -= index * 10.0 == newnode->price;
        
        if (prices[index] == NULL) {
            prices[index] = newnode;
        }
        else {
            curr = prices[index];
            prev = NULL;
            
            while (curr != NULL) {
                if (curr->price > newnode->price) {
                    if (prev) {
                        prev->nxt_price = newnode;
                    }
                    newnode->nxt_price = curr;
                    break;
                }
                prev = curr;
                curr = curr->nxt_price;
            }
            if (curr == NULL) {
                prev->nxt_price = newnode;
            }
        }
    }
    or maybe when they're printed...
    Code:
    void save (enum output_specification spec, FILE *file, booksT *sortby[], int size) {
        int i;
        booksT *curr;
        
        if (spec == by_title) {
            for (i = 0; i < size; i++) {
                if (sortby[i] != NULL) {
                    curr = sortby[i];
                    while (1) {
                        fprintf(file, "%lf (%s)\n", curr->price, curr->title);
                        fflush(file);
                        if (curr->nxt_title == NULL) {
                            break;
                        }
                        curr = curr->nxt_title;    
                    }
                }
            }
            fclose(file);
        }
        else {
            for (i = 0; i < size; i++) {
                if (sortby[i] != NULL) {
                    curr = sortby[i];
                    while (1) {
                        fprintf(file, "%s (%lf)\n", curr->title, curr->price);
                        fflush(file);
                        if (curr->nxt_price == NULL) {
                            break;
                        }
                        curr = curr->nxt_price;
                    }
                }
            }
            fclose(file);
        }
    }
    2) Tried to free all allocated memory using recursion, but I'm getting a segmentation fault. This is the part of the code that's relevant:

    Code:
    void memfree (booksT *titles[26], booksT *prices[10], int size_titles, int size_prices) {
        booksT *runner;
        int i;
        
        for (i = 0; i < size_titles; i++) {
            if (titles[i] != NULL) {
                runner = titles[i];
            }
            clearList(runner);
            titles[i] = NULL;
        }
        
        for (i = 0; i < size_prices; i++) {
            if (prices[i] != NULL) {
            prices[i] = NULL; // Nodes have been freed by the previous loop so I only do this.
            } 
        }
    }
    
    
    
    void clearList(booksT *head) {
        
        if (head == NULL) {
            return;
        } 
        else {
            clearList(head->nxt_title);
            free(head);
            head = NULL;
        }
    }
    Any thoughts?

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    EDIT: I think your insert function doesn't correctly reset the head pointer if you insert an element at the front of a non-empty list. You need to set titles[index] to new node in that case.

    Your print function looks fine, if a bit awkward (see below). Can you run this through a debugger and see what happens when you insert "BDE"? Step through the function line by line, and make sure it locates the correct place to insert the node and doesn't do anything strange along the way. Otherwise, post the sample data file you're using.

    As for your free code, it looks fine, though you could just pass titles[i] directly to clearList. The base case will avoid you having to check titles[i] for NULL in memfree, and you wont need your temporary runner variable either.

    Try printing your list like this, it's a little clearer:
    Code:
    for (i = 0; i < size; i++) {
        for (runner = sortby[i]; runner != NULL; runner = runner->next_title) {
            fprintf(file, ...);
            fflush(file);
        }
    }
    Last edited by anduril462; 01-12-2012 at 04:45 PM.

  4. #19
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    I cannot thank you enough! Indeed, my insert function wasn't correctly resetting the head pointer in that case. Insertion and printing of the nodes is now completely functioning.

    clearList seems to be working too!
    Last edited by Xpl0ReRChR; 01-12-2012 at 05:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Efficent string comparisons.
    By ~Kyo~ in forum C++ Programming
    Replies: 17
    Last Post: 05-20-2011, 12:48 AM
  2. if else comparisons problem
    By stuffed in forum C Programming
    Replies: 2
    Last Post: 03-26-2011, 09:30 PM
  3. float comparisons info
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2011, 09:41 AM
  4. Array Comparisons
    By B.C.Lioness in forum C++ Programming
    Replies: 8
    Last Post: 03-20-2004, 07:37 PM
  5. need HELP!!!!!!!!!C++ COMPARISONS
    By awakicee in forum C++ Programming
    Replies: 7
    Last Post: 10-01-2001, 08:08 PM