CS1201: Programming Language 2 Structure By: Nouf Almunyif.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
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.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Chapter 11 – Structured Data
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 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.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CS102 Introduction to Computer Programming Chapter 11 Structured Data.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
Structured Data Types array array union union struct struct class class.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Programmer Defined Structures (Records)
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
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.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
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.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
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.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
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.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
CS1201: Programming Language 2 Classes and objects.
1 CS161 Introduction to Computer Science Topic #15.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
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.
Computer Programming II
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
CO1401 Program Design and Implementation
Structured Data (Lecture 07)
Programming Structures.
Pointer Data Type and Pointer Variables
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Starting Out with C++: From Control Structures through Objects
Structured Data Types array union struct class.
Arrays Arrays A few types Structures of related data items
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Presentation transcript:

CS1201: Programming Language 2 Structure By: Nouf Almunyif

Data Types C++ has several primitive data types:

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 operations that may be performed on them. ▫The programmer decides what values are acceptable for the data type ▫The programmer decides what operations may be performed on the data type

C++ allows you to group several variables together into a single item known as a structure.

Structure A data structure is a group of data elements joined together under one name. These data elements, known as members, can have different types and different lengths.

Structure Data structures are declared in C++ using the following syntax: struct structure_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3;.. } ; Note semicolon!!!

Structure Example: struct PayRoll { int EmpNumber; string Name; float Hours; float PayRate; float salary; };

8 empNumber name hours payRate Salary Emp1 Structure Variable Name Members

Structure Example: struct PERSON { // Declare PERSON struct type int age; // Declare member types long ss; float weight; char name[25]; } ; int main() { PERSON brother; // C++ style structure declaration brother.age = 7; // assign values to members}

Structure Example: struct product { int weight; float price; } apple, banana, melon;

Two steps to implementing structures: Create the structure declaration. This establishes the tag (or name) of the structure and a list of items that are members. Declare variables (or instances) of the structure and use them in the program to hold data.

12 Accessing Structure Members The dot operator (.) allows you to access structure members in a program

Program #include Using namesspace std; struct PayRoll { int EmpNumber; string Name; float Hours; float PayRate; float salary;}; void main(void) { PayRoll employee; cout << "Enter the employee's number: "; cin >> employee.empNumber; cout << "Enter the employee's name: "; cin>>employee.name; cout << "How many hours did the employee work? "; cin >> employee.hours; cout << "What is the employee's hourly payrate? "; cin >> employee.payRate; employee.salary= employee.hours * employee.payRate; cout << "Here is the employee's payroll data:\n"; cout << "Name: " << employee.name << endl; cout << "Number: " << employee.empNumber << endl; cout << "Hours worked: " << employee.hours << endl; cout << "Hourly Payrate: " << employee.payRate << endl; cout << “salary: $" << employee.salary << endl; } 13

14 Program Output with Example Input Enter the employee's number: 489 [Enter] Enter the employee's name: Jill Smith [Enter] How many hours did the employee work? 40 [Enter] What is the employee's hourly payrate? 20 [Enter] Here is the employee's payroll data: Name: Jill Smith Number: 489 Hours worked: 40 Hourly Payrate: 20 salary: $800.00

15 Displaying a Structure The contents of a structure variable cannot be displayed by passing the entire variable to cout. For example, assuming employee is a PayRoll structure variable, the following statement will not work: cout << employee << endl; //won’t work!

16 Program #include struct Circle { float radius; // نصف القطر float diameter; // القطر float area; //مساحة }; const float pi = ; void main(void) { Circle c; cout << "Enter the diameter of a circle: "; cin >> c.Diameter; c.Radius = C.Diameter / 2; c.Area = pi * pow(c.Radius, 2.0); cout << "The radius and area of the circle are:\n"; cout << "Radius: " << c.radius << endl; cout << "Area: " << c.area << endl; } Area = pi * radius^ 2 Enter the diameter of a circle: 10 [Enter] The radius and area of the circle are: Radius: 5 Area: 78.54

17 Strings as Structure Members When a character array is a structure member, use the same sting manipulation techniques with it as you would with any other character array.

18 Program // This program uses a structure to hold someone's first, // middle, and last name. #include struct Name { char first[15]; char middle[15]; char last[15]; char full[45]; }; READ

19 Program continues void main(void) { Name person; cout << "Enter your first name: "; cin >> person.first; cout << "Enter your middle name: "; cin >> person.middle; cout << "Enter your last name: "; cin >> person.last; strcpy(person.full, person.first); strcat(person.full, " "); strcat(person.full, person.middle); strcat(person.full, " "); strcat(person.full, person.last); cout << "\nYour full name is " << person.full << endl; } READ

20 Program Output with Example Input Enter your first name: Josephine [Enter] Enter your middle name: Yvonne [Enter] Enter your last name: Smith [Enter] Your full name is Josephine Yvonne Smith READ

21 Structures as Function Arguments Structure variables may be passed as arguments to functions.

22 showRect(box); void showRect(Rectangle r) { cout << r.length << endl; cout << r.width << endl; cout << r.area << endl; }

23 Program #include struct Iitem{ int partNum; char description[50]; int onHand; float price;}; void ShowItem(Item);// Function prototype void main(void) {Item Part = {171, "Industrial Widget", 25, 150.0}; ShowItem(part);} void ShowItem(InvItem piece) {cout << "Part Number: " << piece.partNum << endl; cout << "Description: " << piece.description << endl; cout << "Units On Hand: " << piece.onHand << endl; cout << "Price: $" << piece.price << endl; } Part Number: 171 Description: Industrial Widget Units On Hand: 25 Price: $150.00