Thread: Question about c program

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    44

    Question about c program

    I am a junior student in programming. I need to write a c program for a library, which shows different book sections.

    It must prompt the user to enter a section number from 1-5. Then the program must show a grid 10 columns by 10 rows, each grid entry showing a book. The program must prompt the user to enter a row, and then a column. It must tell the user if that space is already taken by a book, if it is not, one can store a book there and enter its details.

    The program must also have an option , which shows the user the different sections , and shows the taken spaces by books by an 'O', and the free spaces by an '-'. The details of the books and grid sections must be entered in a file, so that the user can see which spaces are taken in the different sections.

    Can someone help me please ? I will give best answer !

    I am finding it difficult to create the grid and how can I store the details in the different spaces of he grid please ?


    This is the main menu of the program :

    Code:
    int menu()
    {
    int choice = 0;
    
    do
    {
    printf("1. Show Section \n"); //then user has to specify which section, and grid of section is then shown, showing taken(O) and vacant spaces(-).
    printf("2. Enter new book entry\n"); //user has to specify which section , then section is shown, showing taken spaces and vacant spaces. User has to enter section , then a row and column. If space is vacant, user mus enter details of new book. Else if there is already a book , an error must be shown.
    
    printf("3. Quit\n");
    
    printf("\nEnter choice: ");
    scanf("%d", &choice);
    }
    while ((choice 3));
    
    return choice;
    }
    
    This is the structured information that each space must hold :
    Code:
    typedef struct Book
    {
    
    char name[50];
    
    char author[50];
    char ISBN[10];
    
    } Book;
    
    How can I create the grid and relate this structure with every space in each section in the grid? Can someone help me pls ?

  2. #2
    Registered User Jugurtha Hadjar's Avatar
    Join Date
    Mar 2012
    Location
    Algiers, Algeria.
    Posts
    3
    Hello,

    Why wait for the user to enter a row and a column to tell him if that spot is empty or not ? You said that it must have an option of displaying "O" or "-" ..

    I don't think this should be an option, I think it should be the default thing to do: It's an important information you know the user will ask for, so why wait for him to do so ?

    So basically, you want to display a Section[X][Y] and print "O" and "-" in each element of that.. ?

    As a side note, I think you should check again your while loop and think about adding a condition for the section number among other things. (between 1 & 5).
    Last edited by Jugurtha Hadjar; 04-20-2012 at 01:48 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    So.. a library consists of 5 sections, and each section is a grid of 10x10 books?

    Code:
    typedef struct Book
    {
      char name[50];
    
      char author[50];
      char ISBN[10];
    
    } Book;
    So there's a Book. What does each entry in a grid have? A book. What does each section have? 10x10 books. So you could do:

    Code:
    Book library[5][10][10];
    Given a section s, row r and column c you could access e.g. Author like:
    Code:
    library[s][r][c].author;
    I don't really care for this way though. Everything is expressed in varying dimensions of "Book". So library[x] gives a grid which is type Book[10][10], library[x][y] gives a row, which is type Book[10]. Yeugh. Probably the way most people would do it, but I'd personally put everything in its own struct.

    Code:
    typedef struct GridEntry
    {
    	Book book;
    	char taken;
    } GridEntry;
    
    typedef struct Section
    {
    	GridEntry grid[10][10];
    } Section;
    Then your library would be
    Code:
    Section library[5];
    Accessing stuff would look more horrible, but the types are straightforward.

    Code:
    // author access
    library[s].grid[r][c].book.author;
    Either way, to print a section you need to iterate through every Book.
    Code:
    void DisplaySection(Section section)
    {
    	int r,c;
    	for (r = 0; r < 10; r++)
    	{
    		printf("\n"); // each row on a new line
    		for (c = 0; c < 10; c++)
    		{
    			printf("%c ", section.grid[r][c].taken);
    		}
    	}
    }
    Or if you use a multidimensional array of Book and decide author[0] == 0 means space is free:
    Code:
    void DisplaySection(Book section[10][10])
    	int r,c;
    	for (r = 0; r < 10; r++)
    	{
    		printf("\n"); // each row on a new line
    		for (c = 0; c < 10; c++)
    		{
    			printf("%c ", (section.[r][c].book.author[0] == 0 ? '-' : 'O'); 
    		}
    	}
    }
    Some random thoughts:
    If the sections are numbered 1-5, are the rows/columns numbered 1-10? Don't forget C arrays start at 0, so you need to adjust user input and program output. Section 1 is library[0].
    If you expect most spaces to be empty, you might want to consider dynamically allocating space for each Book and storing a pointer in the struct. Save a bit of memory and make it more scalable.
    Your menu display function should either return choice, or have a loop. Not both. I prefer to return the choice and have a loop in main().
    Code:
    do 
    	{
    		choice = menu();
                    switch(choice)
                               ...blahblahblah
            }  while (choice != 3);
    Is there no option to display the information for a book at a particular section, row and column?
    Writing a human readable file is easy -- but if you need to read the same file back in it could a bit of a pain -- probably not difficult but I don't think you'd be able to reuse many of your interactive-input functions.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Just a note about the title of your post. You're posting in a forum dedicated to asking questions about C programs. So "Question about c program" is not particularly informative. "Program for library of books" is better.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    Thanks for your replies , but I am still a bit confused. Is there a way on how to do this without 3-D arrays ?

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    My way only had 2-D arrays

    What do you have against multidimensional arrays? It seems to me to be a natural way to represent this.

    You could do it with an array of 100 Books for each grid instead, but you'd have to map the 0-99 onto row and column numbers and back again, which seems to be more painful to me.

    Code:
    typedef struct Section
    {
        Books grid[100];
    } Section;

    Or you could just store the sections, rows and columns that have things in them. So have
    Code:
    typedef struct Slot
    {
        int section;
        int row;
        int column;
        Book *book;
        Slot *next;
    } Slot;
    
    Slot *library;
    
    int SlotTaken(Slot *lib, int s, int r, int c)
    {
        // go through the linked list looking for elements that match   
    }
    
    void AddBook(Slot *lib, int s, int r, int cm Book *book)
    {
      // go to the end of the linked list
      // malloc a new Slot
      // store arguments
    }
    Problem with doing it that way is you'd have to call SlotTaken() a LOT. Like for every print of 'O' or '-' you'd have to call it. If it's a long list that'd be pretty inefficient. You could make it not so bad by having an array of Slot linked lists, instead of just having 1 list. I don't think this'll be easier than multidimensional arrays!

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    Can you please provide me with the final working code for this program that does the required work ?

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I'd rather not -- this is sounding like a homework or an assignment - we're not here to do your work for you!

    You should try to do it, then post what you've done and point out where you're stuck.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by TexasKid View Post
    Can you please provide me with the final working code for this program that does the required work ?
    No one here is going to do your work for you. Obviously you missed this, at the top of the forum.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    I am also including some functions that I created :
    Code:
    int showSpace()
    {
    int section=0;
    
    do
    {
    printf("Enter Section [1-5]: ");
    scanf("%d", &section);
    
    
    }	
    while ((section < 1 ) && (section > 5));
    
    return section;
    }
    
    void showGrid(int section)
    {
    printf(" Section %d \n",section);
    }
    

    How can I create the grid and relate the book structure with every space in each section in the grid? This is where I am getting really stuck.

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    So I think I am nearly finished , but the program still has some errors. Can you please help me identify these errors and help me writing the main function ? Throughout the program I am also giving some comments to help explain what is happening.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct Book {
      char name[50];
      char author[50];
      char ISBN[10];
    
    } Book;
    
    typedef struct Slot {
      Book theBook;
      char isFree;
    } Slot;
    
    typedef Slot Section[10][10];   // a section is a grid of 10 rows by 10 columns
    typedef Section Library[5];     // a library contains 5 sections
    
    // a book contains name, author, isbn
    // a slot contains a book and a flag to say whether the slot is free or taken
    // a section is a grid of 10 x 10 slots
    // a library contains 5 sections
    Library myLibrary;
    
    int menu()
    {
      int choice = 0;
    
      do {
        printf("1. Show Section \n");
        printf("2. Enter new book entry\n");
        printf("3. Quit\n");
        printf("\nEnter choice: ");
        scanf("%d", &choice);
      }
      while ((choice != 3));
    
      return choice;
    }
    
    
    
    
    int showSpace()
    {
      int section = 0;
    
      do {
        printf("Enter Section [1-5]: ");
        scanf("%d", &section);
      }
      while ((section < 1) || (section > 5));
    
      return section;
    }
    
    void showGrid(int section)
    {
      int i = 0;
      int k = 0;
    
      printf("\nBooks in section %d", section);
    
      for (i = 0; i < 10; i++)      // go through each row
      {
        for (k = 0; k < 10; k++)    // for each row, go through each column
        {
          printf("\n\nSection %d, Row %d, Column %d\n", section, i + 1, k + 1);
    
          if (myLibrary[section - 1][i][k].isFree == '-') {
            printf("No book at this location");
          } else {
            printf("Author: %s\nTitle: %s\nISBN: %s\n",
                      myLibrary[section-1][i][k].theBook.author,
                      myLibrary[section-1][i][k].theBook.name,
                      myLibrary[section-1][i][k].theBook.ISBN);
          }
        }
      }
    }
    
    void DeleteAll()
    {
    // mark the library as being empty by setting all books to being free
      int s = 0;
      int r = 0;
      int c = 0;
    
      for (s = 0; s < 5; s++)       // each section
        for (r = 0; r < 10; r++)    // each row in each section
          for (c = 0; c < 10; c++)  // each column in each row
          {
    // set it to available
            myLibrary[s][r][c].isFree = '-';
          }
    }
    
    int main()
    {
    // delete all books in the library
      DeleteAll();
    
      Book myBook;
      strcpy(myBook.author, "some author");
      strcpy(myBook.name, "some name");
      strcpy(myBook.ISBN, "123456789");
    
    // add a book to the library.
    // remember C arrays start at 0.
    // add a book to section 1, row 2, column 3
      myLibrary[0][1][2].theBook = myBook;
      myLibrary[0][1][2].isFree = 'O';
    
    // show the books in section 1
      showGrid(1);
    }
    Last edited by Salem; 04-21-2012 at 09:52 AM. Reason: tags fixed - since OP can't be bothered

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try using "paste as text" when you paste your code, then perhaps it will look like this (syntax coloured, monospaced, and indented) whereas yours is none of these things.

    Code:
    int main ( ) {
        printf("Hello world\n");
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Dude, layoff the post resurrection shotgun!

    Soma

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    The thing is , I don't know how I can relate the structure for the book [of name, author etc] with each space in the grid. Moreover , the grid spaces are not showing (O and -). Can someone help me please?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I don't know what you want.

    Code:
    $ gcc foo.c
    $ ./a.out 
    
    Books in section 1
    
    Section 1, Row 1, Column 1
    No book at this location
    
    Section 1, Row 1, Column 2
    No book at this location
    
    Section 1, Row 1, Column 3
    No book at this location
    
    Section 1, Row 1, Column 4
    No book at this location
    
    Section 1, Row 1, Column 5
    No book at this location
    
    Section 1, Row 1, Column 6
    No book at this location
    
    Section 1, Row 1, Column 7
    No book at this location
    
    Section 1, Row 1, Column 8
    No book at this location
    
    Section 1, Row 1, Column 9
    No book at this location
    
    Section 1, Row 1, Column 10
    No book at this location
    
    Section 1, Row 2, Column 1
    No book at this location
    
    Section 1, Row 2, Column 2
    No book at this location
    
    Section 1, Row 2, Column 3
    Author: some author
    Title: some name
    ISBN: 123456789
    
    /// A lot more "No book...." prints snipped
    As written, it seems to do what you posted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on C program
    By tekkiram in forum C Programming
    Replies: 6
    Last Post: 04-19-2010, 06:45 AM
  2. Help to program for this question
    By khamarutheen in forum C Programming
    Replies: 1
    Last Post: 01-19-2006, 01:34 AM
  3. Program question!?!?!?!
    By RealityFusion in forum C++ Programming
    Replies: 12
    Last Post: 04-15-2004, 02:02 AM
  4. Got a question about a program...
    By Phantasmagoria in forum Game Programming
    Replies: 1
    Last Post: 12-19-2003, 09:32 PM
  5. Program question---
    By d21998 in forum C++ Programming
    Replies: 13
    Last Post: 05-20-2002, 07:21 PM