CO1401 Program Design and Implementation

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
Advertisements

Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Chapter 11 – Structured Data
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.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CS102 Introduction to Computer Programming Chapter 11 Structured Data.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
Starting Out with C++ 2nd Edition, by Tony Gaddis 1 Chapter 7 – Classes and Structured Data.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Structures Combining data types into a logical groupings.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
1 C++ Classes and Data Structures Course link…..
Structured Data.
Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their own range (or domain) of data and their own set of.
Classes and Data Abstraction
11 Chapter Structured Data
Chapter 7 – Arrays.
Structured Data (Lecture 07)
Objectives Identify the built-in data types in C++
CO1401 Programming Design and Implementation
Programming Structures.
Arrays Part-1 Armen Keshishian.
Introduction to C++ October 2, 2017.
Student Book An Introduction
Multi-dimensional Array
Structures - Part II aggregate operations arrays of type struct
Data Types – Structures
DATA HANDLING.
Structures Lesson xx In this module, we’ll introduce you to structures.
Enumeration used to make a program more readable
Data Types – Structures
Chapter 2 Elementary Programming
Chapter 5 Function Basics
Starting Out with C++: From Control Structures through Objects
CS150 Introduction to Computer Science 1
Arrays and Arrays as Parameters
Starting Out with C++: From Control Structures through Objects
Chapter 11: Structured Data.
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.
Structured Data Types array union struct class.
Struct Data Type in C++ What Are Structures?
Chapter 3 Input output.
Let’s all Repeat Together
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
Arrays Arrays A few types Structures of related data items
What Actions Do We Have Part 1
Programming Structures.
Standard Version of Starting Out with C++, 4th Edition
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Programming Structures.
Structures Structured Data types Data abstraction structs ---
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Programming Fundamental
Presentation transcript:

CO1401 Program Design and Implementation Week 7 Akiyo Kano

This Week Structures What are structures? How to define structures. How to define members of structures. How to create an instance of structures. Arrays of structures.

Structures With an array, all the elements have to be the same type and it is accessed by a numerical position in the array. But what if we want to group data of different types? e.g. Details about employees - name, age, rate, etc. which are different data types. C++ allows you to group several variables together in a single item known as a structure.

Structure A structure is a collection of related data, which does not have to be the same type, and each field is accessed by name. Structure Name ID Num D.O.B Rate Address[] D.O.B is Date of Birth. Employee

Example A company payroll system might keep the following variables, which relate to a single employee: int EmpNumber; // Employee Number char Name[SIZE]; // Employee’s Name double Hours; // Hours worked double PayRate; // Hourly Rate paid double GrossPay; // Gross pay All these variables are related because they hold data about the same employee.

Making It Clear These variables on their own does not make it clear that they belong together. We can package them in a structure to create a relationship between them. Group them into a structure.

Declaring a Structure Before we can use a structure, we must first declare it. Syntax for declaring structure: struct tag { variable declaration; }; “Members” Don’t forget the semi-colon at the end of the brackets!!

Example - PayRoll Structure const int SIZE = 25; // Array Size struct PayRoll { int EmpNumber; //Employee Number char Name[SIZE]; // Empolyee’s Name double Hours; // Hours worked double PayRate; // Hourly rate double GrossPay; // Gross Pay };

Structure Declaration Declaring a structure does not define the values of a varaible. It is bit like defining what the data type int is. It tells the compiler what the structure PayRoll is made of. Creates a new data type named PayRoll like int, char, float, but defined by you.

Defining An Instance of A Structure Now that we have defined a new data type, we can create variables that is of the new data type. int Num; Num is a variable of type int. PayRoll DeptHead; DeptHead is a variaible of type PayRoll you defined. We say DeptHead is an instance of the structure PayRoll

Template & Forms Defining your structure is like creating a template for someone to fill in. Declaring a DeptHead variable of the type PayRoll is like photocopying the form, and assigning it to that person. EmpNumber Name Hours PayRate GrossPay PayRoll “Template” EmpNumber Name Hours PayRate GrossPay DeptHead

Declaring Multiple Instances Just as you can declare multiple variables of the same data type on one line, you can do the same for multiple instances of a structure: int Num1, Num2, Num3; PayRoll DeptHead, Foreman; Associate; Seperate instrances of PayRoll structure, containing Their own members with the same name.

PayRoll DeptHead, Foreman; Associate; EmpNumber Name Hours PayRate GrossPay DeptHead Foreman Associate EmpNumber Name Hours PayRate GrossPay PayRoll “Template”

More Examples struct Time { int Hour; int Minutes; int Seconds; } Time Now; Time Before; struct Date { int Day; int Month; int Year; } Time Today; Time Yesterday;

Steps In Implementing Structures There are three steps to implementing and using structures: Create the structure Define instances of the structure Use the instances

Accessing Structure Members In arrays, to access the elements in it, we had to define where abouts in the array it is at. In structures, we don’t have to worry about where it is, as we can access each member by name. we do this by using the dot operator (.)

Assigning a Value to a Member To assign a value to a member of an instance: DeptHead.EmpNumber = 457; Foreman.EmpNumber = 897; EmpNumber Name Hours PayRate GrossPay 475 DeptHead EmpNumber Name Hours PayRate GrossPay 897 Foreman

Also... You can initialise values of members when the variable is declared: Struct Time { int Hour; int Minute; int Seconds; }; main() Time Now = {12, 30, 10}; }

Accessing Value of a Member If you want to print out all details of DeptHead: cout << DeptHead.EmpNumber << endl; cout << DeptHead.Name << endl; cout << DeptHead.Hours << endl; cout << DeptHead.PayRate << endl; cout << DeptHead.GrossPay << endl;

Entire Program #include <stdafx.h> #include <iostream> #include <iomanip> using namespace std; const int SIZE = 25; struct PayRoll { int EmpNumber; // Employee number char Name[SIZE]; // Employee’s name double Hours; // Hours worked double PayRate; // Hourly Pay Rate double GrossPay; // Gross Pay };

// Get the Employee’s Number void main() { PayRoll Employee; // Get the Employee’s Number cout << “Enter the Employee’s number: “ << endl; cin >> Employee.EmpNumber; // Get the employee’s Name cout << “Enter employee’s name: “ << endl; cin.ignore(); //skip remaining \n char cin.getline(Employee.Name, SIZE); You will need the ignore function for the cin to ignore the next character in the input buffer. This is necessary for the cin.getline statement to work properly in the program.

// Get the hours worked by Employee cout << “Enter hours worked: “ << endl; cin >> Employee.Hours; // Get the employee’s hourly rate cout << “What is the Hourly pay rate?: “ << endl; cin >> Employee.PayRate; //calculate the gross pay Employee.GrossPay = Employee.Hours * Employee.PayRate;

// Display the employee Data cout << “Here is the employee data\n”; cout << “Name: “ << Employee.Name << endl; cout << “Number: “ << Employee.EmpNumber << endl; cout << “Hours worked: “ << Employee.Hours << endl; cout << “Hourly Rate: “ << Employee.PayRate << endl; cout << fixed << showpoint << setprecision(2); cout << “Gross Pay: £” << Employee.GrossPay << endl; system(“pause”); }

Displaying Members Note that the contents of a structure variable cannot be displayed by passing the entire variable to cout. cout << Employee << endl; will not work!!

Comparing Members Similarly, we cannot just compare if two variables are the same: if (DeptHead == Associate) will not work! if (DeptHead.PayRate == Associate PayRate)

Arrays of Structures An array can only hold variables of the same type. But structure is a data type. So we can create an array of a particular structure! int Array[5] PayRoll Array[5] This can hold all the data about each employee in each element of the array.

Example - Book Info struct BookInfo { char Title[50]; char Author[30]; char Publisher[25]; double price; }; BookInfo BookList[20]; This array will hold information about 20 books

Arrays of Structures To find the title of the book in BookList[5]: BookList[5].title To display all the info on all the books: for (int x=0; x < 20; x++) { cout << BookList[x].Title << endl; cout << BookList[x].Author << endl; cout << BookList[x].Publisher << endl; cout << BookList[x].Price << endl; }

Also... As the members Title, Author and Publishers are also arrays, their individual elements may be accessed as well: cout << BookList[10].Title[0]; This will cout the first letter of the title of the eleventh book in the BookList array.

Functions With Structures Just like any other variable, a function can accept a structure as a parameter. Functions can also return a structure. Function prototype should come AFTER the structure declaration. This allows us to return several variables (members) from a function, as long as the function returns one structure.

struct Time { int Hour; int Minute; int Second; }; Time AddHour(Time); main() Time Now, Soon; Now.Hour = 12; Now.Minute = 30; Now.Second = 10; Soon = AddHour(Now); }

Time AddHour(Time Now){ Time Soon; Soon.Hour = (Now.Hour+1); Soon.Minute = Now.Minute; Soon.Second = Now.Second; return Soon; }

Summary Structure is a form of data type that you can define, made up of collection of members of various data types. A variable can be declared as having a structure type you defined. (instance of a structure) You can access members of a variable by using the dot operator (.) Arrays can be constructed to hold variables of one structure type.