1 / 50

Introduction to Comput er Programming Lecture 17 Structures (Part 2)

This lecture focuses on different topics related to structures in computer programming, including arrays of structs, typedef, structs and functions, pointers to structs, structs within structs, and data structures and modular design.

stevenadams
Download Presentation

Introduction to Comput er Programming Lecture 17 Structures (Part 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer ProgrammingLecture 17Structures (Part 2) • Assist. Prof. Dr. NükhetÖZBEK • Ege University • Department of Electrical & Electronics Engineering • nukhet.ozbek@ege.edu.tr

  2. Topics • Structure • Arrays of structs • typedef • structs and functions • Pointers to structs • structs within structs • Data structures and modular design

  3. Record of student last names and marks Recall: #include <stdio.h> #include <stdlib.h> #define MAXLEN 50 #define MAXN 20 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; marks4a.c

  4. Functions to read and print a student record Recall: Student readRecord ( void ) { Student newStudent; printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark) ); return newStudent; } void printRecord ( Student item ) { printf("Last name: %s\n", item.lastname); printf(" Mark: %.1f\n\n", item.mark); } marks4a.c

  5. Sample main: an array of student records int main() { int count = 0; Student class[MAXN]; int i; printf("How many students? "); scanf("%d", &count); if (count > MAXN) { printf("Not enough space.\n"); exit(1); } for (i=0; i < count; i++) { class[i] = readRecord(); } printf("\nClass list:\n\n"); for (i=0; i < count; i++) { printRecord(class[i]); } return 0; } Recall: marks4a.c

  6. Passing a struct Variable studentA: int main() { Student studentA; studentA = readRecord(); return 0; } lastname: mark:

  7. Passing a struct Variable (cont) Student readRecord ( void ) { Student newStudent; printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark)); return newStudent; } newStudent: lastname: mark: Version 1 int main() { Student studentA; studentA = readRecord(); return 0; } studentA: lastname: mark:

  8. Passing a struct Variable (cont) Student readRecord ( void ) { Student newStudent; printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark)); return newStudent; } newStudent: lastname: mark: Version 1 int main() { Student studentA; studentA = readRecord(); return 0; } studentA: lastname: mark:

  9. Passing a struct Variable (cont) studentA: int main() { Student studentA; studentA = readRecord(); return 0; } lastname: mark:

  10. Passing a struct Variable (cont) Student readRecord ( Student newStudent ) { printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark) ); return newStudent; } Version 2 A similar thing happens here int main() { Student studentA; studentA = readRecord(studentA); return 0; }

  11. Passing a struct Variable (cont) What if the struct is huge? Student readRecord ( Student newStudent ) { printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark) ); return newStudent; } Version 2 int main() { Student studentA; studentA = readRecord(StudentA); return 0; }

  12. Passing a struct Pointer Pass a pointer to the struct variable instead! studentPtr: studentA: lastname: mark:

  13. studentA: mark: Passing a struct Pointer studentPtr: void readRecord ( Student *studentPtr ) { /* De-reference pointer here. */ } int main() { Student studentA; readRecord(&(studentA)); return 0; } lastname: lastname:

  14. studentPtr: lastname: studentA: mark: Passing a struct Pointer To de-reference a pointer to a struct variable: Style 1:Use the *operator void readRecord ( Student *studentPtr ) { printf("Enter last name and mark: "); scanf("%s %f", (*studentPtr).lastname, &((*studentPtr).mark) ); } marks4b.c

  15. studentPtr: lastname: studentA: mark: Passing a struct Pointer To de-reference a pointer to a struct variable: Style 2:Use the ->operator void readRecord ( Student *studentPtr ) { printf("Enter last name and mark: "); scanf("%s %f", studentPtr->lastname, &(studentPtr->mark)); } marks4b.c

  16. #include <stdio.h> #include <stdlib.h> #define MAXLEN 50 #define MAXN 20 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; void readRecord ( Student *studentPtr ) { printf("Enter last name and mark: "); scanf("%s %f", studentPtr->lastname, &(studentPtr->mark)); } void printRecord ( Student *item ) { printf("Last name: %s\n", (*item).lastname); printf(" Mark: %.1f\n\n", (*item).mark); } Example: Pointers to Structs-1 marks4c.c

  17. Example: Pointers to Structs-1 (cont) #include <stdio.h> #include <stdlib.h> #define MAXLEN 50 #define MAXN 20 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; void readRecord ( Student *studentPtr ) { printf("Enter last name and mark: "); scanf("%s %f", studentPtr->lastname, &(studentPtr->mark) ); } void printRecord ( Student *item ) { printf("Last name: %s\n", (*item).lastname); printf(" Mark: %.1f\n\n", (*item).mark); } marks4c.c

  18. Example: Pointers to Structs-1 (cont) #include <stdio.h> #include <stdlib.h> #define MAXLEN 50 #define MAXN 20 struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; void readRecord ( Student *studentPtr ) { printf("Enter last name and mark: "); scanf("%s %f", studentPtr->lastname, &(studentPtr->mark) ); } void printRecord ( Student *item ) { printf("Last name: %s\n", (*item).lastname); printf(" Mark: %.1f\n\n", (*item).mark); } marks4c.c

  19. Example: Pointers to Structs-2 int main() { int count = 0; Student class[MAXN]; int i; printf("How many students? "); scanf("%d", &count); if (count > MAXN) { printf("Not enough space.\n"); exit(1); } for (i=0; i < count; i++) { readRecord( &(class[i]) ); } printf("\nClass list:\n\n"); for (i=0; i < count; i++) { printRecord( &(class[i]) ); } return 0; } marks4c.c

  20. Structs within Structs • A struct can be a member of another struct • Example: • A student record contains the last name, mark (ID Number, first name, etc) • A class list is a collection of student records (number of students, subject code, etc) • A departmental database is a collection of class lists (department name, number of subjects, etc)

  21. lastname: mark: Example: Student Record struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student;

  22. class: lastname: lastname: lastname: mark: mark: mark: Example: Class Record (cont) • Information required to maintain a class list • Number of students in the class (<= MAXN) • Subject code Student class[MAXN];

  23. subjCode: count: class: lastname: lastname: lastname: mark: mark: mark: Example: Class Record (cont) • Information required to maintain a class list • Number of students in the class (<= MAXN) • Subject code char subjCode[MAXLEN]; int count; Student class[MAXN];

  24. subjCode: count: class: lastname: lastname: lastname: mark: mark: mark: Example: Class Record (cont) “Encapsulates” the data needed to maintain a class list struct ClassRec { char subjCode[MAXLEN]; int count; Student class[MAXN]; }; typedef struct ClassRec ClassList;

  25. Example: Department Database subject: ClassList subject[MAXSUBJ];

  26. Example: Department Database (cont) subject: deptName: count: char deptName[MAXLEN]; int count; ClassList subject[MAXSUBJ];

  27. Example: Department Database (cont) subject: deptName: count: “Encapsulates” the data needed to maintain a database struct DatabaseRec { char deptName[MAXLEN]; int count; ClassList subject[MAXSUBJ]; }; typedef struct DatabaseRec Database;

  28. Access to Struct Members subject: deptName: count: Suppose we declare a struct variable: How do I put the mark 97.5 there? Database finalMarks;

  29. Access to Struct Members (cont) finalMarks: subject: deptName: count: finalMarks

  30. Access to Struct Members (cont) finalMarks: subject: deptName: count: finalMarks.subject[1]

  31. Access to Struct Members (cont) finalMarks: subject: deptName: count: finalMarks.subject[1].class[0]

  32. Access to Struct Members (cont) finalMarks: subject: deptName: count: finalMarks.subject[1].class[0].mark = 97.5;

  33. Data Structures and System Design:Structure Charts • A modular design makes handling of complex data structures easy • A module handles operations on each data structure • The level of the module in the structure chart should (more or less) correspond to the size of the data they handle

  34. Structure Charts • A structure chart is a diagram consisting of rectangular boxes, which represent the modules, and connecting arrows • A structure chart is a graphic tool that shows the hierarchy of program modules and interfaces between them • Structure charts include annotations for data flowing between modules

  35. main initDBase readDBase printDBase initList readList printList readRecord printRecord Example: Data and Modules Department Database (Database) Class Record (ClassList) Student Record (Student)

  36. Example: Data and Modules main Department Database (Database) Class Record (ClassList) Student Student Student Student Record (Student) printRecord readRecord

  37. Example: Data and Modules main Department Database (Database) ClassList ClassList ClassList ClassList ClassList Class Record (ClassList) initList readList printList Student Student Student Student Record (Student) readRecord printRecord

  38. Example: Data and Modules main Database Database Database Department Database (Database) Database Database initDBase readDBase printDBase ClassList ClassList ClassList ClassList ClassList Class Record (ClassList) initList readList printList Student Student Student Student Record (Student) readRecord printRecord

  39. Example: Modules for Student Record struct StudentRec { char lastname[MAXLEN]; float mark; }; typedef struct StudentRec Student; void readRecord ( Student *studentPtr ); void printRecord ( const Student *item ); dbase2a.c

  40. Example: Modules for Student Record (cont) void readRecord ( Student *studentPtr ) { printf("Enter last name and mark: "); scanf("%s %f", studentPtr->lastname, &(studentPtr->mark)); } void printRecord ( const Student *item ) { printf("Last name: %s\n", item->lastname); printf(" Mark: %.1f\n\n", item->mark); } dbase2a.c

  41. Example: Modules for Class Record struct ClassRec { char subjCode[MAXLEN]; int count; }; typedef struct ClassRec ClassList; void initList ( ClassList *list ); void readList ( ClassList *list ); void printList ( const ClassList *list ); Student student[MAXN]; dbase2b.c

  42. Example: Modules for Class Record (cont) void initList ( ClassList *list ) { printf("\nEnter subject code: "); scanf("%s", list->subjCode); printf("How many students? "); scanf("%d", &(list->count) ); if (list->count > MAXN) { printf("Not enough space.\n"); exit(1); } } dbase2b.c

  43. Example: Modules for Database struct DatabaseRec { char deptName[MAXLEN]; int count; }; typedef struct DatabaseRec Database; void initDBase ( Database *dbase ); void readDBase ( Database *dbase ); void printDBase ( const Database *dbase ); ClassList subject[MAXSUBJ}; dbase2c.c

  44. Example: Modules for Database (cont) void initDBase ( Database *dbase ) { printf("\nEnter department or faculty code: "); scanf("%s", dbase->deptName); printf("How many subjects? "); scanf("%d", &(dbase->count) ); if (dbase->count > MAXSUBJ) { printf("Not enough space.\n"); exit(1); } } dbase2c.c

  45. void readDBase ( Database *dbase ) { int i; for ( i=0; i < dbase->count; i++ ) { } } Example: Modules for Database (cont) initList(&(dbase->subject[i])); readList(&(dbase->subject[i])); void printDBase ( const Database *dbase ) { int i; printf("\nDEPARTMENT/FACULTY CODE: %s\n", dbase->deptName); for ( i=0; i < dbase->count; i++ ) { } } printList(&(dbase->subject[i])); dbase2c.c

  46. #include <stdio.h> #include <stdlib.h> #define MAXLEN 50 #define MAXN 100 #define MAXSUBJ 5 #include "dbase2a.c" #include "dbase2b.c" #include "dbase2c.c" int main() { Database finalMarks; return 0; } Example: Test Main Program initDBase(&finalMarks); readDBase(&finalMarks); printDBase(&finalMarks); dbase2.c

  47. Example: Change definition of student record which modules/files do I need to modify in order to use student ID instead of last name? main Database Database Database Database Database initDBase readDBase printDBase ClassList ClassList ClassList ClassList ClassList initList readList printList Student Student Student readRecord printRecord

  48. struct StudentRec { long IDNumber; float mark; }; typedef struct StudentRec Student; void readRecord ( Student *studentPtr ); void printRecord ( const Student *item ); Example: Change definition of student record (cont) void readRecord ( Student *studentPtr ) { printf("Enter ID Number and mark: "); scanf(”%ld %f", &(studentPtr->IDNumber), &(studentPtr->mark)); } void printRecord ( const Student *item ) { printf(”ID Number: %ld\n", item->IDNumber); printf(" Mark: %.1f\n\n", item->mark); } dbase1a.c

  49. Documentation of Structs • Data documentation, like function documentation, is very important • Can determine a team’s success in completing a large software project • Should include information on: • assumptions and limitations • operations and corresponding modules • correct usage

  50. /***********************************************************\/***********************************************************\ * DATA: * Database * * DESCRIPTION: * A database is a collection of class lists for subjects * conducted by a school or department. * * USAGE: * A database variable canstore class lists for up to * MAXSUBJ subjects. The member ‘count’ keeps track of * the number of class lists in the database. * * A data of this type needs to be initialized using the * function initDBase() before use. The member `count' * has to be initialized to a valid value. * * Use the function readDBase() to initialize and read * data into each class list. Use the function printDBase() * to print the content of the entire database. * \***********************************************************/ Example: Data documentation

More Related