We need to complete the function code so in info.c tError si_getSectionInfo(tBoo
ID: 3843080 • Letter: W
Question
We need to complete the function code so in info.c
tError si_getSectionInfo(tBookTable tabB, tSectionTable tabS, char sectionId, tSectionInfo *si )
So when given to the program a table of books, a table of sections and a section identifier the programs gives us back in an output parameter of the type tSectionInfo the composition of the section in subsections and the books contained in each subsection
So this function will give back
OK: if everything went ok, and we get back the requested info
ERR_INVALID_DATA : if the section doesn’t exist in the section table
ERR_ENTRY_NOT_FOUND: if there are no books in this section
I give the code data.h and info.c where the complete function code must go after /************ PR2 - EX3A************/
* 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
/******************** PR2 - EX6B ********************/
tBook *table;
#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;
/******************** PR2 - EX2 *********************/
/* Books of a class */
typedef struct {
/* Table of books of the subsection */
#ifdef SIMPLE_VERSION
char id;
int subBooks[MAX_BOOKS];
unsigned int totSubBooks;
#endif
#ifdef COMPLETE_VERSION
char id;
int *subBooks[MAX_BOOKS];
unsigned int totSubBooks;
/******************** PR2 - EX6A ********************/
#endif
} tSubInfo;
/* Classes of a section */
typedef struct {
tSection section;
tSubInfo secSubs[MAX_SUB];
unsigned int totSecSubs;
unsigned int totSecBooks;
} tSectionInfo;
#endif /*__DATA_H*/
Explanation / Answer
info.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "info.h"
#include "books.h"
#include "sections.h"
/******************** PR2 - EX3A ********************/
tError si_getSectionInfo(tBookTable tabB, tSectionTable tabS, char sectionId, tSectionInfo *si ){
tError retVal = OK;
unsigned int i;
unsigned int j;
unsigned int k;
unsigned int sectionFound = 0;
#ifdef SIMPLE_VERSION
//Initialize subsections
for(i=0;i<MAX_SUB;i++) {
si->secSubs[i].id = 0;
for(j=0;j<MAX_BOOKS;j++){
si->secSubs[i].subBooks[j]=0;
}
si->secSubs[i].totSubBooks = 0;
}
si->totSecBooks =0;
si->totSecSubs =0;
//Loop all sections
for(i=0;i<tabS.size;i++) {
//If section exist in the table of sections
if(tabS.table[i].id==sectionId) {
sectionFound++;
strcpy(&si->section.id,&tabS.table[i].id);
strcpy(si->section.name,tabS.table[i].name);
//Loop all books in the books table
for(j=0;j<tabB.size;j++) {
//If section exist in the books table
if(tabB.table[j].clas.secId==sectionId) {
//Loop all subsections until max. subsections of 10
for(k=0;k<tabB.size;k++) {
//If position in the vector is empty then add subsection
if(si->secSubs[k].id == 0){
strcpy(&si->secSubs[k].id,&tabB.table[j].clas.subId);
si->secSubs[k].subBooks[si->secSubs[k].totSubBooks] = bookTable_find(tabB,tabB.table[j].ISBN);
si->totSecSubs++; //One more subsection in the section
si->secSubs[k].totSubBooks++; //One more book in the subsection
break;
//If the subsection already exist
}else if (si->secSubs[k].id == tabB.table[j].clas.subId){
si->secSubs[k].subBooks[si->secSubs[k].totSubBooks] = bookTable_find(tabB,tabB.table[j].ISBN);
si->secSubs[k].totSubBooks++;//One more book in the subsection
break;
}
if (k == MAX_SUB) break; //If max. subsections is reached end loop
}
si->totSecBooks++; //One more book in the section
}
}
}
}
//No books in this section
if (si->totSecBooks == 0) retVal = ERR_ENTRY_NOT_FOUND;
//Section doesn't exist in sections table
if (sectionFound == 0) retVal = ERR_INVALID_DATA;
#endif
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6A ********************/
//Initialize subsections
for(i=0;i<MAX_SUB;i++) {
si->secSubs[i].id = 0;
si->secSubs[i].subBooks=NULL;
si->secSubs[i].subBooks=(int*)malloc(sizeof(int));
si->secSubs[i].totSubBooks = 0;
}
si->totSecBooks =0;
si->totSecSubs =0;
//Loop all sections
for(i=0;i<tabS.size;i++) {
//If section exist in the table of sections
if(tabS.table[i].id==sectionId) {
sectionFound++;
strcpy(&si->section.id,&tabS.table[i].id);
strcpy(si->section.name,tabS.table[i].name);
//Loop all books in the books table
for(j=0;j<tabB.size;j++) {
//If section exist in the books table
if(tabB.table[j].clas.secId==sectionId) {
//Loop all subsections until max. subsections of 10
for(k=0;k<tabB.size;k++) {
si->secSubs[k].subBooks=(int*)realloc(si->secSubs[k].subBooks, (si->secSubs[k].totSubBooks+1)*sizeof(int));
//If position in the vector is empty then add subsection
if(si->secSubs[k].id == 0){
strcpy(&si->secSubs[k].id,&tabB.table[j].clas.subId);
si->secSubs[k].subBooks[si->secSubs[k].totSubBooks] = bookTable_find(tabB,tabB.table[j].ISBN);
si->totSecSubs++; //One more subsection in the section
si->secSubs[k].totSubBooks++; //One more book in the subsection
break;
//If the subsection already exist
}else if (si->secSubs[k].id == tabB.table[j].clas.subId){
si->secSubs[k].subBooks[si->secSubs[k].totSubBooks] = bookTable_find(tabB,tabB.table[j].ISBN);
si->secSubs[k].totSubBooks++;//One more book in the subsection
break;
}
if (k == MAX_SUB) break; //If max. subsections is reached end loop
}
si->totSecBooks++; //One more book in the section
}
}
}
}
//No books in this section
if (si->totSecBooks == 0) retVal = ERR_ENTRY_NOT_FOUND;
//Section doesn't exist in sections table
if (sectionFound == 0) retVal = ERR_INVALID_DATA;
#endif
return retVal;
}
/******************** PR2 - EX3B ********************/
tBook si_getBook(tBookTable tabB, tSectionInfo si, unsigned int nSub, unsigned int nBook) {
tBook book;
#ifdef SIMPLE_VERSION
book = tabB.table[si.secSubs[nSub].subBooks[nBook]];
#endif
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6A ********************/
#endif
book = tabB.table[si.secSubs[nSub].subBooks[nBook]];
return book;
}
/******************** PR2 - EX3C ********************/
void si_listSectionInfo(tBookTable tabB, tSectionInfo si){
unsigned int i;
unsigned int j;
tBook book;
char str[MAX_LINE];
#ifdef SIMPLE_VERSION
getSectionStr(si.section, MAX_LINE, str);
printf(" Section %s: %d subsections, %d books", str,si.totSecSubs,si.totSecBooks);
for(i=0;i<si.totSecSubs;i++) {
printf(" Subsection %c: %d books:",si.secSubs[i].id,si.secSubs[i].totSubBooks);
for(j=0;j<si.secSubs[i].totSubBooks;j++) {
book = si_getBook(tabB,si,i,j);
getBookStr(book, MAX_LINE, str);
printf(" %s",str);
}
}
printf(" ");
#endif
#ifdef COMPLETE_VERSION
/******************** PR2 - EX6A ********************/
getSectionStr(si.section, MAX_LINE, str);
printf(" Section %s: %d subsections, %d books", str,si.totSecSubs,si.totSecBooks);
for(i=0;i<si.totSecSubs;i++) {
printf(" Subsection %c: %d books:",si.secSubs[i].id,si.secSubs[i].totSubBooks);
for(j=0;j<si.secSubs[i].totSubBooks;j++) {
book = si_getBook(tabB,si,i,j);
getBookStr(book, MAX_LINE, str);
printf(" %s",str);
}
}
#endif
}
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
/******************** PR2 - EX6B ********************/
tBook *table;
#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;
/******************** PR2 - EX2 *********************/
/* Books of a class */
typedef struct {
/* Table of books of the subsection */
#ifdef SIMPLE_VERSION
char id;
int subBooks[MAX_BOOKS];
unsigned int totSubBooks;
#endif
#ifdef COMPLETE_VERSION
char id;
int *subBooks[MAX_BOOKS];
unsigned int totSubBooks;
/******************** PR2 - EX6A ********************/
#endif
} tSubInfo;
/* Classes of a section */
typedef struct {
tSection section;
tSubInfo secSubs[MAX_SUB];
unsigned int totSecSubs;
unsigned int totSecBooks;
} tSectionInfo;
#endif /*__DATA_H*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.