HELP > How to remove element in a Structure Array

This is a discussion on HELP > How to remove element in a Structure Array within the C Programming forums, part of the General Programming Boards category; Hi All, I'm doing a project creating a 'Library of Books'. How should I go about 'removing' a book from ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    HELP > How to remove element in a Structure Array

    Hi All,

    I'm doing a project creating a 'Library of Books'.

    How should I go about 'removing' a book from my current library if let's say, a user has 'borrowed' a book?

    Many thanks!!

    Code:
    struct info{
    	char title[99];
    	char author[99];
    	char publisher[99];
    	char subject[5];
    	char index[99];
    	int page;
    	char location[99];
    	char type[99];
    	float price;
    
    } books[N] = {
    	{"Upgrading and Repairing PCs", "Scott K. Muller", "Que Publishing", "COM", "C112.50COM", 1458, "A-C", "For Circulation", 75.50},
    	{"Law : Fundamental Rights and Freedoms", "Charlotte R. Coombs", "IPI Publishing", "LAW", "L145.76LAW", 159, "J-L", "For REFERENCE Only!"},
    	{"Matrix Methods", "Eugene O. Trytyshnikov", "World Scientific", "MATH", "M211.91MATH", 560, "M-O", "For Circulation", 80.50},
    	{"Arts With The Brain In Mind", "Eric L. Jensen", "ASCD", "ARTS", "A871.96ARTS", 140, "A-C", "For Circulation", 25.00},
    	{"A Guide to Physics Problems", "Sidney B. Cahn", "Plenum", "PHY", "S109.87PHY", 351, "S-U", "For Circulation", 63.00},
    	{"Geography : History & Concepts", "Arild H. Jensen", "SAGE", "GEO", "G314.55GEO", 255, "G-I", "For Circulation", 55.50},
    	{"A History of Australia", "Manning S. Clark", "Melbourne University", "HIS", "H539.23HIS", 572, "G-I", "For REFERENCE Only!"},
    	{"Gray's Anatomy", "Henry J. Gray", "Harcourt", "MED", "S280.111MED", 474, "S-U", "For Circulation", 105.40},
    	{"The Japanese Langugage", "Anthony E. Backhouse", "Oxford University", "ARTS", "A705.51ARTS", 196, "A-C", "For REFERENCE Only!"},
    	{"Programming Pearls", "Scott K. Muller", "Que Publishing", "COM", "C257.69COM", 252, "A-C", "For Circulation", 40.00},
    	{"Space & Order", "Steen P. Eiler", "MIT Press", "ARC", "B113.36ARC", 202, "A-C", "For Circulation", 62.00},
    	{"Mental Disorder", "Frank C. Krout", "Broadway House", "MED", "S311.64MED", 149, "S-U", "For Circulation", 33.70},
    	{"Thomas Calculus", "George B.Thomas", "Addison-Wesley", "MATH", "M216.88MATH", 831, "M-O", "For Circulation", 90.90},
    	{"Carnivores", "Jonathan G. Kingdom", "University of Chicago", "ZOO", "S256.17ZOO", 311, "S-U", "For Circulation", 58.00},
    	{"General Chemistry", "Linus J. Pauling", "Pan American", "CHEM", "S211.19CHEM", 115, "S-U", "For Circulation", 57.00}
    };

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Inneart View Post
    Hi All,

    I'm doing a project creating a 'Library of Books'.

    How should I go about 'removing' a book from my current library if let's say, a user has 'borrowed' a book?
    Identify the element to be removed.
    Write a loop to move the next and all following elements forward one position in the array.
    Mark the last position as unused in some manner.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Do you mean 'borrowed' as in they'll never return it, or 'borrowed' as in checked it out? The latter is normal operation for a library, and because the book is technically still owned by the library, removing it from the list is nonsensical. A flag saying that it's checked out or a due back date would both be better options than removal.

    If it's the former, what kind of neighborhood is your library in where that's a use case for the tracking software?
    My best code is written with the delete key.

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Perhaps you don't need to remove it at all. You could have a flag in your structure to indicate that a book is in or out of the library instead of moving things around.

    The books don't seem to be in any sorted order, so if you really want to remove an element, you could just copy the last valid element into the element being removed. You will need to maintain a variable holding the index of the last valid element.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    An array is not the ideal data structure for this operation as you may have already noticed. If you have to use arrays though, I suggest doing what CommonTater suggested, which is define a sentinel value that is invalid within the scope of your program. If you cannot have "holes" within your array (i.e. invalid values) then you have to copy over all the elements following the position you are removing at one position to the left. Note that this is very expensive computationally O(n) where n is the size of your array.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    its trivial like
    Code:
    void removeAt(struct info *b, int at, int end)
    {
      memmove(&b[i],&b[i+1],(end-at-1)*sizeof*b);
      memset(&b[end-1],0,sizeof*b);
    }
    where end is in your case "N" and at is 0 based.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of templated structure
    By cplusplustudent in forum C++ Programming
    Replies: 8
    Last Post: 07-20-2010, 04:07 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. array in structure help
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 08:51 AM
  5. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21