1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK...
-
Upload
pamela-simmons -
Category
Documents
-
view
234 -
download
0
description
Transcript of 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK...
![Page 1: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/1.jpg)
1
Introduction to Computer Programming
Lecture 17Structures (Part 2)
Assist. Prof. Dr. Nükhet ÖZBEKEge University
Department of Electrical & Electronics [email protected]
![Page 2: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/2.jpg)
2
Topics• Structure• Arrays of structs• typedef• structs and functions• Pointers to structs• structs within structs• Data structures and modular design
![Page 3: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/3.jpg)
3
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
Record of student last names and marks
![Page 4: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/4.jpg)
4
Student readRecord ( void ){ Student newStudent; printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark) ); return newStudent;}
Recall: Functions to read and print a student record
void printRecord ( Student item ){ printf("Last name: %s\n", item.lastname); printf(" Mark: %.1f\n\n", item.mark);}
marks4a.c
![Page 5: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/5.jpg)
5
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;} marks4a.c
Recall: Sample main: an array of student records
![Page 6: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/6.jpg)
6
Passing a struct Variable
lastname:
mark:
studentA:int main(){ Student studentA; studentA = readRecord(); return 0;}
![Page 7: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/7.jpg)
7
Student readRecord ( void ){ Student newStudent; printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark)); return newStudent;}
lastname:mark:
newStudent:
int main(){ Student studentA; studentA = readRecord(); return 0;}
Version 1
Passing a struct Variable (cont)
lastname:
mark:
studentA:
![Page 8: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/8.jpg)
8
Student readRecord ( void ){ Student newStudent; printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark)); return newStudent;}
lastname:
mark:
studentA:
lastname:
mark:
newStudent:
Version 1
Passing a struct Variable (cont)
int main(){ Student studentA; studentA = readRecord(); return 0;}
![Page 9: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/9.jpg)
9
Passing a struct Variable (cont)
lastname:
mark:
studentA:int main(){ Student studentA; studentA = readRecord(); return 0;}
![Page 10: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/10.jpg)
10
int main(){ Student studentA; studentA = readRecord(studentA); return 0;}
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
Passing a struct Variable (cont)
![Page 11: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/11.jpg)
11
What if the struct is huge?
Passing a struct Variable (cont)Student readRecord ( Student newStudent ){ printf("Enter last name and mark: "); scanf("%s %f", newStudent.lastname, &(newStudent.mark) ); return newStudent;}
int main(){ Student studentA; studentA = readRecord(StudentA); return 0;}
Version 2
![Page 12: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/12.jpg)
12
Passing a struct Pointer
Pass a pointer to the struct variable
instead!
lastname:
mark:
studentA:
studentPtr:
![Page 13: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/13.jpg)
13
void readRecord ( Student *studentPtr ){ /* De-reference pointer here. */}
int main(){ Student studentA; readRecord( &(studentA) ); return 0;}
Passing a struct Pointer
lastname:
mark:
studentA:
studentPtr:
lastname:
![Page 14: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/14.jpg)
14
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
Passing a struct Pointer studentPtr:
mark:
studentA:
lastname:
![Page 15: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/15.jpg)
15
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
Passing a struct Pointer studentPtr:
mark:
studentA:
lastname:
![Page 16: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/16.jpg)
16
Example: Pointers to Structs-1#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
![Page 17: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/17.jpg)
17
#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
Example: Pointers to Structs-1 (cont)
![Page 18: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/18.jpg)
18
#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
Example: Pointers to Structs-1 (cont)
![Page 19: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/19.jpg)
19
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
Example: Pointers to Structs-2
![Page 20: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/20.jpg)
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)
![Page 21: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/21.jpg)
21
Example: Student Record
lastname:
mark:
struct StudentRec{ char lastname[MAXLEN]; float mark;};
typedef struct StudentRec Student;
![Page 22: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/22.jpg)
22
Information required to maintain a class list• Number of students in the class (<= MAXN)• Subject code
class:
lastname:
mark:
lastname:
mark:
lastname:
mark:
Student class[MAXN];
Example: Class Record (cont)
![Page 23: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/23.jpg)
23
Information required to maintain a class list• Number of students in the class (<= MAXN)• Subject code
char subjCode[MAXLEN]; int count; Student class[MAXN];
count:
class:
lastname:
mark:
lastname:
mark:
lastname:
mark:
subjCode:
Example: Class Record (cont)
![Page 24: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/24.jpg)
24
struct ClassRec{ char subjCode[MAXLEN]; int count; Student class[MAXN];};
typedef struct ClassRec ClassList;
Example: Class Record (cont)
count:
class:
lastname:
mark:
lastname:
mark:
lastname:
mark:
subjCode:“Encapsulates” the data needed to maintain a class list
![Page 25: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/25.jpg)
25
ClassList subject[MAXSUBJ];
Example: Department Databasesubject:
![Page 26: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/26.jpg)
26
char deptName[MAXLEN]; int count; ClassList subject[MAXSUBJ];
Example: Department Database (cont)subject:
deptName:
count:
![Page 27: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/27.jpg)
27
Example: Department Database (cont)
subject:
deptName:
count:
struct DatabaseRec{ char deptName[MAXLEN]; int count; ClassList subject[MAXSUBJ];};
typedef struct DatabaseRec Database;
“Encapsulates” the data needed to maintain a database
![Page 28: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/28.jpg)
28
Access to Struct Members
subject:
deptName:
count:
How do I put the mark 97.5
there?Database finalMarks;
Suppose we declare a struct variable:
![Page 29: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/29.jpg)
29
Access to Struct Members (cont)
finalMarks
subject:
deptName:
count:
finalMarks:
![Page 30: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/30.jpg)
30
Access to Struct Members (cont)
finalMarks.subject[1]
subject:
deptName:
count:
finalMarks:
![Page 31: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/31.jpg)
31
Access to Struct Members (cont)
finalMarks.subject[1].class[0]
subject:
deptName:
count:
finalMarks:
![Page 32: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/32.jpg)
32
Access to Struct Members (cont)
finalMarks.subject[1].class[0].mark = 97.5;
subject:
deptName:
count:
finalMarks:
![Page 33: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/33.jpg)
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
![Page 34: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/34.jpg)
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
![Page 35: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/35.jpg)
35
Example: Data and Modules
Student Record(Student)
Class Record(ClassList)
Department Database
(Database)
readRecord printRecord
readList printListinitList
readDBase printDBaseinitDBase
main
![Page 36: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/36.jpg)
36
Example: Data and Modules
Student Record(Student)
Class Record(ClassList)
Department Database
(Database)
printRecord
Student
readRecordSt
uden
tStudent
main
![Page 37: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/37.jpg)
37
Example: Data and Modules
Student Record(Student)
Class Record(ClassList)
Department Database
(Database)
main
printRecord
Student
printList
Clas sLi st
readList
Cla
ssLi
st
ClassList
initListC
lass
ListC
lassL is treadRecord
Stud
entStudent
![Page 38: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/38.jpg)
38
Example: Data and Modules
Student Record(Student)
Class Record(ClassList)
Department Database
(Database)
main
printDBase
Database
readDBase
Database
Dat
abas
e
initDBaseDatabase
Database
printRecord
Student
printList
Clas sLi st
readList
Cla
ssLi
stClas sLi st
initListClassList
ClassList
readRecordSt
uden
tStudent
![Page 39: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/39.jpg)
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
![Page 40: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/40.jpg)
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
![Page 41: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/41.jpg)
41
struct ClassRec{ char subjCode[MAXLEN]; int count;
};
typedef struct ClassRec ClassList;
void initList ( ClassList *list );void readList ( ClassList *list );void printList ( const ClassList *list );
Example: Modules for Class Record
dbase2b.c
Student student[MAXN];
![Page 42: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/42.jpg)
42
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
Example: Modules for Class Record (cont)
![Page 43: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/43.jpg)
43
struct DatabaseRec{ char deptName[MAXLEN]; int count;
};
typedef struct DatabaseRec Database;
void initDBase ( Database *dbase );void readDBase ( Database *dbase );void printDBase ( const Database *dbase );
Example: Modules for Database
dbase2c.c
ClassList subject[MAXSUBJ};
![Page 44: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/44.jpg)
44
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); }}
Example: Modules for Database (cont)
dbase2c.c
![Page 45: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/45.jpg)
45
Example: Modules for Database (cont)
void printDBase ( const Database *dbase ){ int i; printf("\nDEPARTMENT/FACULTY CODE: %s\n", dbase->deptName); for ( i=0; i < dbase->count; i++ ) { }}
dbase2c.c
printList(&(dbase->subject[i]));
void readDBase ( Database *dbase ){ int i; for ( i=0; i < dbase->count; i++ ) {
}}
initList(&(dbase->subject[i]));readList(&(dbase->subject[i]));
![Page 46: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/46.jpg)
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
dbase2.c
initDBase(&finalMarks);readDBase(&finalMarks);printDBase(&finalMarks);
![Page 47: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/47.jpg)
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
printDBase
Database
readDBase
Database
Dat
abas
einitDBase
Database
Database
printRecord
Student
printList
Clas sList
readList
Cla
ssLi
stCl as sLi st
initListClassList
ClassList
readRecord
Stud
entStudent
![Page 48: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/48.jpg)
48
Example: Change definition of student record (cont)struct StudentRec{ long IDNumber; float mark;};typedef struct StudentRec Student;void readRecord ( Student *studentPtr );void printRecord ( const Student *item );
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
![Page 49: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/49.jpg)
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
![Page 50: 1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nkhet ZBEK Ege University Department of Electrical Electronics.](https://reader035.fdocuments.in/reader035/viewer/2022062317/5a4d1b697f8b9ab0599b25ef/html5/thumbnails/50.jpg)
50
/***********************************************************\ * DATA: * Database * * DESCRIPTION: * A database is a collection of class lists for subjects * conducted by a school or department. * * USAGE: * A database variable can store 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