1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.

Slides:



Advertisements
Similar presentations
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Advertisements

StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Week 8 Arrays Part 2 String & Pointer
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
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.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
1 Structure part 1 & File Processing. 2 Structures.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Learners Support Publications Classes and Objects.
User Defined Data Types - updated Chapter 10: User Defined Data Types Objectives: In this chapter you will learn about, Introduction Declaring.
Chapter 9 Structured Data: Structs and ADTs (Data Base Programs with C++) Mr. Dave Clausen La Cañada High School.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
CS1010 Programming Methodology
Chapter 10-1: Structure.
C Programming Tutorial – Part I
EKT120 : Computer Programming
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
EKT120: Computer Programming
Chapter 10: Records (structs)
EKT150 : Computer Programming
EKT120: Computer Programming
Classes and Objects.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Structures In C Programming By Rajanikanth B.
Submitted By : Veenu Saini Lecturer (IT)
Programming Languages and Paradigms
Structure (i.e. struct) An structure creates a user defined data type
Getting Started With Coding
Presentation transcript:

1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming

2 Outline Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array of Structures UniMAP SEM I - 11/12EKT 120 Computer Programming

3 Introduction Structures Collection of related data items called components (or members) that are NOT necessarily of the same data type. Commonly used to define records to be stored in files. Usually the collections of related data item are characteristics in an object For example: Object Characteristics Book price, number of pages, year published Car price, year, model, colour Student name, matric_no, semester UniMAP SEM I - 11/12EKT 120 Computer Programming

4 Structure Definition Syntax: struct StructureTypeName { structure member declaration list }; Example: struct book { float fPrice; int iNumPages; int iYear; }; UniMAP SEM I - 11/12EKT 120 Computer Programming

UniMAP SEM I - 11/12EKT 120 Computer Programming 5 Structure Definition struct information A struct cannot contain an instance of itself Can contain a member that is a pointer to the same structure type A structure definition does not reserve space in memory Instead creates a new data type used to define structure variables

6 Structure Declaration After defining a structure type, we may declare variables that are of that type. A structure variable declaration requires these elements : keyword struct structure type name a list of variables names separated by commas concluding semicolon E.g. struct book sBook1; struct book sMyBook,sHisBook,sHerBook; UniMAP SEM I - 11/12EKT 120 Computer Programming

7 How does it look like in my program? #include struct book { float fPrice; int iNumPages; int iYear; }; int main( ) { struct book sBook1; …… } #include struct book { float fPrice; int iNumPages; int iYear; } sBook1; int main( ) { …… } or struct book sBook1; UniMAP SEM I - 11/12EKT 120 Computer Programming

8 Initializing Structures struct book sBook1 = {25.50, 690, 2005}; OR sBook1.fPrice = 25.50; sBook1.iNumPages = 690; sBook1.iYear = 2005; dot operator UniMAP SEM I - 11/12EKT 120 Computer Programming

9 Operations on Structures Members OperationNormal variableStructure member Read from userscanf(“%f”, &fPrice);scanf(“%f”, &sBook1.fPrice); Assign valuefPrice = 25.50;sBook1.fPrice = 25.50; Print as outputprintf(“%f\n”, fPrice);printf(“%f\n”, sBook1.fPrice); Copy to a variable fNewPrice = fPrice;fNewPrice = sBook1.fPrice; Pass value to a function if(fnFun1(fPrice) > 5)if(fnFun1(sBook1.fPrice) > 5) UniMAP SEM I - 11/12EKT 120 Computer Programming

10 Multiple Structures Variables struct book sMyBook, sHisBook, sHerBook; sMyBook  fPrice  iNumPages  iYear  fPrice  iNumPages  iYear  fPrice  iNumPages  iYear sHisBook sHerBook UniMAP SEM I - 11/12EKT 120 Computer Programming

11 Multiple Structures Variables e.g. struct book sMyBook, sHisBook, sHerBook; sMyBook. fPrice = 25.50; sHerBook.fPrice = 10.50; if(sMyBook.fPrice > sHerBook.fPrice) printf(“My book is more expensive than hers\n”); else printf(“My book is cheaper than hers\n”); UniMAP SEM I - 11/12EKT 120 Computer Programming

12 Sample Program #include struct book { float fPrice; int iNumPages; int iYear; }; int main() { struct book sMyBook = {25.50,690,2005}; struct book sHerBook; printf("Enter book price : "); scanf("%f", &sHerBook.fPrice); printf("Enter number of pages : "); scanf("%d", &sHerBook.iNumPages); printf("Enter year published : "); scanf("%d", &sHerBook.iYear); printf("My book :\n"); printf("%.2f\t%d\t%d\n", sMyBook.fPrice, sMyBook.iNumPages, sMyBook.iYear); printf("Her book :\n"); printf("%.2f\t%d\t%d\n", sHerBook.fPrice, sHerBook.iNumPages, sHerBook.iYear); if(sMyBook.iYear > sHerBook.iYear) printf("My book is the latest publication\n"); else printf("Her book is the latest publication\n"); return 0; } UniMAP SEM I - 11/12EKT 120 Computer Programming

13 Structures as Function Parameters  Like variables of any other data type, structure variables can be used as formal and actual function parameters.  In passing structure variables by value to a function, the operating systems makes a copy of the entire structure in the data area of the called function. UniMAP SEM I - 11/12EKT 120 Computer Programming

14 Structures as Function Parameters …… float fnComputePrice(struct book sBkC) ; //function prototype … int main( ) {struct book sBookC; …………… fNewPrice = fnComputePrice(sBookC ); //function call …… } float fnComputePrice(struct book sBkC ) //function definition {……. sBkC.fPrice=sBkC.fPrice + fTax; …… return(fBkC.fPrice); } UniMAP SEM I - 11/12EKT 120 Computer Programming

15 Structures as Function Parameters A nonvoid function of a structure type can return a structure of that structure type under the function’s name if we use a return statement in the function. struct book fnRead ();//function prototype int main( ) {………. struct book sB; sB = fnRead( );//function call ………. } struct book fnRead( )//function definition {struct book sBk; printf(“Enter price:”); scanf(“%f”, &sBk.fPrice); ….. return(sBk); } UniMAP SEM I - 11/12EKT 120 Computer Programming

16 Structures as Function Parameters If all members of a structure variable are not needed for a function to perform its task, we can pass only the required members. However, we must specify structure members using the component selection operator. int fnModifyYear(int iA, int iB, int iYear); //function prototype …….. int main( ) {struct book sBkC; ……………. iAvgYear=fnModifyYear(iAa, iBb, sBkC.iYear); //function call ……. } UniMAP SEM I - 11/12EKT 120 Computer Programming

17 Structures as Function Parameters It is possible to pass structure variables using pointers int main() {struct book sB; ……… fnRead(&sB); //function call } void fnRead(struct book *sBk) { printf(“Enter price:”); scanf(“%f”, &sBk->fPrice); printf(“Enter numpages:”); scanf(“%d”, &sBk->iNumPages); ……. } UniMAP SEM I - 11/12EKT 120 Computer Programming

18 Sample Program #include struct book { float fPrice; int iNumPages; int iYear; }; struct book fnRead(); void fnPrint(struct book, struct book); void fnCompare(int, int); int main() { struct book sMyBook = {25.50,690,2005}; struct book sSheBook; sSheBook=fnRead(); fnPrint(sMyBook, sSheBook); fnCompare(sMyBook.iYear, sSheBook.iYear); return 0; } struct book fnRead() { struct book sHerBook; printf("Enter book price : "); scanf("%f", &sHerBook.fPrice); printf("Enter number of pages : "); scanf("%d", &sHerBook.iNumPages); printf("Enter year published : "); scanf("%d", &sHerBook.iYear); return(sHerBook); } void fnPrint(struct book sMyBook, struct book sHerBook) { printf("My book :\n"); printf("%.2f\t%d\t%d\n", sMyBook.fPrice, sMyBook.iNumPages, sMyBook.iYear); printf("Her book :\n"); printf("%.2f\t%d\t%d\n", sHerBook.fPrice, sHerBook.iNumPages, sHerBook.iYear); } void fnCompare(int iMyYear, int iSheYear) { if(iMyYear > iSheYear) printf("My book is the latest publication\n"); else printf("Her book is the latest publication\n"); } UniMAP SEM I - 11/12EKT 120 Computer Programming

Sample program2 #include struct student {char name[50]; char program[5]; float gpa; }; void displayInfo(struct student); struct student readInfo(); int main() { struct student year1={"Aliana","RK20",3.33}; struct student year2; displayInfo(year1); year2=readInfo(); displayInfo(year2); return 0; } void displayInfo(struct student std) { printf("%s\t%s\t%.2f\n",std.name,std.program,std.gpa); } struct student readInfo() {struct student s; printf("Enter student name: "); scanf("%s", s.name); printf("Enter program: "); scanf("%s", s.program); printf("Enter gpa: "); scanf("%f", &s.gpa); return(s); } 19

Sample program3 #include struct student {char name[50]; char program[5]; float gpa; }; void displayInfo(struct student); void readInfo(struct student *); int main() { struct student postgrad; readInfo(&postgrad); displayInfo(postgrad); return 0; } void displayInfo(struct student std) {printf("%s\t%s\t%.2f\n",std.name,std.program,std.gpa); } void readInfo(struct student *s) { printf("Enter student name: "); scanf("%s", s->name); printf("Enter program: "); scanf("%s", s->program); printf("Enter gpa: "); scanf("%f", &s->gpa); } 20

21 An Array of Structures Suppose a company has 50 full-time employees. struct employeeType { char acFirstName[20]; char acLastName[20]; int iPersonID; char acDeptID[10]; double dYearlySalary; double dMonthlySalary; double dYearToDatePaid; double dMonthlyBonus; }; struct employeeType asEmployees[50]; UniMAP SEM I - 11/12EKT 120 Computer Programming

22 How it looks like UniMAP SEM I - 11/12EKT 120 Computer Programming [0] [1] [2]... [25] [26]... [48] [49] Array of structs asEmployees asEmployees[2] acFirstName acLastName iPersonID acDeptID dYearlySalary dMonthlySalary dYearToDatePaid dMonthlyBonus

23 How to access?? int iCounter; for(iCounter = 0; iCounter < 50; iCounter++) { scanf(“%s %s %d %s %lf”, &asEmployees[iCounter].acFirstName, &asEmployees[iCounter].acLastName, &asEmployees[iCounter].iPersonID, &asEmployees[iCounter].acDeptID, &asEmployees[iCounter].dYearlySalary); asEmployees[iCounter].dMonthlySalary=asEmployees[iCounter].dYearlySalary/12; asEmployees[iCounter].dYearToDatePaid = 0.0; asEmployees[iCounter].dMonthlyBonus = 0.0; } UniMAP SEM I - 11/12EKT 120 Computer Programming

24 How to access?? Suppose that for a given month the monthly bonuses are already stored in each employee’s record, and we have to calculate the monthly paycheck and update the yearToDatePaid amount. The following loop computes and prints the employee’s paycheck for the month: double dPayCheck; //variable to calculate the paycheck for(iCounter = 0; iCounter < 50; iCounter++) { printf(“%s %s”, asEmployees[iCounter].acFirstName, asEmployees[iCounter].acLastName); dPayCheck = asEmployees[iCounter].dMonthlySalary + asEmployees[iCounter].dMonthlyBonus; asEmployees[iCounter].dYearToDatePaid = asEmployees[iCounter].dYearToDatePaid + dPayCheck; } UniMAP SEM I - 11/12EKT 120 Computer Programming

25 Arrays in Structures Example const arraySize = 5; struct listType { int aiListElem[arraySize]; //array containing the list int iListLength; //length of the list }; struct listType sList; UniMAP SEM I - 11/12EKT 120 Computer Programming

26 How it looks like struct listType aiListElem iListLength UniMAP SEM I - 11/12EKT 120 Computer Programming

27 Structure Within a Structure struct nameType { string acFirst; string acMiddle; string acLast; }; struct addressType { string acAddress1; string acAddress2; string acCity; string acState; string acZip; }; struct dateType { string acMonth; string acDay; string acYear; }; struct contactType { string acPhone; string acCellphone; string acFax; string acPager; string ac ; }; struct employeeType { struct nameType sName; string acEmplID; struct addressType sAddress; struct dateType sHiredate; struct dateType sQuitdate; struct contactType sContact; string acDeptID; double dSalary; }; UniMAP SEM I - 11/12EKT 120 Computer Programming

28 Structure Within a Structure : How to access?? //variable declaration struct employeeType sNewEmployee; //declare 100 employees' records struct employeeType asEmployees[100]; sNewEmployee.dSalary = ; sNewEmployee.sName.acFirst = "Mary"; sNewEmployee.sName.acMiddle = "Beth"; sNewEmployee.sName.acLast = "Simmons"; UniMAP SEM I - 11/12EKT 120 Computer Programming

29 Structure Within a Structure : How to access?? The statement scanf(“%s”, &sNewEmployee.sName.acFirst); reads and stores a string into sNewEmployee.sName.acFirst. The statement sNewEmployee.dSalary = sNewEmployee.dSalary * 1.05; updates the salary of newEmployee UniMAP SEM I - 11/12EKT 120 Computer Programming

TQ 30 UniMAP SEM I - 11/12EKT 120 Computer Programming