Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C. Prog. Implement Actions and Functions I have to implement the function and ac

ID: 3841530 • Letter: C

Question

C. Prog. Implement Actions and Functions

I have to implement the function and action:

tError bookTable_sortedAdd(tBookTable *tabBook, tBook book)

so given a table of books sorted according the criteria from the function book_cmp ( for the fields section, subsection, author and ISBN ) from lowest to highest and a book, if there is enough space in the table to displace all books higher then than the book we want to insert and to insert it in the right position.

void bookTable_sort(tBookTable tabBook, tBookTable *result)

so given a table of books, the program returns a table with the same books but sorted accoring to the criteria from the function book_cmp ( for the fields section, subsection, author and ISBN) lowest to highest. - I think we should use here a "while" algorithm in the original table and in the previous function in order to insert each book in a sorted manner in the table, that we will have previously emptied-

BOOKS.C

#include
#include
#include
#include "books.h"


void getBookStr(tBook book, int maxSize, char *str) {
    int length;
    unsigned short int tmpAvail;
   
    if (book.avail==TRUE)
        tmpAvail=1;
    else
        tmpAvail=0;
       
    length = snprintf(str,maxSize-1,"%s %hu %hu %c %c %s %s", book.ISBN, book.year, tmpAvail, book.clas.secId, book.clas.subId, book.author, book.title);
    if (length>0)
        str[length]='';
}

tError getBookObject(const char *str, tBook *book) {

    tError retVal = OK;
    unsigned short int tmpAvail;
   
    sscanf(str, "%s %hu %hu %c %c %s %s", book->ISBN, &book->year, &tmpAvail, &book->clas.secId, &book->clas.subId, book->author, book->title);
   
    book->avail = (tmpAvail==1);

    return retVal;
}

void bookTable_init(tBookTable *bookTable) {
    bookTable->size=0;
#ifdef COMPLETE_VERSION

#endif
}

int book_cmp(tBook b1, tBook b2) {
   
    int retVal=0;
   
    if (b1.clas.secId>b2.clas.secId) retVal = 1;
    else if (b1.clas.secId     else {
        if (b1.clas.subId>b2.clas.subId) retVal = 1;
        else if (b1.clas.subId         else {
            retVal = strcmp(b1.author, b2.author);
            if(retVal==0) {
                /* If the author is equal, order by title */
                retVal=strcmp(b1.title, b2.title);
                if (retVal==0)
                    retVal=strcmp(b1.ISBN,b2.ISBN);
            }
        }
    }
   
    return retVal;
}

void book_cpy(tBook *dst, tBook src) {
    strcpy(dst->ISBN,src.ISBN);
    dst->year = src.year;
    dst->avail = src.avail;
    dst->clas.secId = src.clas.secId;
    dst->clas.subId = src.clas.subId;
    strcpy(dst->author, src.author);
    strcpy(dst->title, src.title);
}

tError bookTable_add(tBookTable *tabBook, tBook book) {

    tError retVal = OK;

#ifdef SIMPLE_VERSION
    /* Check if there enough space for the new book */
    if(tabBook->size>=MAX_BOOKS) {
        retVal = ERR_MEMORY;
    }
#endif
#ifdef COMPLETE_VERSION

#endif
    if (retVal==OK){
    /* Add the new book to the end of the table */
        book_cpy(&(tabBook->table[tabBook->size]), book);   
        tabBook->size++;
    }

    return retVal;
}

int bookTable_find(tBookTable tabBook, char *ISBN) {
    int i;
    int idx = -1;
   
    i=0;
    while(i< tabBook.size && idx==-1) {
        /* Check if the id is the same */
        if(strcmp(tabBook.table[i].ISBN,ISBN)==0) {
            /* Get the position of the match */
            idx = i;
        }
        i++;
    }
   
    return idx;
}

void bookTable_del(tBookTable *tabBook, tBook book) {
    int i;
    int pos;

    pos = bookTable_find(*tabBook,book.ISBN);
    if (pos!=-1){
    /* If the book is found, all the rest of the elements are displaced one position */
        for(i=pos;isize-1; i++) {       
            book_cpy(&(tabBook->table[i]), tabBook->table[i+1]);           
        }
        tabBook->size=tabBook->size-1;   
#ifdef COMPLETE_VERSION

#endif
    }
}

tError bookTable_save(tBookTable tabBook, const char* filename) {
    FILE *fout=0;
    int i;
    char str[MAX_LINE];
    tError retVal = OK;
   
    /* Open the output file */
    if((fout=fopen(filename, "w"))!=0) {

        /* Save all books to the file */
        for(i=0;i             getBookStr(tabBook.table[i], MAX_LINE, str);
            fprintf(fout, "%s ", str);
        }
       
        /* Close the file */
        fclose(fout);
        retVal = OK;
    } else {
        retVal = ERR_CANNOT_WRITE;
    }
   
    return retVal;
}

tError bookTable_load(tBookTable *tabBook, const char* filename) {

    tError retVal = OK;
    FILE *fin=0;   
    char line[MAX_LINE];
    tBook newBook;
   
    /* Initialize the output table */
    bookTable_init(tabBook);
   
    /* Open the input file */
    if((fin=fopen(filename, "r"))==0) {
        retVal = ERR_CANNOT_READ;
    }
   
    /* Read all the books */
    while(retVal==OK && !feof(fin) && tabBook->size         /* Remove any content from the line */
        line[0] = '';
        /* Read one line and store it in "line" variable */
        fgets(line, MAX_LINE-1, fin);
        /* Ensure that the string is ended by 0*/
        line[MAX_LINE - 1]='';
        if(strlen(line)>0) {
            /* Obtain the object */
            getBookObject(line, &newBook);
            /* Add the new book to the output table */
            bookTable_add(tabBook, newBook);       
        }
    }   
       
    /* Close the file */
    fclose(fin);
   
    return retVal;
}


void bookTable_filterBySection(tBookTable tabBook, char sectionId, tBookTable *result) {
    int i;
   
    bookTable_init(result);
    i=0;
    while(i< tabBook.size) {
        /* Check if the section is the same */
        if(tabBook.table[i].clas.secId==sectionId) {
            /* Add to the result table */
            bookTable_add(result,tabBook.table[i]);
        }
        i++;
    }
}

unsigned int bookTable_getOnLoanNumber(tBookTable tabBook){
    unsigned int i;
    unsigned int numBooks=0;
   
    i=0;
    while(i< tabBook.size) {
        /* Check if the book is not available */
        if(tabBook.table[i].avail!=TRUE) {
            /* Add a unit to the total */
            numBooks++;
        }
        i++;
    }
   
    return numBooks;
}

unsigned int bookTable_getAuthorNumber(tBookTable tabBook, char *author){
    unsigned int i;
    unsigned int numBooks=0;
   
    i=0;
    while(i< tabBook.size) {
        /* Check if the author matches */
        if(strcmp(tabBook.table[i].author,author)==0) {
            /* Add a unit to the total */
            numBooks++;
        }
        i++;
    }
   
    return numBooks;
}


tError bookTable_sortedAdd(tBookTable *tabBook, tBook book){
    tError retVal = OK;

    /* Check if there enough space for the new book */
#ifdef SIMPLE_VERSION

#endif
#ifdef COMPLETE_VERSION

#endif

   
    return retVal;   
}


void bookTable_sort(tBookTable tabBook, tBookTable *result){

}

/* Release a books table */
void bookTable_release(tBookTable *tabBook) {
#ifdef COMPLETE_VERSION

#endif   
}

BOOKS.H

#include "data.h"

/* Get a textual representation of a book */
void getBookStr(tBook book, int maxSize, char *str);
   
/* Get a book object from its textual representation */
tError getBookObject(const char *str, tBook *book);

/* Compare two books by author name*/
int book_cmp(tBook t1, tBook t2);

/* Copy the book data in src to dst*/
void book_cpy(tBook *dst, tBook src);

/* Initialize the table of books */
void bookTable_init(tBookTable *bookTable);

/* Add a new book to the table of books */
tError bookTable_add(tBookTable *table, tBook book);

/* Find a book in the table */
int bookTable_find(tBookTable table, char *ISBN);

/* Remove the first occurence of a book in the table */
void bookTable_del(tBookTable *table, tBook book);

/* Load the table of books from a file */
tError bookTable_load(tBookTable *table, const char* filename);

/* Save a table of books to a file */
tError bookTable_save(tBookTable table, const char* filename);

void bookTable_filterBySection(

tBookTable tabBook, char sectionID, tBookTable *result);

unsigned int bookTable_getOnLoanNumber(tBookTable tabBook);

unsigned int bookTable_getAuthorNumber(tBookTable tabBook, char *author);

tError bookTable_sortedAdd(tBookTable *tabBook, tBook book);

void bookTable_sort(tBookTable tabBook, tBookTable *result);

/* Release a books table */
void bookTable_release(tBookTable *table);

DATA.H

/* Uncomment the practice version you want to run */
#define SIMPLE_VERSION
//#define COMPLETE_VERSION

/* This code ensures that this file is included only once */
#ifndef __DATA_H
#define __DATA_H
/* If the constant DATA_H is not defined (ifndef), the code is added, otherwise, this code is excluded. When the code is added, the constant is defined, therefore next time this file will be included it will be defined and no inclusion will be done. */

#define MAX_PATHNAME 256
#define MAX_LINE 512
#define MAX_SECTIONS 10
#define MAX_SECTION_NAME 100
#define MAX_BOOKS 300
#define MAX_SUB 10
#define MAX_BOOK_ISBN 14
#define MAX_BOOK_AUTHOR_CODE 4
#define MAX_BOOK_TITLE 101

/* Definition of a boolean type */
typedef enum {FALSE, TRUE} tBoolean;

/* Definition of the error type. */
typedef enum {OK=1, ERROR=0, ERR_CANNOT_READ=-1, ERR_CANNOT_WRITE=-2, ERR_MEMORY=-3, ERR_DUPLICATED_ENTRY=-4, ERR_INVALID_DATA=-5, ERR_ENTRY_NOT_FOUND=-6} tError;

/* Definition of a location */
typedef struct {
    char row;
    char column;
    char shelf;
} tLocation;

/* Definition of a section */
typedef struct {
    char id;
    char name[MAX_SECTION_NAME];
    tLocation init;
} tSection;

/* Table of sections */
typedef struct {
    tSection table[MAX_SECTIONS];
    int size;
} tSectionTable;

/* Definition of a classification */
typedef struct {
    char secId;
    char subId;
} tClass;

/* Definition of the book */
typedef struct {
    char ISBN[MAX_BOOK_ISBN];
    unsigned short year;
    tBoolean avail;
    tClass clas;
    char author[MAX_BOOK_AUTHOR_CODE];
    char title[MAX_BOOK_TITLE];
} tBook;

/* Table of books */
typedef struct {
#ifdef SIMPLE_VERSION
    tBook table[MAX_BOOKS];
#endif
#ifdef COMPLETE_VERSION

#endif   
    int size;
} tBookTable;


/* Definition of the application data structure */
typedef struct {
    /* Path where data will be stored */
    char path[MAX_PATHNAME];
   
    /* sections table */
    tSectionTable sections;
   
    /* Books table */
    tBookTable books;
   
} tAppData;


/* Books of a class */
typedef struct {
    char id;
    /* Table of books of the subsection */
#ifdef SIMPLE_VERSION   
#endif   
#ifdef COMPLETE_VERSION   

#endif
} tSubInfo;

/* Classes of a section */
typedef struct {
    unsigned int totSecSubs;
} tSectionInfo;

#endif /*__DATA_H*/

API.H

#include "data.h"

/*
* Methods for application data management
*/

/* Initialize the application data */
void appData_init(tAppData *object);

/* Load the application data from file */
tError appData_load(tAppData *object);

/* Save the application data to a file */
tError appData_save(tAppData object);

/* Allow to assign a path to the application data */
void appData_setPath(tAppData *object, const char *path);

/*
* API
*/

/* Return a table with the books */
tError getBooks(tAppData object, tBookTable *result);

/* Get the section information */
tError getBook(tAppData object, char *ISBN, tBook *book);

/* Add a new book */
tError addBook(tAppData *object, tBook book);

/* Remove a certain book */
tError removeBook(tAppData *object, tBook book);

/* Return the table of sections */
tError getSections(tAppData object, tSectionTable *result);

/* Get the section information */
tError getSection(tAppData object, char C1, tSection *section);

/* Add a new section */
tError addSection(tAppData *object, tSection section);

INFO.H

#include "data.h"

tError si_getSectionInfo(tBookTable tabB, tSectionTable tabS, char sectionId, tSectionInfo *si );

tBook si_getBook(tBookTable tabB, tSectionInfo si, unsigned int nSub, unsigned int nBook);

void si_listSectionInfo(tBookTable tabB, tSectionInfo si );

MENU.H

include "data.h"
#include "api.h"

/* Request an option to the user and check its validity */
int getOption(int numOptions);

/* Define the main menu options type */
typedef enum {MAIN_MENU_LOAD, MAIN_MENU_SAVE, MAIN_MENU_BOOKS, MAIN_MENU_SECTIONS, MAIN_MENU_STATS, MAIN_MENU_EXIT} tMainMenuOptions;

/* Define the book management menu options type */
typedef enum {BOOK_MENU_LIST, BOOK_MENU_ADD, BOOK_MENU_DEL, BOOK_MENU_SORT, BOOK_MENU_EXIT} tBookMenuOptions;

/* Define the section management menu options type */
typedef enum {SEC_MENU_LIST, SEC_MENU_ADD, SEC_MENU_INFO, SEC_MENU_EXIT} tSectionMenuOptions;

/* Define the status menu options type */
typedef enum {STAT_MENU_ON_LOAN, STAT_MENU_AUTHOR, STAT_MENU_SECTION, STAT_MENU_EXIT} tStatsMenuOptions;

/* Print the main menu options */
void printMainMenuOptions();

/* Get the option for the main menu */
tMainMenuOptions getMenuOption();

/* Perform the actions for the main menu */
void mainMenu(tAppData *appData);

/* Print the book management menu options */
void printBookMenuOptions();

/* Get the option for the book management menu */
tBookMenuOptions getBooksMenuOption();

/* Perform the actions for the book management menu */
void bookMenu(tAppData *appData);

/* Print the section management menu options */
void printSectionMenuOptions();

/* Get the option for the section management menu */
tSectionMenuOptions getSectionMenuOption();

/* Perform the actions for the section management menu */
void secMenu(tAppData *appData);

/* Print the stats menu options */
void printStatsMenuOptions();

/* Get the option for the status menu */
tStatsMenuOptions getStatsMenuOption();

/* Perform the actions for the status menu */
void statsMenu(tAppData appData);

SECTIONS.H

#include "data.h"

/* Get a textual representation of a section */
void getSectionStr(tSection section, int maxSize, char *str);

/* Get a section object from its textual representation */
tError getSectionObject(const char *str, tSection *section);

/* Compare two sections */
int section_cmp(tSection s1, tSection s2);

/* Copy the section data in src to dst*/
void section_cpy(tSection *dst, tSection src);

/* Initialize the table of sections */
void secTable_init(tSectionTable *tabSec);

/* Add a new section to the table of sections */
tError secTable_add(tSectionTable *tabSec, tSection section);

/* Find a section in the table */
int secTable_find(tSectionTable tabSec, char sectionId);

/* Remove the first occurence of a section in the table */
void secTable_del(tSectionTable *tabSec, tSection section);

/* Load the table of sections from a file */
tError secTable_load(tSectionTable *tabSec, const char* filename);

/* Save a table of sections to a file */
tError secTable_save(tSectionTable tabSec, const char* filename);

Explanation / Answer

At the point when the compiler forms a call to a capacity, it will verify that the right number and sorts of information things are being passed to the capacity, and will consequently produce sort changes as necessary. This is known as sort checking.
Sort checking is just conceivable if the compiler definitely thinks about the capacity, including what sort of information the capacity is hoping to receive. Otherwise, the compiler needs to make suspicions, which can prompt off base and whimsical conduct if those presumptions are not right.
One method for managing this circumstance is to verify that all capacities seem prior in a document than any calls to them. This works for straightforward cases, yet can make vast complex projects hard to take after, and does not work in the instances of ( mutually ) recursive capacities and capacities situated in particular records or libraries.
A superior approach is to utilize work prototypes. This is a method for announcing to the compiler what information a capacity will require, without really giving the capacity itself.
Illustrations:
int include( int an, int b );
int include( int, int );
Take note of that the capacity models end with semicolons, showing this is not a capacity, but rather simply a model of a capacity to be given somewhere else.
Note likewise that the variable names are not required in the capacity prototype. They might be incorporated for lucidity on the off chance that you wish, however the compiler will overlook them. This additionally implies the variable names utilized as a part of the capacity model don't have to coordinate those utilized as a part of the real capacity itself.
For clearness it is by and large great style to rundown all capacities that will be utilized by models toward the start of the document. At that point give principle( ) as the primary full capacity definition, trailed by each of alternate capacities in the request in which the models are recorded. ( I.e. the model rundown goes about as a sort of list of chapters for the genuine capacity which show up after primary. )
Work models are regularly set in isolated header documents, which are then incorporated into the schedules which require them. For instance, "math.h" incorporates the capacity models for the C math capacities sqrt( ) and cos( ).
Exercise: Write work models for:
A capacity which takes an int and a buoy, and returns a twofold.
Reply: twofold myfunction( int, skim );
A capacity which takes no contentions and returns no esteem.
Reply: void yourfunction( void );
Operation
Calling a Function
A capacity is called by utilizing the capacity name, trailed by an arrangement of enclosures containing the information to be passed to the capacity.
The information gone to the capacity are alluded to as the "genuine" parameters. The factors inside the capacity which get the passed information are alluded to as the "formal" parameters.
The formal parameters are neighborhood factors, which exist amid the execution of the capacity just, and are just known by the called function. They are introduced when the capacity begins by duplicates of the information gone as real parameters. This instrument, known as "go by esteem", guarantees that the called capacity can not straightforwardly change the estimations of the calling capacities variables. ( Exceptions to this will be talked about later. )
To call a capacity which takes no contentions, utilize a vacant match of brackets.
Example:         add up to = include( 5, 3 );
Exceptionally IMPORTANT: It is essential that the number and sorts of genuine parameters passed coordinate with the number and sorts of parameters expected by the capacities formal parameter list. ( If the quantity of contentions matches yet the information sorts don't, then the compiler MAY embed some sort transformation code if the right information sorts are known, however it is more secure not to depend on this. )
NOTE CAREFULLY: The genuine parameters gone by the calling program and the formal parameters used to get the qualities in the called capacity will frequently have a similar variable names. Be that as it may it is critical to perceive that they are very surprising free factors ( on the grounds that they exist in various degrees, see underneath ), whether they happen to have a similar name or not.
Case: In the code beneath, the factors x, y, z, and x again in principle are utilized to introduce the factors x, z, b, and c in the capacity. The way that x and z show up in both principle and the capacity is insignificant - They are autonomous factors with free values.
void func( twofold x, twofold z, twofold b, twofold c );
int fundamental( void ) {
twofold x( 1.0 ), y( 2.0 ), z( 3.0 );
func( x, y, z, x );
The call to the capacity above instates the capacity parameters identically to the accompanying task explanations:
x in func = x in fundamental
z in func = y in fundamental
b in func = z in fundamental
c in func = x in fundamental

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote