Introduction to Computer Programming Lecture 17 Structures (Part 2)

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
1 Introduction to Computing Lecture 15 Arrays (Part 1) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
1 CSE1301 Computer Programming Lecture 22 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 20 Structures (Part 1)
1 CSE1301 Computer Programming Lecture 21 Structures (Part 2)
1 CSE1301 Computer Programming Lecture 24 Structures (Part 2)
1 CSE1301 Computer Programming Lecture 23 Structures (Part 1)
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 CSE1301 Computer Programming: Lecture 25 Software Engineering 2.
1 CSE1301 Computer Programming Lecture 23 Structures (Part 2)
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 CSE1301 Computer Programming Lecture 24: Software Engineering.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
1 Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Structures, Unions, Enumerations
CS1010 Programming Methodology
EKT120 : Computer Programming
Introduction to Computer Programming Lecture 18 Binary Files
CS1010 Programming Methodology
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
EECE.2160 ECE Application Programming
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Computer Programming Lecture 15 Text File I/O
Buy book Online -
CSI 121 Structured Programming Language Lecture 23 Structures (Part 1)
CSI 121 Structured Programming Language Lecture 24 Structures (Part 2)
EKT150 : Computer Programming
EKT120: Computer Programming
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
EECE.2160 ECE Application Programming
Introduction to Computing Lecture 10 Arrays (Part 1)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Introduction to Computer Programming Lecture 16 Structures (Part 1)
Introduction to Computing Lecture 08: Functions (Part I)
EECE.2160 ECE Application Programming
Exam #1 February 23rd (Next Friday)
READING AND PRINTING MATRICES (with functions)
Programming in C Pointer Basics.
CSCE 206 Lab Structured Programming in C
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Abstract Data Types Stacks CSCI 240
EECE.2160 ECE Application Programming
Presentation transcript:

Introduction to Computer Programming Lecture 17 Structures (Part 2) ITCS 102 Computer Programming Introduction to Computer Programming Lecture 17 Structures (Part 2) Assist. Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Dr. B. KARLIK

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

Record of student last names and marks Recall: Record of student last names and marks #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

Functions to read and print a student record Recall: Functions to read and print a student record 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

Sample main: an array of student records Recall: 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"); printRecord(class[i]); return 0; marks4a.c

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

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:

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:

Passing a struct Variable (cont) ITCS 102 Computer Programming Passing a struct Variable (cont) studentA: int main() { Student studentA; studentA = readRecord(); return 0; } lastname: mark: Dr. B. KARLIK

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; }

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; }

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

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

Passing a struct Pointer ITCS 102 Computer Programming Passing a struct Pointer studentPtr: mark: studentA: lastname: 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 Dr. B. KARLIK

Passing a struct Pointer ITCS 102 Computer Programming Passing a struct Pointer studentPtr: mark: studentA: lastname: 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 Dr. B. KARLIK

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

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

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

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"); printRecord( &(class[i]) ); return 0; marks4c.c

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)

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

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

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

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

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

Example: Department Database (cont) ITCS 102 Computer Programming Example: Department Database (cont) subject: deptName: count: char deptName[MAXLEN]; int count; ClassList subject[MAXSUBJ]; Dr. B. KARLIK

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;

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

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

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

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

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

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

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

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

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

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

Example: Data and Modules ITCS 102 Computer Programming 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 Dr. B. KARLIK

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

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

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

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

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

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

Example: Modules for Database (cont) ITCS 102 Computer Programming void readDBase ( Database *dbase ) { int i; for ( i=0; i < dbase->count; i++ ) } 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 Dr. B. KARLIK

Example: Test Main Program ITCS 102 Computer Programming Example: Test Main Program #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; } initDBase(&finalMarks); readDBase(&finalMarks); printDBase(&finalMarks); dbase2.c Dr. B. KARLIK

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

Example: Change definition of student record (cont) ITCS 102 Computer Programming 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 Dr. B. KARLIK

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

Example: Data documentation /***********************************************************\ * 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. \***********************************************************/