Programming Structures.

Slides:



Advertisements
Similar presentations
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Advertisements

Structure.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Structures COMP104 Structs / Slide 2 Motivation: a new type * Structures hold data that belong together. * Examples: n Student record  student id, name,
Structured Data Types array array union union struct struct class class.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
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.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
Array, Structure and Union
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Struct COMP104 Struct / Slide 2 Motivation * Structures hold data that belong together. * Examples: n Student record  student id, name, major, gender,
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Data structures Dynamic data structures are data structures that grow and shrink as you need them to by allocating and deal locating memory from a place.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
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.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming II
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 1.2 Introduction to C++ Programming
User-Defined Data Types
LESSON 06.
Chapter 1.2 Introduction to C++ Programming
What header file contains C++ file I/O instructions?
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CO1401 Program Design and Implementation
Chapter 10-1: Structure.
Arrays Arrays exist in almost every computer language.
Structures and Unions.
Concepts and Basics of C++ Programming
Introduction to C++ October 2, 2017.
Student Book An Introduction
Concepts and Basics of C++ Programming
Structures Lesson xx In this module, we’ll introduce you to structures.
Variables with Memory Diagram
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
FUNCTIONS& FUNCTIONS OVERLOADING
Structures.
Structured Data Types array union struct class.
Structure As Function Arguments
Struct Data Type in C++ What Are Structures?
Data Structures.
Structures In C Programming By Rajanikanth B.
Programming Structures.
Strings Skill Area 313 Part C
CS31 Discussion 1D Winter18: week 6
Programming Structures.
Structures Structured Data types Data abstraction structs ---
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Structures Chapter 4.
Structures, Unions, and Enumerations
Structures Declarations CSCI 230
Programming Fundamental
Exercise 6 – Compound Types und Kontrollfluss
Getting Started With Coding
Presentation transcript:

Programming Structures

Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous in that it can be composed of data of different types. In contrast, array is homogeneous since it can contain only data of the same type.

Defination A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures can be declared in C++ using the following syntax: struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; Where type_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces {}, there is a list with the data members, each one is specified with a type and a valid identifier as its name

Structures Structures hold data that belong together. Examples: Student record: student id, name, major, gender, start year, … Bank account: account number, name, currency, balance, … Address book: name, address, telephone number, … In database applications, structures are called records.

Structures Individual components of a struct type are called members (or fields). Members can be of different A struct is named as a whole while individual members are named using field identifiers. Complex data structures can be formed by defining arrays of structs.

struct basics Definition of a structure: Example: The “Date” structure struct <struct-type>{ <type> <identifier_list>; ... } ; Example: struct Date { int day; int month; int year; Each identifier defines a member of the structure. The “Date” structure has 3 members, day, month & year.

struct examples Example: The “StudentGrade” structure has 5 members of struct StudentInfo{ int Id; int age; char Gender; double CGA; }; struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; The “StudentInfo” structure has 4 members of different types. The “StudentGrade” structure has 5 members of different array types.

struct examples Example: The “StudentRecord” structure has 4 struct BankAccount{ char Name[15]; int AcountNo[10]; double balance; Date Birthday; }; struct StudentRecord{ int Id; char Dept[5]; char Gender; The “BankAcount” structure has simple, array and structure types as members. The “StudentRecord” structure has 4 members.

struct basics Declaration of a variable of struct type: Example: <struct-type> <identifier_list>; Example: StudentRecord Student1, Student2; Student1 and Student2 are variables of StudentRecord type. Name Id Gender Dept Name Id Gender Dept Student1 Student2

struct basics struct product { int weight; double price; } ; product apple; product banana, melon; struct product { int weight; double price; } apple, banana, melon;

Structure E.g Initializing data members struct part { int modelnumber; int partnumber; float cost; }; int main () part part1, part2; part1.modelnumber = 1111; part1.partnumber = 111; part1.cost = 111.11; Initializing data members

Structure E.g cout<<“Enter Model number = ”; cin>>part2.modelnumber ; .. … …..; cin>>part2.partnumber ; .. .. .. ..; cin>>part2.cost ; cout<<"\nModel of Part1 = "<<part1.modelnumber; cout<<"\nPart of part 1 = "<<part1.partnumber; cout<<"\nCost of part1 = "<<part1.cost<<endl; cout<<"\nModel of part2 = "<<part2.modelnumber; cout<<"\nPart of part2 = "<<part2.partnumber; cout<<"\nCost of part2 = "<<part2.cost<<endl; return 0; }

(Out Put) Model of Part1 = 1111 Part of part 1 = 111 Cost of part1 = 111.11 Model of part2 = 222 Part of part2 = 2222 Cost of part2 = 222.222 Press any key to continue

Structure definition { int modelnumber; } ; struct part Keyword Structure name struct part { int modelnumber; int partnumber; Structure Members float cost; } ; Semicolon terminate definition Braces Delimit Structure Members

Structure definition The structure definition serves only as a blueprint for the creation of variables of type part. It does not itself create any structure variables; that is, it does not set aside any space in memory or even name any variables. This is unlike the definition of a simple variable, which does set aside memory.

The first statement in main() part part1, part2; Structure Name Variable Names

Memory defines two variables of type structure part. The definition reserve space in memory for part1 & part2. How Much Space? Enough to hold all the members of part1 & part2. part1 part2 modelnumber = 4byte modelnumber = 4byte partnumber = 4byte partnumber = 4byte cos t= 4byte cos t= 4byte (Because int and float data type take 4 byte )

Accessing Structure members Members of part1 part1.modelnumber = 1111; part1.partnumber = 111; part1.cost = 111.11;

Accessing Structure members Members of part2 Entered by user part2.modelnumber = 222; part2.partnumber = 2222; part2.cost = 222.222;

Accessing Structure members Can access using “dot operator” Also know as member “access operator” The variable name must be used to distinguish one variable from another, such as part1, part2.

Initializing Structures Members part part2 = {1980,321,22.22};

struct part {. int modelnumber;. int partnumber; struct part { int modelnumber; int partnumber; float cost; }; int main (){ part part1, part3;; part part2 = {222,21232,22.22}; cin>>part1.modelnumber; cin>>part1.partnumber; cin>>part1.cost;

part3 = part1; cout<<"\nModel of Part1 = "<<part1.modelnumber; cout<<"\nPart of part 1 = "<<part1.partnumber; cout<<"\nCost $ of part1 = "<<part1.cost<<endl; cout<<"\nInitail Model = "<<part2.modelnumber; cout<<"\nInitial Part = "<<part2.partnumber; cout<<"\nInitial Cost = "<<part2.cost<<endl; cout<<"\nInitail Model = "<<part3.modelnumber; cout<<"\nInitial Part = "<<part3.partnumber; cout<<"\nInitial Cost = "<<part3.cost<<endl; return 0; }

Out Put Model of Part1 = 1111 Part of part 1 = 111 Cost $ of part1 = 111.11 Initail Model = 222 Initial Part = 21232 Initial Cost = 22.22 Initail Model = 1111 Initial Part = 111 Initial Cost = 111.11 Press any key to continue

Example Write a program in C++ that shows the area of 3 room's. Using Structure namely "distance". Take input of feet & inches from user for variable d1 (feet & inches), assign variable d2 = {10, 5.25} values. Now add feet and inches of d1 & d2 and store in d3. Display d1 (feet & inches) d2 (feet & inches) d3 (feet & inches) separately. Put Condition if d1 & d2 inches increase by 12 it become a foot.

Code struct Distance { int feet; float inches; }; void main() Distance d1,d3; Distance d2= { 11, 6.25 }; cout<<“\n Enter feet:”; cin>>d1.feet; cout<< “Enter inches:”; cin>>d1.inches; d3.inches= d1.inches + d2.inches; d3.feet=d1.feet+ d2.feet; if(d3.inches>=12.0) { d3.inches=d3.inches – 12.0; d3.feet++; } cout<< d1.feet<<“---”<<d1.inches<<“ + “; cout<<d2.feet<<“---”<<d2.inches<<“ = “; cout<<d3.feet<<“---”<<d3.inches;

Defining of objects struct product { int weight; double price; } apple, banana, melon;

#include <iostream> #include <string> #include <sstream> using namespace std; struct movies_t { string title; int year; } mine, yours; void printmovie (movies_t movie); int main () { string mystr; mine.title = "2001 A Space Odyssey"; mine.year = 1968; cout << "Enter title: "; getline (cin,yours.title); cout << "Enter year: "; getline (cin,mystr); stringstream(mystr) >> yours.year; cout << "My favorite movie is:\n "; printmovie (mine); cout << "And yours is:\n "; printmovie (yours); return 0; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n";

Arrays of structures An ordinary array: One type of data An array of structs: Multiple types of data in each array element. 0 1 2 … 98 99 0 1 2 … 98 99

Arrays of structures We often use arrays of structures. Example: StudentRecord Class[100]; strcpy(Class[98].Name, "Chan Tai Man"); Class[98].Id = 12345; strcpy(Class[98].Dept, "COMP"); Class[98].gender = 'M'; Class[0] = Class[98]; Chan Tai Man 12345 M COMP . . . 0 1 2 … 98 99

Arrays inside structures We can use arrays inside structures. Example: struct square{ point vertex[4]; }; square Sq; Assign values to Sq using the given square (4, 3) (10, 3) (4, 1) (10, 1) x y x y x y x y

Example Create a structure called emp that contains three members, int id, char name[100], float sal. Ask the user to fill in data for three employees and then display information for each employee. Hint Variable of struct emp will be array Use while / for loop to control array

Que 3 (Code) #include<iostream.h> struct emp { int id; char name[100]; float sal; }; void main () emp ob1[3]; int c=0; //Index Variable

Que 3 (Code) while(c<3) { cout<<"Enter ID of Employee "<<c+1<<" = "; cin>>ob1[c].id; cout<<"Enter name of Employee "<<c+1<<" = "; cin>>ob1[c].name; cout<<"Enter salary of Employee "<<c+1<<" = "; cin>>ob1[c].sal; c++; cout<<"\n\n"; }

Que 3 (Code) c = 0; while (c<3) { cout<<"\nId of emp "<<c+1<<" = "<<ob1[c].id; cout<<"\nName of emp "<<c+1<<" = "<<ob1[c].name; cout<<"\nSalary ofemp "<<c+1<<" = "<<ob1[c].sal; c++; cout<<"\n\n"; }

Que 3 (Out Put) Enter ID of Employee 1 = 123 Enter name of Employee 1 = Yasir Enter salary of Employee 1 = 50000 Enter ID of Employee 2 = 124 Enter name of Employee 2 = Ali Enter salary of Employee 2 = 55000 Enter ID of Employee 3 = 125 Enter name of Employee 3 = Aamir Enter salary of Employee 3 = 65000 Id of emp 1 = 123 Name of emp 1 = Yasir Salary ofemp 1 = 50000 Id of emp 2 = 124 Name of emp 2 = Ali Salary ofemp 2 = 55000 Id of emp 3 = 125 Name of emp 3 = Aamir Salary ofemp 3 = 65000 Press any key to continue