Ekt120 Lecture10 Structures

27
1 Structures

description

Structures-computer programming

Transcript of Ekt120 Lecture10 Structures

Page 1: Ekt120 Lecture10 Structures

1

Structures

Page 2: Ekt120 Lecture10 Structures

2

Outline Introduction Structure Definitions and

Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array of Structures

Page 3: Ekt120 Lecture10 Structures

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 CharacteristicsBook price, number of pages, year publishedCar price, year, model, colourStudent name, matric_no, semester

Page 4: Ekt120 Lecture10 Structures

4

Structure Definition Syntax:

struct StructureTypeName { structure member declaration list};

Example:struct book {float price;int numpages;int year;

};

Page 5: Ekt120 Lecture10 Structures

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

Page 6: Ekt120 Lecture10 Structures

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 book1;struct book my_book,his_book,her_book;

Page 7: Ekt120 Lecture10 Structures

7

How does it look like in my program?

#include <stdio.h>struct book{

float price;int numpages;int year;

};int main( )

{struct book book1;……

}

#include <stdio.h>struct book{

float price;int numpages;int year;

} book1;

int main( ){

……}

or

struct book book1;

Page 8: Ekt120 Lecture10 Structures

8

Initializing Structuresstruct book book1 = {25.50, 690, 2005};ORbook1.price = 25.50;book1.numpages = 690;book1.year = 2005;

dot operator

Page 9: Ekt120 Lecture10 Structures

9

Operations on Structures Members

Operation Normal variable Structure member

Read from user

scanf(“%f”, &price); scanf(“%f”, &book1.price);

Assign value price = 25.50; book1.price = 25.50;

Print as output printf(“%f\n”, price);

printf(“%f\n”, book1.price);

Copy to a variable

newprice = price; newprice = book1.price;

Pass value to a function

if(fun1(price) > 5) if(fun1(book1.price) > 5)

Page 10: Ekt120 Lecture10 Structures

10

Multiple Structures Variablesstruct book my_book, his_book, her_book;

my_book

price

numpages

year

price

numpages

year

price

numpages

year

his_book her_book

Page 11: Ekt120 Lecture10 Structures

11

Multiple Structures Variablese.g.struct book my_book, his_book, her_book;my_book. price = 25.50;her_book.price = 10.50;if(my_book.price > her_book.price)

printf(“My book is more expensive than hers\n”);else

printf(“My book is cheaper than hers\n”);

Page 12: Ekt120 Lecture10 Structures

12

Sample Program#include <stdio.h>

struct book { float price; int numpages; int year;};

int main(){ struct book my_book = {25.50,690,2005}; struct book her_book;

printf("Enter book price : "); scanf("%f", &her_book.price); printf("Enter number of pages : "); scanf("%d", &her_book.numpages); printf("Enter year published : "); scanf("%d", &her_book.year);

printf("My book :\n");printf("%.2f\t%d\t%d\n", my_book.price, my_book.numpages, my_book.year);printf("Her book :\n");printf("%.2f\t%d\t%d\n", her_book.price, her_book.numpages, her_book.year);

if(my_book.year > her_book.year) printf("My book is the latest publication\n");else printf("Her book is the latest publication\n"); return 0;}

Page 13: Ekt120 Lecture10 Structures

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.

Page 14: Ekt120 Lecture10 Structures

14

Structures as Function Parameters

……float compute_price(struct book bk); //function prototype…int main( ){ struct book bookC;

……………new_price = compute_price(bookC); //function call……

}float compute_price(struct book bkC) //function definition

{ …….bkC.price=bkC.price + tax;……return(bkC.price);

}

Page 15: Ekt120 Lecture10 Structures

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 read (); //function prototypeint main( ){ ……….

struct book b;b = read( ); //function call……….

}struct book read( )//function definition{ struct book bk;

printf(“Enter price:”);scanf(“%f”, &bk.price);…..return(bk);

}

Page 16: Ekt120 Lecture10 Structures

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 modify_year(int a, int b, int year); //function prototype……..int main( ){ struct book bkC;

…………….avg_year=modify_year(aa, bb, bkC.year); //function call…….

}

Page 17: Ekt120 Lecture10 Structures

17

Structures as Function Parameters

It is possible to pass structure variables using pointersint main(){ struct book b;

………read(&b); //function call

}void read(struct book *bk){

printf(“Enter price:”);scanf(“%f”, &bk->price);printf(“Enter numpages:”);scanf(“%d”, &bk->numpages);…….

}

Page 18: Ekt120 Lecture10 Structures

18

Sample Program#include <stdio.h>struct book { float price; int numpages; int year;};struct book read();void print(struct book, struct book);void compare(int, int);int main(){ struct book my_book =

{25.50,690,2005}; struct book she_book; she_book=read(); print(my_book , she_book); compare(my_book.year,

she_book.year); return 0;}

struct book read(){ struct book her_book;

printf("Enter book price : "); scanf("%f", &her_book.price); printf("Enter number of pages : "); scanf("%d", &her_book.numpages); printf("Enter year published : "); scanf("%d", &her_book.year); return(her_book);}void print(struct book my_book, struct book

her_book){ printf("My book :\n"); printf("%.2f\t%d\t%d\n", my_book.price,

my_book.numpages, my_book.year); printf("Her book :\n"); printf("%.2f\t%d\t%d\n", her_book.price,

her_book.numpages, her_book.year);}void compare(int my_year, int she_year){ if(my_year > she_year) printf("My book is the latest publication\

n"); else printf("Her book is the latest publication\

n");}

Page 19: Ekt120 Lecture10 Structures

19

An Array of Structures Suppose a company has 50 full-time employees.

struct employeeType {

char firstName[20]; char lastName[20]; int personID; char deptID[10]; double yearlySalary; double monthlySalary double yearToDatePaid; double monthlyBonus;

}; struct employeeType employees[50];

Page 20: Ekt120 Lecture10 Structures

20

How it looks like

Page 21: Ekt120 Lecture10 Structures

21

How to access??int counter;

for(counter = 0; counter < 50; counter++){

scanf(“%s %s %d %s %lf”, &employees[counter].firstName, &employees[counter].lastName,&employees[counter].personID,&employees[counter].deptID,&employees[counter].yearlySalary);

employees[counter].monthlySalary = employees[counter].yearlySalary/12; employees[counter].yearToDatePaid = 0.0; employees[counter].monthlyBonus = 0.0;

}

Page 22: Ekt120 Lecture10 Structures

22

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 payCheck; //variable to calculate the paycheck for(counter = 0; counter < 50; counter++) {

printf(“%s %s”, employees[counter].firstName, employees[counter].lastName); payCheck = employees[counter].monthlySalary +

employees[counter].monthlyBonus;employees[counter].yearToDatePaid =

employees[counter].yearToDatePaid + payCheck; }

Page 23: Ekt120 Lecture10 Structures

23

Arrays in Structures Example

const arraySize = 5; struct listType {

int listElem[arraySize]; //array containing the list

int listLength; //length of the list };

struct listType list;

Page 24: Ekt120 Lecture10 Structures

24

How it looks like struct listType

listElem

listLength

Page 25: Ekt120 Lecture10 Structures

25

Structure Within a Structurestruct nameType {

string first; string middle; string last;

}; struct addressType {

string address1; string address2; string city; string state; string zip;

}; struct dateType{

string month;string day; string year;

};

struct contactType {

string phone; string cellphone; string fax; string pager; string email;

}; struct employeeType {

struct nameType name; string emplID; struct addressType address; struct dateType hiredate; struct dateType quitdate; struct contactType contact; string deptID; double salary;

};

Page 26: Ekt120 Lecture10 Structures

26

Structure Within a Structure : How to access??//variable declaration struct employeeType newEmployee; //declare 100 employees' records struct employeeType employees[100];

newEmployee.salary = 45678.00; newEmployee.name.first = "Mary"; newEmployee.name.middle = "Beth"; newEmployee.name.last = "Simmons";

Page 27: Ekt120 Lecture10 Structures

27

Structure Within a Structure : How to access??

The statementscanf(“%s”, &newEmployee.name.first); reads and stores a string into newEmployee.name.first.

The statement newEmployee.salary = newEmployee.salary * 1.05; updates the salary of newEmployee