Edited from Powerpoint Slides provided by Thomson Learning

Slides:



Advertisements
Similar presentations
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 10: Records ( structs )
Chapter 11: Records (structs)
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
Chapter 9: Arrays and Strings
Selection in C.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
EGR 2261 Unit 10 Records (structs)
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10: Records (structs)
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
LAB SESSION 5 STRUCT WITH FUNCTION 1. QUESTION 1 Given a struct definition: struct athleteRec { char name[30]; int age; char IC_number[20]; int score_event1;
Chapter 10: Records (structs)
Topic 7: Records in Arrays C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objective In this chapter, you will: Learn how to create.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Objectives: In this chapter, you will: Know the different between struct and array.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Structures (aka records, structs) Textbook Chapter 11 sections:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Standard Template Library (STL) - Use Vector and Deque
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records ( struct s)
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 7 User-Defined Methods.
Exercise : Write a program that print the final price of purchase at a store where everything costs exactly one dollar. Ask for the number of items purchased.
Chapter 7: User-Defined Functions II
REVISION CSC 138 – Structured Programming One Dimensional Array
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
MIS 120 Structures.
Algorithm development
Records C++ Structs Chapter 10.1.
Pointer Data Type and Pointer Variables II
C++ Structs.
Records C++ Structs Chapter 12.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
Review for Final Exam.
Strings A collection of characters taken as a set:
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Review for Final Exam.
Chapter 11: Records (structs)
Lec17 Structs.
Chapter 10: Void Functions
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Chapter 10 C Structures and Unions
Chapter 9: Records (structs)
Presentation transcript:

Edited from Powerpoint Slides provided by Thomson Learning C++ Programming: From Problem Analysis to Program Design, Fourth Edition Topic 4 : Records Edited from Powerpoint Slides provided by Thomson Learning

Objectives In this chapter, you will: Learn about records (structs) Examine various operations on a struct Explore ways to manipulate data using a struct Learn about the relationship between a struct and functions C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Records (structs) struct: collection of a fixed number of components (members), accessed by name Members may be of different types Syntax: C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programming Example struct studentType { char firstName[20]; char lastName[20]; char courseGrade; int testScore; int programmingScore; double GPA; }; void main( ) studentType newStudent; studentType student; newStudent.GPA = 0.0; strcpy(newStudent.firstName,"John"); strcpy(newStudent.lastName,"Brown"); } C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Records (structs) (continued) A struct is a definition, not a declaration C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Records (structs) (continued) C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Accessing struct Members The syntax for accessing a struct member is: The dot (.) is an operator, called the member access operator C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Accessing struct Members (continued) To initialize the members of newStudent: newStudent.GPA = 0.0; strcpy(newStudent.firstName,"John“); strcpy(newStudent.lastName,"Brown“); C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Accessing struct Members (continued) More examples: cin.getline(newStudent.firstName,20); 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

Assignment Value of one struct variable can be assigned to another struct variable of the same type using an assignment statement The statement: student = newStudent; copies the contents of newStudent into student C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Assignment (continued) 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

Comparison (Relational Operators) Compare struct variables member-wise No aggregate relational operations allowed To compare the values of student and newStudent: C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Input/Output No aggregate input/output operations on a struct variable Data in a struct variable must be read one member at a time The contents of a struct variable must be written one member at a time C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Exercise 1 Define a struct workerType that have 5 variables or members. It is name, staffID, age, salary and depatmentName. 2. Write a main program that will: a) declare two struct variable which is worker1 and worker2. b) input a details for worker1. c) assign the given value for worker2. d) display name and staff id of the worker that have more salary e) display the details for worker1. Variables Value name Rohaya Alias staffID 3451 age 32 salary 4500 departmentName Finance C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Exercise 2 1)Write a struct definition called studentRec that have 7 variables/data member. The data member for this struct are name, matric number, quiz mark, test mark, final mark and total mark. 2)Write a main program that will: a) Declare one struct variable called student. b) Input name, matric number, quiz mark, test mark and final mark. c) Calculate the total mark by using the given formula: Total mark= 20% of quiz mark + 20% of test mark + 60% of final mark

Exercise (cont.) d) Display the details of the student as below: ********************************* Name : XXXXXX Matric Number:XXXXXX Total Mark :XXXXXXX C++ Programming: From Problem Analysis to Program Design, Fourth Edition

struct Variables and Functions A struct variable can be passed as a parameter by value or by reference A function can return a value of type struct C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Programing Example (struct involve function) struct studentType { char firstName[20]; char lastName[20]; char courseGrade; int testScore; int programmingScore; double GPA; }; void inputData(studentType &s); void main() studentType newStudent; newStudent.GPA = 0.0; strcpy(newStudent.firstName,"John"); strcpy(newStudent.lastName,"Brown"); inputData(newStudent); cout<<"\nCourse Grade for newStudent:“ <<newStudent.courseGrade; cout<<"\nfirstName for newStudent:"<<newStudent.firstName; } void inputData(studentType &s) { cout<<"Course Grade:"; cin>>s.courseGrade; cout<<"\nProgramming Score:"; cin>>s.programmingScore; C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Exercise 1 Define a struct workerType that have 5 variables or members. It is name, staffID, age, salary and depatmentName. 2. Write a main program that will: a) declare two struct variable which is worker1 and worker2. b) call all the functions C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Exercise 1(continue) 3. Write 4 functions that will: a) input a details for worker1 and return the struct variable by using parameter by reference . b) assign the given value for worker2 and return the struct variable by using parameter by reference . Variables Value name Rohaya Alias staffID 18225 age 32 salary 4500 departmentName Finance C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Exercise 1(continue) c) display name of the worker that have more salary -this function will receive two struct variable d) display the details for worker1. -this function will receive one struct variable C++ Programming: From Problem Analysis to Program Design, Fourth Edition

Exercise2(function) Items Prices/Unit Plain egg RM1.50 Muffin RM2.00 Write a C++ program to help a local restaurant owner to automate his/her breakfast billing system. The program should do the following tasks:   Show the customer the different breakfast items offered by the restaurant. Allow the customer to select one item and the amount of item purchased from the menu. Calculate and print the bill. Assume that the restaurant offers the following breakfast items: Items Prices/Unit Plain egg RM1.50 Muffin RM2.00 French toast RM2.50 Fruit Basket RM3.00

Exercise3(function)..Continued.. Your program must contain at least the following functions:   Function showMenu: this function shows the different items offered by the restaurant and tells the user how to select the items. Function CalculateTotal: this function calculates the total price to be paid by the customer. The total price should include a 5% tax. Function printCheck: this function prints the check. Your program should use a structure to store information about the customer’s purchase: menu item, item price, the amount of item, tax value and the total amount due.  A sample output is as follows: Thanks for dining at Jonny’s Place Plain egg 2 RM3.00 Tax RM0.15  Amount Due RM3.15 C++ Programming: From Problem Analysis to Program Design, Fourth Edition