chapter 3: records...

88
CHAPTER 3: RECORDS (STRUCT) [PART 1] CSC 138 – Structured Programming

Upload: others

Post on 21-Jun-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

CHAPTER 3: RECORDS (STRUCT) [PART 1]

CSC 138 – Structured Programming

Page 2: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

LEARNING OBJECTIVES

Upon completion, you should be able to:

o define records (structures) that contain array data type as one of the member.

o declare and initialize C++ record (struct) as an array.

o differentiate between array and record (struct).

o write program using array and function in record (struct).

Page 3: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD OVERVIEW

3.1 RECORDS DEFINITION

3.2 RECORDS DECLARATION

3.3 RECORDS INITIALIZATION

3.4 ACCESSING RECORDS MEMBERS

3.5 RECORDS ASSIGNMENT

3.6 AGGREGATE OPERATIONS WITH STRUCT

Page 4: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS / STRUCT

CHAPTER 3

• DEFINITION • DECLARATION • INITIALIZATION • ACCESSING RECORD MEMBERS • RECORD ASSIGNMENT • AGGREGATE OPERATIONS WITH STRUCT

Page 5: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD INTRODUCTION o Assume that we want to store the following students’

information: name

Ic number

Matric number

Year entry

o Below are the example of data representation that might be used: char Name[15], ICno[15], MatricNo[7];

int YearEntry;

o We cannot use an array to group all of the items associated with a student. How to group elements of different types together?

Page 6: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD INTRODUCTION o An array is a homogeneous data structure; a struct

is typically a heterogeneous data structure.

o A collection of fixed number of components in which components are accessed by name. The components may be of different types.

o Allowed a group of related data being accessed and manipulate as a unit(record) struct

o For example, data associated with student are: Name - string 15 characters

IcNo - string 15 characters/array

MatricNo - string 7 characters

YearEntry - integer

Page 7: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD DEFINITION

o struct: collection of a fixed number of components (members), accessed by name • Members may be of different types

o Syntax:

Page 8: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD DECLARATION

o A struct is a definition, not a declaration

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

8

struct studentType

{

string firstName;

string lastName;

char courseGrade;

int testScore;

int programmingScore;

double GPA;

//variable declaration

} newStudent, student;

OR

Page 9: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD DECLARATION

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

9

Page 10: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Company Logo

RECORD INITIALIZATION o Initialization can be done during declaration.

o Example 1:

struct student stud1 = {“Nadiah”, “880409-13-5555”, “A11000”, 1998};

o Example 2:

struct date {

int day;

int month;

int year;

}

struct date Birth_Day = {1, 1, 1982}; Birth_Day

day

month

year

1

1

1982

CSC 138 Structured programming

Page 11: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Company Logo

o Example 3:

struct date Birth_Day = {31, 10, 1966};

31 day

month

year

Birth_Day

10

1966

CSC 138 Structured programming

RECORD INITIALIZATION

Page 12: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ACCESSING RECORDS MEMBERS

o The syntax for accessing a struct member

is:

o The dot (.) is an operator, called the member access operator

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

12

Page 13: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

o To initialize the members of newStudent: newStudent.GPA = 0.0;

newStudent.firstName = "John";

newStudent.lastName = "Brown";

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

13

ACCESSING RECORDS MEMBERS

Page 14: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

More examples: cin>>newStudent.firstName;

cin>>newStudent.testScore>>newStudent.programmingScore;

Score=(newStudent.testScore + newStudent.programmingScore) / 2;

if (score >= 90)

newStudent.courseGrade = 'A';

else if (score >= 80)

newStudent.courseGrade = 'B';

else if (score >= 70)

newStudent.courseGrade = 'C';

else if (score >= 60)

newStudent.courseGrade = 'D';

else

newStudent.courseGrade = 'F';

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

14

ACCESSING RECORDS MEMBERS

Page 15: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

o Example 3: struct student S1;

strcpy(S1.name, "C Bin D");

strcpy(S1.IcNo, "661122-02-5554");

strcpy(S1.MatricNo, "A11122");

S1.YearEntry = 1990;

'C' ' ' 'B' 'i' 'n' ' ' 'D' '\0'

'6' '6' '1' '1' '2' '2' '-' '0' '2' '-' '5' '5' '5' '4' '\0'

Name

IcNo

YearEntry

MatricNo S1

1990

'A' '1' '1' '1' '2' '2' '\0'

CSC 138 Structured programming

ACCESSING RECORDS MEMBERS

Page 16: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Company Logo

o Example 4: struct date Birth_Day;

cout<<“When is your birthday?(DD MM YYYY)\n";

cin>>Birth_Day.day>>Birth_Day.month>>

Birth_Day.year;

cout<<“Your birthday is ”<< Birth_Day.day<<

“/”<<Birth_Day.month<<“/”<<

Birth_Day.year<<endl;

??? day

month

year

Birth_Day

???

???

CSC 138 Structured programming

ACCESSING RECORDS MEMBERS

Page 17: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Company Logo

o Example struct stud S1;

cout<<"Name, Ic no, Matric no, year?\n");

gets(S1.Name); //also act as input data for char [cin]

gets(S1.IcNo); //needs to include stdio.h in using gets()& puts()

gets(S1.MatricNo);

cin>>S1.YearEntry;

puts(S1.Name); // also act as display data for char [cout]

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ?

???

Name

IcNo

YearEntry

MatricNo S1

CSC 138 Structured programming

ACCESSING RECORDS MEMBERS

Page 18: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

o Value of one struct variable can be assigned to another struct variable of the same type using an assignment statement

o The statement: student = newStudent;

copies the contents of newStudent into student

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

18

RECORD ASSIGNMENT

Page 19: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD ASSIGNMENT

o The assignment statement: student = newStudent;

is equivalent to the following statements: student.firstName = newStudent.firstName;

student.lastName = newStudent.lastName;

student.courseGrade = newStudent.courseGrade;

student.testScore = newStudent.testScore;

student.programmingScore = newStudent.programmingScore;

student.GPA = newStudent.GPA;

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

19

Page 20: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

20

AGGREGATE OPERATIONS WITH STRUCTURES

o Recall that arrays had none (except reference parameter) o Structures DO have aggregate operators

• assignment statement =

• parameter (value or reference)

• return a structure as a function type

Page 21: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

21

AGGREGATE OPERATIONS WITH STRUCTURES

o Limitations on aggregate operations • no I/O

• no arithmetic operations

• no comparisons

cout << old_part;

cin >> new_part;

old_part = new_part + old_part;

if (old_part < new_part)

cout << ...;

Page 22: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

COMPARISON (RELATIONAL OPERATORS)

o Compare struct variables member-wise • No aggregate relational operations allowed

o To compare the values of student and newStudent:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

22

Page 23: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

INPUT/OUTPUT

o No aggregate input/output operations on a struct variable

o Data in a struct variable must be read one member at a time

o The contents of a struct variable must be written one member at a time

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

23

Page 24: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAYS VERSUS STRUCTS

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

24

Page 25: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Exercise 1 1. Define a struct, checkingAccount, to store the following data about a

checking account; account holder’s name(string), account number (int), balance (double), and interest rate (double).

2. Define a struct, movieType, to store the following data about a movie: movie name(string), movie director(string), producer (string), the year movie was released (int),and number of copies in stock.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

25

Page 26: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Exercise 2 – Assume the definition of Exercise 1

1. Declare a checkingAccount variable and write C++ statements to store the following information: account holder’s name – Jason Miller, account number – 17328910, balance – 24476.38, interest rate -2.5%.

2. Declare a variable of type movieType to store the following data: movie name – Summer Vacation, director – Tom Blair, producer – Rajiv Merchant, year the movie released – 2005, the number of copies in stock – 34

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

26

Page 27: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Exercise 3

a) Write a C++ statement for each of the following questions:

i. Define a struct named Student_Information which has four members of different types. The members are student name, gender(‘F’ or ‘M’), semester and CGPA.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

27

Page 28: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Exercise 4 a) Consider the following statements:

Struct nameType

{

string first;

string last;

double score;

};

nameType name;

nameType name2;

Mark the following statements as valid or invalid. If statement is invalid explain why.

i. name.first = “Nafisah”;

ii. cout<< nameType;

iii. name2= name;

iv. cin >> nameType;

v. cout<< name2. last;

vi. nameType.last = “Amin”;

vii. double total = score;

viii. double average; average= name2.score /2;

C++ Programming: From Problem Analysis to Program Design, Fourth Edition

28

Page 29: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Mark the following statements as valid or invalid based on the struct

definition below:

struct bookType

{

char authorName[30];

float price;

char edition[15];

int yearPublished;

char publisher[50];

}book1, book2;

Exercise 5

a)book1.price=50.00;

b)book1.publisher[30];

c)book2==book1;

d)if (strcmp(book1.edition,book2.edition)==0)

e)strcpy(book2.publisher, ‘Pearson’);

Page 30: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

a) Define a struct named Course with 3 members: courseName, courseCode and courseFee.

Declare 2 struct variables namely course1 and course2.

b) A company has six salespersons. Every month they go on road trips to sell the company’s product. At the end of each month, the total sales for each salesperson, together with that salesperson’s ID and month is recorded. Write an appropriate struct definition for this problem.

Exercise 6

Page 31: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

Given the following struct definition:

struct bookType

{

char authorName[30];

float price;

char edition[15];

int yearPublished;

char publisher[50];

}book1, book2;

Exercise 7

write a C++ statement for each of the following:

a) Set the value of yearPublished for book2 to 2009.

b) Set the price of book2 equal to the price of book1.

c) Set the value of authorName for book2 as D.S. Malik.

Page 32: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

CHAPTER 3: RECORDS (STRUCT) [PART 2]

CSC 138 – Structured Programming

Page 33: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD OVERVIEW

3.7 RECORDS AND ARRAY

3.8 RECORDS AND FUNCTION

Page 34: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS AND ARRAY

CHAPTER 3

• ARRAY IN RECORD

• RECORDS IN ARRAY

Page 35: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 1: const int arraySize = 500;

struct listType

{

//array in struct

int elements[arraySize];

int listLength;

};

listType listOne; //declared a struct variable

elements

listLength

Memory: listOne

elements[0]

elements[1]

. . .

elements[499]

Page 36: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 1:

listOne.listlength = 0;

listOne.elements[0] = 11;

listOne.listlength++;

listOne.elements[1] = 22;

listOne.listlength++;

elements

listLength

Memory: listOne

elements[0]

elements[1]

. . .

elements[499]

0

Page 37: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 1:

listOne.listlength = 0;

listOne.elements[0] = 11;

listOne.listlength++;

listOne.elements[1] = 22;

listOne.listlength++;

elements

listLength

Memory: listOne

elements[0]

elements[1]

. . .

elements[499]

11

0

Page 38: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 1:

listOne.listlength = 0;

listOne.elements[0] = 11;

listOne.listlength++;

listOne.elements[1] = 22;

listOne.listlength++;

elements

listLength

Memory: listOne

elements[0]

elements[1]

. . .

elements[499]

11

1 0

Page 39: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 1:

listOne.listlength = 0;

listOne.elements[0] = 11;

listOne.listlength++;

listOne.elements[1] = 22;

listOne.listlength++;

elements

listLength

Memory: listOne

elements[0]

elements[1]

. . .

elements[499]

11

22

1

Page 40: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 1:

listOne.listlength = 0;

listOne.elements[0] = 11;

listOne.listlength++;

listOne.elements[1] = 22;

listOne.listlength++;

elements

listLength

Memory: listOne

elements[0]

elements[1]

. . .

elements[499]

11

2 1

22

Page 41: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 1 (Complete Program): const int arraySize = 500;

struct listType {

int elements[arraySize]; //array in struct

int listLength;

};

int main(){

listType listOne; //Declares a struct variable

cout << "Enter number of element(s): ";

cin >> listOne.listLength;

for(int i = 0; i < listOne.listLength; i++)

listOne.elements[i] = (i+1);

for(int i = 0; i < listOne.listLength; i++)

cout << listOne.elements[i] << endl;

}

Page 42: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 2 (struct definition & variable declaration):

struct studentType

{

string studName;

char courseGrade;

int testScore[2];

int asgnScore;

double GPA;

};

studentType student;

studName

courseGrade

testScore [1]

asgnScore

GPA

Memory: student

testScore [0]

Page 43: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 2 (variable declaration & initialization):

studentType student =

{

“Jamaluddin”,

‘A’,

90,

87,

85,

3.85

};

studName

courseGrade

testScore [1]

asgnScore

GPA

Jamaluddin

A

87

85

3.85

Memory: student

testScore [0] 90

Page 44: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 2 (main function): int main() {

studentType student = { “Jamaluddin”,‘A’, 90, 87,

85, 3.85 };

//Assignment statement: Method 1

studentType newStudent;

//Assignment statement: Method 2

for(int i = 0; i < 2; i++)

newStudent.studName = student.studName;

newStudent.courseGrade = student.courseGrade;

newStudent.testScore[i] = student.testScore[i];

newStudent.asgnScore = student.asgnScore;

newStudent.GPA = student.GPA;

Page 45: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 2 (main function cont.): ...

cout << " STUDENT'S INFORMATION" << endl;

cout << " NAME\t: " << student.studName[0]

<< " " << student.studName[1];

cout << "\n GPA\t: " << student.GPA;

cout << "\n COURSE GRADE : "

<< student.courseGrade;

cout << "\n\n ASSIGNMENT SCORE\t: "

<< student.asgnScore;

cout << "\n TEST SCORE\t\t: [1] "

<< student.testScore[0] << " [2] "

<< student.testScore[1];

}

Page 46: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ARRAY IN RECORD (STRUCT)

o Example 2 (Output):

Page 47: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXERCISE (3.2.1)

Extends the codes from previous program in Example 1 to:

1. display the elements of the array in reverse order.

2. display the odd elements of the array.

Page 48: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ANSWER (3.2.1) const int arraySize = 500;

struct listType

{

int elements[arraySize]; //array in struct

int listLength;

};

main(){

:

//Display elements in reverse order

//Display odd elements

}

for(int i = (listOne.listLength - 1); i >= 0; i--)

cout << listOne.listElement[i] << endl;

for(int i = 0; i < listOne.listLength; i++)

if(listOne.listElement[i] % 2 != 0)

cout << listOne.listElement[i] << endl;

Page 49: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS (STRUCT) IN ARRAY

o Suppose a company has 50 employees. The employer need to print the employees’ monthly paychecks and keep track of how much money has been paid to each employee.

o Step 1 : Define struct

const int noOfEmployee = 50;

struct employeeType

{

char name[50],

deptID[15],

employeeID[15];

int hourWorked;

double basicSalary,

monthlySalary;

};

name

deptID

employeeID

hourWorked

basicSalary

monthlySalary

Page 50: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS (STRUCT) IN ARRAY

o Step 2 : Declare an array of struct variable

employeeType employees[noOfEmployee];

• Declares an array employees with 50 components of type employeeType. Every elements is a struct.

name

deptID

employeeID

hourWorked

basicSalary

Memory: employees[0]

monthlySalary

name

deptID

employeeID

hourWorked

basicSalary

Memory: employees[1]

monthlySalary

name

deptID

employeeID

hourWorked

basicSalary

Memory: employees[49]

monthlySalary

Page 51: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS (STRUCT) IN ARRAY

o Step 3 : Input / Read data for each of the employees for(int x = 0; x < noOfEmployee; x++)

{

//Input statement

//Calculate the monthly salary

}

Output screen:

Page 52: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS (STRUCT) IN ARRAY

//Input statement

cout << " Employee " << x+1 << endl;

cout << "----------\n";

cout << " Name : ";

cin.getline(employees[x].name, 50);

cout << " Department : ";

cin.getline(employees[x].deptID,15);

cout << " Employee ID : ";

cin.getline(employees[x].employeeID,15);

cout << " Hours Worked : ";

cin >> employees[x].hourWorked;

cout << " Basic Salary : RM ";

cin >> employees[x].basicSalary;

//Calculate the monthly salary

employees[x].monthlySalary =

employees[x].hourWorked * employees[x].basicSalary;

cout << endl;

Page 53: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS (STRUCT) IN ARRAY

o Step 4 : Access elements of the array, elements of the struct

//print all the name for each employees

for(int x = 0; x < noOfEmployee; x++)

cout << employees[x].name;

//print all the employeeID

for(int x = 0; x < noOfEmployee; x++)

cout << employees[x].employeeID;

Page 54: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS (STRUCT) IN ARRAY

//Display the record for each employee

cout << setw(10) << "EMPLOYEE ID"

<< setw(20) << "BASIC SALARY (RM)"

<< setw(25) << "MONTHLY SALARY (RM)"

<< endl;

cout << setfill('-') << setw(55) << endl;

cout << setfill(' ');

for(int i = 0; i < noOfEmployee; i++)

{

cout << setw(8) << employees[i].employeeID;

cout << setw(16) << employees[i].basicSalary;

cout << setw(16) << employees[i].monthlySalary;

cout << endl;

}

system("PAUSE");

}

Page 55: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXERCISE (3.2.2)

Extends the codes from previous program to:

1. search employee record by their employee ID.

2. display the particular details as the following output screen:

Page 56: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ANSWER (3.2.2) main() {

employeeType employees[noOfEmployee];

char searchID[15];

cout << "\n Enter Employee ID to be searched: ";

cin >> searchID;

for(int i = 0; i < noOfEmployee; i++) {

if(strcmp(employees[i].employeeID, searchID) == 0) {

//Display the record for the employee

cout << endl << setw(10) << " EMPLOYEE ID"

<< setw(15) << "EMPLOYEE NAME"

<< setw(25) << "DEPARTMENT"

<< endl;

cout << "------------------------------" << endl;

cout << setfill(' ');

cout << setw(10) << employees[i].employeeID;

cout << setw(15) << employees[i].name;

cout << setw(25) << employees[i].deptID;

cout << endl;

}

else

cout << " Record doesn't exist!";}

}

Page 57: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXERCISE (3.2.3)

Suppose FSKM has 5 students. We need to calculate the total score obtained by each student. The total score is by adding the mark of test score and programming score. From the total score, find the course grade of each student. Lastly, display all information of all students.

struct studentType {

string firstName;

string lastName;

char courseGrade;

int testScore;

int programmingScore;

double GPA;

int score;

} ;

studentType student[5];

Page 58: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXERCISE (3.2.4)

Suppose a company has 10 full-time employed. We need to print their monthly paychecks and keep track of how much money has been paid to each employee in the year-to-date. First, let’s define an employee’s record:

Input : firstName, lastName, personId, deptId, yearlySalary, monthlyBonus

struct employeeType {

string firstName, lastName;

string deptId;

int personId;

double yearlySalary;

double monthlySalary;

double yearToDatePaid;

double monthlyBonus;

} ;

employeeType employee[10];

Page 59: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ANSWER (3.2.3)

int main()

{

for(int i=0; i<5; i++)

{

cout<<"insert first name : ";

cin>>student[i].firstName;

cout<<"insert last name : ";

cin>>student[i].lastName;

cout<<"insert test score : ";

cin>>student[i].testScore;

cout<<"insert programming score : ";

cin>>student[i].programmingScore;

cout<<"insert GPA : ";

cin>>student[i].GPA;

}

}

for(int i=0; i<5; i++) {

student[i].score =

student[i].testScore+student[i].programmingScore;

if(student[i].score >=90)

student[i].courseGrade='A';

else if(student[i].score >=80 && student[i].score<90)

student[i].courseGrade='B';

else if(student[i].score >=70 && student[i].score<80)

student[i].courseGrade='C';

else if(student[i].score >=60 && student[i].score<70)

student[i].courseGrade='D';

else

student[i].courseGrade='F';

}

for(int i=0; i<5; i++) {

cout<<"First Name : “

<<student[i].firstName<<endl;

cout<<"Last Name : “

<<student[i].lastName<<endl;

cout<<"course Grade : “

<<student[i].courseGrade<<endl;

cout<<"test Score : “

<<student[i].testScore<<endl;

cout<<"Programming Score : “

<<student[i].programmingScore<<endl;

cout<<" GPA : “

<<student[i].GPA<<endl;

}

Page 60: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

ANSWER (3.2.4) int main()

{

for(int i=0; i<10; i++)

{

cout << “\ninsert first name : ";

cin >> employee[i].firstName;

cout << “\ninsert last name : ";

cin >> employee[i].lastName;

cout << “\ninsert id : ";

cin >> employee[i].personId;

cout << “\ninsert dept id : ";

cin >> employee[i].deptId;

cout << “\ninsert yearly salary: ";

cin >> employee[i].yearlySalary;

cout << “\ninsert monthly bonus: ";

cin >> employee[i].monthlyBonus;

}

for(int i=0; i<10; i++)

{

employee[i].monthlySalary

= employee[i].yearlySalary / 12;

double paycheck

= employee[i].monthlySalary

+ employee[i].monthlyBonus;

employee[i].yearToDatePaid = paycheck;

cout << "Year To Date Paid :“

<<employee[i].yearToDatePaid << endl;

}

}

Page 61: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD OVERVIEW

3.7 RECORDS AND ARRAY

3.8 RECORDS AND FUNCTION

Page 62: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORDS AND FUNCTION

CHAPTER 3

• PASS RECORD VARIABLE AS PARAMETER

• PASS RECORD MEMBER AS PARAMETER

• PASS ARRAY OF RECORDS AS PARAMETER

• RETURN RECORDS

Page 63: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD VARIABLE AS PARAMETER

o Recall the difference between array and struct involving aggregate operation:

AGGREGATE OPERATION ARRAY STRUCT

Arithmetic No No

Assignment No Yes

Input / Output No (except strings) No

Comparison No No

Parameter passing By reference only By value or reference

Function returning a value No Yes

Page 64: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD VARIABLE AS PARAMETER

o The following struct definition were given:

struct studentType

{

char name[25];

char matrixNo[15];

int icNo;

};

name

matrixNo

icNo

Page 65: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD VARIABLE AS PARAMETER

o Construct main function:

main() {

studentType s;//struct variable declaration

cout << “Enter Matrix No.: ”;

cin.getline(s.matrixNo, 15);

cout << “Enter Name: ”

cin.getline(s.name, 25);

cout << “Enter IC No.: ”;

cin >> s.icNo;

display(s) //function call

}

Page 66: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD VARIABLE AS PARAMETER (Pass entire struct)

o Construct function prototype:

void display(studentType);

o Construct function definition:

void display(studentType x)

{

cout << x.name << “ ”

<< x.matrixNo << “ ”

<< x.icNo;

}

Member’s name CANNOT BE RENAMED! But the arrangement of variable CAN BE MODIFIED.

Pass entire struct at once

Page 67: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD VARIABLE AS PARAMETER (Pass address of struct)

o Construct function prototype:

void display(studentType&);

o Construct function definition:

void display(studentType& x)

{

cout << x.name << “ ”

<< x.matrixNo << “ ”

<< x.icNo;

}

Pass only the address of struct

Member’s name CANNOT BE RENAMED! But the arrangement of variable CAN BE MODIFIED.

Page 68: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD MEMBERS AS PARAMETER

o The following struct definition were given:

struct studentType

{

char name[25];

char matrixNo[15];

int icNo;

};

name

matrixNo

icNo

Page 69: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD MEMBERS AS PARAMETER

o Construct main function:

main() {

studentType s; //struct variable declaration

cout << “Enter Matrix No.: ”;

cin.getline(s.matrixNo, 15);

cout << “Enter Name: ”

cin.getline(s.name, 25);

cout << “Enter IC No.: ”;

cin >> s.icNo;

//function call

display( s.matrixNo, s.name, s.icNo )

}

Pass members of struct individually

Page 70: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS RECORD MEMBERS AS PARAMETER

o Construct function prototype:

void display(char[], char[], int);

o Construct function definition:

void display(char matrix[], char

name[], int ic)

{

cout << matrix << “ ”

<< name << “ ”

<< ic;

}

Pass only the members of STRUCT individually

Member’s name CAN BE RENAMED! But the arrangement of variables CANNOT BE MODIFIED

Page 71: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS ARRAY OF RECORDS AS PARAMETER

o The following struct definition were given:

struct studentType

{

char name[25];

char matrixNo[15];

int icNo;

};

name

matrixNo

icNo

Page 72: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS ARRAY OF RECORDS AS PARAMETER

o Main function:

main() {

int arraySize = 10;

studentType s[arraySize];

getData(s, arraySize); //function call

for(int i = 0; i < arraySize; i++) {

cout << s[i].name << “ ”

<< s[i].matrixNo << “ ”

<< s[i].icNo;

}

} Member’s name CANNOT BE RENAMED! But the arrangement of variables CAN BE

MODIFIED

Page 73: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

PASS ARRAY OF RECORDS AS PARAMETER

o Function prototype:

void getData(studentType[]);

o Function definition: void getData(studentType s[], int size)

{

for(int i = 0; i < size; i++)

{

cout << “Enter Matrix No. and Name ”;

cin.getline(s[i].matrixNo, 15);

cin.getline(s[i].name, 25);

cout << “Enter IC No.: ”;

cin >> s[i].icNo;

}

}

Pass the array of struct

Page 74: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RETURN RECORD (By parameter)

o The following struct definition were given:

struct studentType

{

char name[25];

char matrixNo[15];

int icNo;

};

name

matrixNo

icNo

Page 75: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RETURN RECORD (By parameter)

o Function prototype:

void getData(studentType&);

o Function definition:

void getData(studentType& x)

{

cout << “Enter Matrix No.: ”;

cin.getline(x.matrixNo, 15);

cout << “Enter Name: ”

cin.getline(x.name, 25);

cout << “Enter IC No.: ”;

cin >> x.icNo;

}

Pass the address of struct, by reference

Page 76: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RETURN RECORD (By parameter)

o Main function:

main() {

studentType s;//struct variable declaration

getData(s) //function call

cout << s.name << “ ”

<< s.matrixNo << “ ”

<< s.icNo;

}

Member’s name CANNOT BE RENAMED! But the arrangement of variables CAN BE

MODIFIED

Page 77: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RETURN RECORD (By return value)

o The following struct definition were given:

struct studentType

{

char name[25];

char matrixNo[15];

int icNo;

};

name

matrixNo

icNo

Page 78: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RETURN RECORD (By return value)

o Function prototype:

studentType getData();

o Function definition: studentType getData() {

studentType s;

cout << “Enter Matrix No. and Name ”;

cin.getline(s.matrixNo, 15);

cin.getline(s.name, 25);

cout << “Enter IC No.: ”;

cin >> s.icNo;

return s;

}

Pass the type of struct

Page 79: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RETURN RECORD (By return value)

o Main function:

main() {

studentType s;//struct variable declaration

s = getData() //function call

cout << s.name << “ ”

<< s.matrixNo << “ ”

<< s.icNo;

}

Member’s name CANNOT BE RENAMED! But the arrangement of variables CAN BE

MODIFIED

Page 80: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXERCISE (3.2.5)

o Suppose that you have the following definitions: struct tourType {

char cityName[20];

int distance, hour, sec;

double min;

};

i. Declare the variable named destination of type tourType.

ii. Write C++ statements to store the following data in destination: cityName - chicago, distance - 550 miles, travelTime - 9 hours and 30 minutes.

iii. Write the definition of a function to output the data stored in a variable of type tourType.

iv. Write the definition of value –returning function that input data into a variable of type tourType.

v. Write the definition of void function with reference parameter of type tourType to input data in a variable of type tourType.

Page 81: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXERCISE (3.2.6)

o The following struct definition consists of four data members: book’s title, serial number, price and shelf location. struct Books {

char title[50];

int serialNo;

float price;

char location[20];

};

a) Create an array of 100 malay books

b) Write the function definition for each of the following

i. Function inputData(): This function receives two parameters; an array of malay books and the size of the array. It then reads data from the user and stores them into the array.

ii. Function preciousBook(): this function receives two parameters; an array of malay books and the size of the array. It determines the most expensive book and displays all the information about the book.

Page 82: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXERCISE (3.2.7) A bookshop wants to manage the information of the books sold in their shop. You have been assigned to develop the program to update the books information. The information stored consists of the titles, quantities and prices of books. The title, quantity and price of book are stored in a structure named bookStock. Assuming that there are FIVE HUNDRED (500) books managed by the bookshop. Write definition for the following functions. Note : function parameter must at least have an array of type bookStock. o InputData() : this function prompts user to enter book information and

stores them in array of type bookStock. o totalPrice(): this function calculates total price by summing up

multiplication result of quantities and prices. It will then return the result. o displayRestock(): this function displays the details of restocked

book by finding its quantity which is less than FIVE (5) books. o searchTitle(): this function searches book title and display the book

details. If the title is not found, display appropriate message. Searched book title is received through this function parameter.

Page 83: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

RECORD OVERVIEW

3.7 RECORDS AND ARRAY

3.8 RECORDS AND FUNCTION

Page 84: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXAMPLE 1 OF STRUCT WITH FUNCTION #include <iostream>

#include <cstring>

#include <conio.h>

void printBook( struct Books book );

struct Books {

char title[50];

char author[50];

char subject[100];

int book_id;

};

int main( )

{

struct Books Book1; // Declare Book1 of type Book

struct Books Book2; // Declare Book2 of type Book

strcpy( Book1.title, "Learn C++ Programming"); // book 1 specification

strcpy( Book1.author, "Chand Miyan");

strcpy( Book1.subject, "C++ Programming");

Book1.book_id = 6495407;

Page 85: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXAMPLE 1 OF STRUCT WITH FUNCTION

strcpy( Book2.title, "Telecom Billing"); // book 2 specification

strcpy( Book2.author, "Yakit Singha");

strcpy( Book2.subject, "Telecom");

Book2.book_id = 6495700;

printBook( Book1 ); // Print Book1 info

printBook( Book2 ); // Print Book2 info

getch();

}

void printBook( struct Books book )

{

cout << "Book title : " << book.title <<endl;

cout << "Book author : " << book.author <<endl;

cout << "Book subject : " << book.subject <<endl;

cout << "Book id : " << book.book_id <<endl;

}

Page 86: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXAMPLE 2 OF STRUCT WITH FUNCTION

#include <iostream.h>

#include <conio.h>

#include <stdio.h>

#include <cstring.h>

void display_details (struct person Someone_I_know); //function prototype

//declaraton of struct

struct person

{

char name [30];

int eye_colour;

float height;

};

int main()

{

struct person sibling, mother;

struct person spouse;

//initialization for person : sibling, mother and spouse

strcpy(sibling.name, "Diane");

sibling.eye_colour = 1;

sibling.height = 1.61;

Page 87: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

EXAMPLE 2 OF STRUCT WITH FUNCTION strcpy(mother.name,"Mary");

mother.eye_colour = 2;

mother.height = 1.44;

strcpy(spouse.name,"Helen");

spouse.eye_colour = 1;

spouse.height = 1.7;

display_details(sibling); //function call

display_details(mother);

display_details(spouse);

getch();

}

void display_details (struct person someone_I_know)

{

cout << "Name : " << someone_I_know.name <<endl;

cout << "Eye colour : ";

switch (someone_I_know.eye_colour)

{

case 1 : cout << "Brown";

break;

case 2 : cout << "Blue";

break;

case 3 : cout <<"Green";

break;

default : cout << "Unknown";

}

cout << endl;

cout << "Height : " << someone_I_know.height << " metres" << endl;

}

Page 88: CHAPTER 3: RECORDS (STRUCThabibalbi.weebly.com/uploads/2/1/2/9/21292280/csc138_struct_prog_record_ell.pdfAssume the definition of Exercise 1 1. Declare a checkingAccount variable and

THE END…..

CSC 138 – Structured Programming