Structures - Part II aggregate operations arrays of type struct

Slides:



Advertisements
Similar presentations
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
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.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Structured Data Types array array union union struct struct class class.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
計算機程式語言 Lecture 13-1 國立臺灣大學生物機電系 13 Structures.
Module 4: Structures ITEI222 Advanced Programming.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Struct Data Type in C++ What Are Structures?
Data Types Storage Size Domain of all possible values Operations 1.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Example 21 #include<iostream.h> int main() { char Letter = 0;
Test 2 Review Outline.
MT262A Review.
Classes and Data Abstraction
CO1401 Program Design and Implementation
Chapter 9: Pointers.
Quiz # 02 Design a data type Date to hold date
Arrays Arrays exist in almost every computer language.
Records C++ Structs Chapter 10.1.
Arrays Part-1 Armen Keshishian.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
DATA HANDLING.
I Know What I Want to Do – Now What??
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Chapter 9: Pointers.
Pass by Reference, const, readonly, struct
CS 1430: Programming in C++.
Structs And Arrays.
Passing Structures Lesson xx
Chapter 9: Pointers.
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
More About Data Types & Functions
Review for Final Exam.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Lecture 8: A Simple System
CS148 Introduction to Programming II
Reference Parameters.
Structured Data Types array union struct class.
Struct Data Type in C++ What Are Structures?
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Review for Final Exam.
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
CS31 Discussion 1D Winter18: week 6
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
HNDIT11034 More Operators.
Structures Structured Data types Data abstraction structs ---
CS31 Discussion 1H Fall18: week 9
Functions Chapter No. 5.
Presentation transcript:

Structures - Part II aggregate operations arrays of type struct nested structures compared to classes

Aggregate Operations Operation Arrays Structs I/O No (except strings) No Assignment No Yes Arithmetic No No Comparison No No Parameter pass. Ref. only Either value or ref. Funct. return value No Yes

Arrays of Structures struct Salaried { char dept[5]; int salary; int vac_days; }; // array of 20 elements, each of type Salaried Salaried emp[20];

Arrays of Structures ... emp[0].dept = “Purc”; emp[0].salary = 34560; emp[0].vac_days = 14; ... emp[2].salary = 32100; emp[2].dept = “Ship”; emp[2].vac_days = 10; … emp[19].dept = “Acct”; emp[19].salary = 22500; emp[19].vac_days = 12;

Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // an array of 3 records of type Payroll Payroll employee[3];

Arrays of Structures Payroll employee[3] = { {11, “Begay”, 7.25}, // load array -- there are other ways to load the array Payroll employee[3] = { {11, “Begay”, 7.25}, {12, “Gioseffi”, 6.50}, {13, “Marra”, 9.00} }; // display array for(ndx = 0; ndx < 3; ndx++) cout << ‘\n’ << employee[ndx].id << setw(20) << employee[ndx].name << setw(20) << employee[ndx].payrate;

Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // prototypes void loadarray(Payroll [3]); void showarray(Payroll [3]); *

Arrays of Structures void main() { //declare array of 3 records of type Payroll // and initialize the first record Payroll employee[3] = { 11, "Begay", 7.25 }; loadarray(employee); // calls showarray(employee); cout << endl<<endl; // for formatting } // end main()

Arrays of Structures // load array - data typically entered via file input void loadarray(Payroll staff[3]) { // begin at 1 because [0] already entered for(int ndx = 1; ndx < 3; ndx++) { cout << "Enter the ID, name, and pay rate: "; cin >> staff[ndx].id >> staff[ndx].name >> staff[ndx].payrate; cout << endl; }

Arrays of Structures void showarray(Payroll staff[3]) { cout << setiosflags(ios::fixed) << setprecision(2); for(int ndx = 0; ndx < 3; ndx++) cout << '\n' << setw(5) << staff[ndx].id << setw(13) << staff[ndx].name << setw(10) << staff[ndx].payrate; }

In Class Assign. 3 Element at. num at. mass density tungsten (W) 74 183.850 19.300 sulfur (S) 16 32.064 2.07 carbon (C) 6 12.011 2.260 Write the plan first. 9. Write a program which: a. creates an array of structures b. uses a global constant for the array size c. contains functions to load and to display d. format similar to the above chart

In Class Assign. 3-ans. #include<iostream.h> void main() #include<iomanip.h> struct Element { char symbo; int at_num; double at_mass; double density; }; const int ARR_SIZE = 3; void loadarray(Element [ARR_SIZE ]); void showarray(Element [ARR_SIZE ]); void main() { //declare array - type Element Element atom[ARR_SIZE ]; loadarray(atom); // calls showarray(atom); cout << endl<<endl; } // end main()

Nested Structures * struct Date { int month; int day; int year; }; struct vital_Data { char name[15]; char dept[10]; int ID; Date birth; // Date must be previously defined double payrate; }; *

Nested Structures vital_Data Courtney; // declaration of an object // assignments of data to an object Courtney.name = “Lawrence”; Courtney.dept = “personnel”; Courtney.ID = 1234; Courtney.birth = {10, 25, 87}; // this is a struct Coutrney.payrate = 12.75;

Nested Structures 1. Write the cin statements for the department and the birthday. 2. Write the cout statements for the department and the birthday.

Nested Structures personnel[ndx].name; personnel[ndx].dept; * * * * * void loadarray(vital_Data personnel[ARR_SIZE ]) { for(int ndx = 1; ndx < ARR_SIZE ; ndx++) { cout << "\nEnter the name: "; cin >> cout << "Enter the department: "; cout << "Enter the id# and the payrate: "; cin >> >> cout << "Enter the birth date (dd mm yy) "; >> >> } } personnel[ndx].name; personnel[ndx].dept; personnel[ndx].ID personnel[ndx].payrate; personnel[ndx].birth.day personnel[ndx].birth.month personnel[ndx].birth.year; * * * * *

Formatted Output ID# name department birthday payrate 1234 Lawrence personnel 10/ 5/87 12.75 765 Newman shipping 2/29/59 3.11 cout << setiosflags(ios::fixed | ios::right); for(int ndx = 0; ndx < ARR_SIZE ; ndx++) cout << '\n' << setw(5) << setprecision(0) << personnel[ndx].ID << setw(12) << personnel[ndx].name << setw(11) << personnel[ndx].dept << setw(5) << personnel[ndx].birth.day <<'/' << setw(2) << personnel[ndx].birth.month <<'/' << setw(2) << personnel[ndx].birth.year << setw(7) << setprecision(2) << personnel[ndx].payrate;

Structure vs. Class By default: struct have public member variables class have private member variables

Class Class syntax: class Classname { public: list of function prototypes private: list of private variable declarations };

Class An example: class Date { public: private: int day; int month; int year; };

Imagination is more important than knowledge. Albert Einstein