30
Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Embed Size (px)

Citation preview

Page 1: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

STRUCTURESCHAPTER 9 in A

Book in C

Page 2: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures in C

• A structure is – a convenient way of grouping several

pieces of related information together– a collection of variables under a single

name

Examples :

real number && imaginary number complex number ( 3+5i )

height && width && length rectangular prism

Page 3: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures in C

• The variables in structures can be of different types, and each has a name which is used to select it from the structure

Example : ID (integer) && Name (char array) A Student record

• A structure is a new named type that may involve several different typed variables

Page 4: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Defining Structures 1

struct complex_number{

int real_part;int imaginary_part;

};

“complex_number”

is the tag name

“struct complex_number” is the new type

Page 5: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Defining Structures 2/* DEFINITION OF RECTANGULAR

PRISM */struct rectangular_prism{ int height;

int width;int length;};

// name of new type??

/* DEFINITION OF STUDENT RECORD */

struct student_record{ int ID;

char name[100]; };

Page 6: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : Creating objects

struct complex_number{

int real_part;int imaginary_part; };

// Below the definition of structure you can create// objects from it “s1” and “s2”

struct complex_number s1;

struct complex_number s2;s2

real_part imaginary_part

s1

real_part imaginary_part

Page 7: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : Creating objects

struct rectangular_prism{ int height;

int width;int length; };

// Below the definition of structure you can create

// objects from it “obj”

struct rectangular_prism obj; obj

height lengthwidth

Page 8: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : Creating static

arrays of objects struct student_record{ int ID;

char name[100]; };

// Creates an array of student records “group”

struct student_record group[4];group

ID name

ID name

ID name

ID name

group[0]

group[1]

group[2]

group[3]

Page 9: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : Creating dynamic arrays of objects

struct rec{ int ID;

char name[100]; };

// Creating a dynamic array // of student records “group”

struct rec * group; // DECLARES POINTER

group = ( struct rec * ) malloc ( sizeof (struct rec) * 4 );

size ofobjects in

array

number of objects in

array(array size)

Page 10: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : Accessing members of

structures struct rectangular_prism{ int height;

int width;int length; };

// Create an object from the structure defined above “obj”

struct rectangular_prism obj;

// Members of the objects can be accessed by putting a dot

// following the object name

obj.height=10;obj.width=15;obj.length=40;

obj

height=10

length=40

width=15

Page 11: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : Accessing members of

structures struct rectangular_prism{ int height;

int width;int length; };

struct rectangular_prism obj;

// Create a pointer to point object “obj”struct rectangular_prism *p = &obj;

(*p).height=10 // or obj.height or p->height=10

p->width=15;p->length=40;

obj == *p

height=10

length=40

width=15

Page 12: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : Accessing members of structures

// Defines structurestruct student_record{ int ID; char

name[100]; };

// Creates an array of student records “group”

struct student_record group[2];

group[0].ID=200710;strcpy(group[0].name, “doddy”);

group[1].ID=200711;strcpy(group[1].name,group[0].name);

group

ID=200710

name= “doddy”

ID=200711

name =??

group[0]

group[1]

Page 13: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : DECLARATION ALTERNATIVES

Declaration 1 :

struct record { int ID; char * name; char grade; };struct record s1;struct record s2;struct record s3;

Declaration 2 :

struct record { int ID; char * name; char grade; } s1, s2;struct record s3;

Page 14: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : DECLARATION ALTERNATIVES

Declaration 1 :

struct record { int ID; char * name; char grade; };

struct record s1;struct record s2;

Declaration 3 :

struct { int ID; char * name; char grade; } s1, s2;

/* no tag name *//* no permission to declare other variables of this type */

Page 15: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures : DECLARATION ALTERNATIVES

Declaration 4 :

struct record { int ID; char * name; char grade; };typedef struct record rec;

rec s1;struct record s2;

Declaration 5 : /* high degree of modularity and portability */

typedef struct { int ID; char * name; char grade; } rec;rec s1; rec s2;

Page 16: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Initialization of Structure Objects

1. struct names { char name[10];int length;int weigth;} man[3]= { “Tom”, 180,

65, “George”, 170, 68,

“Bob”, 190, 100 };

2. struct names woman[2]={{“Mary”, 170, 55}, {“Sue”, 160,67}};

3. struct names your;

your. name=“Jane”;your.length=160;your.weigth=50;

Page 17: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures in Structures#include<stdio.h>

struct physical_info { int length;int weigth; } ;

struct record {int salary;int working_hour;struct physical_info man; } ;

main(){ struct record s1;

s1.salary=10000;s1.working_hour= 6;

s1.man.length=180;s1.man.weigth=78; }

Page 18: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 1 on Structures

• Declare a structure to represent fractions• Create 2 objects of fractions• Ask user to fill the objects • Calculate and print out the multiplication value

Fraction x / y x over y (3/7, 8/5) Need 2 numbers as numerator and

denominator User has to give 2 numbers for each object Multiplication rule a/b * c/d = (a*c) / (b*d)

Page 19: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 1 on Structures struct fraction{ int n; // an integer to represent numerator

int d; // an integer to represent denominator };

main(){ struct fraction obj1, obj2; // Create input objects

struct fraction result; // Create an object to store the result of // multiplication

printf(“please enter values for fractions”);scanf(“%d%d”, &obj1.n , &obj1.d);scanf(“%d%d”, &obj2.n , &obj2.d);

result.n = obj1.n * obj2.n;result.d = obj1. d * obj2. d;

printf(“result is %d / %d”, result.n, result.d); }

Page 20: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 2 on Structures• Declare a structure for a customer record

(name, surname, phone number)

• Create 5 customer objects

• Ask user to fill the objects

• Print out the information of all customers as in the following output

customer 1 : Gabriel Gray 1222222customer 2 : Claire Bennet 1234567customer 3 : Hiro Nakamura 2222222customer 4 : Nathan Petrelli 1234569 customer 5 : Niki Sanders 2333333

Page 21: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 2 on Structures struct customer{ char name[100];

char surname[100];int phone; };

main(){ struct customer heroes[5]; // Create

input objectsint i;

printf("please fill customer records");

for(i=0;i<5;i++)scanf("%s%s%d",

heroes[i].name, heroes[i].surname, &heroes[i].phone );

for(i=0;i<5;i++)printf(“customer %d : %s%s%d\n",

i+1, heroes[i].name, heroes[i].surname, heroes[i].phone);

}

Page 22: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 3 on Structures• Declare a structure for complex numbers

(real and imaginary part)

• Create 1 dynamic object (use pointers)

• Ask user to fill the object

• Print out the complex number as given below output

Please give the values for complex number : 5 6

Complex number : 5 + 6i

Page 23: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 3 on Structures struct complex{ int real;

int imaginary; };

main(){

struct complex * p; // Declare pointer for object

// Memory allocation for object

p=(struct complex *) malloc(sizeof(struct complex));

printf( " Please give the values for complex number : " );scanf( "%d%d", &(p->real), & (p->imaginary) );

printf( "Complex number : %d + %d i", p->real, p->imaginary );

}

Page 24: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Structures as Function Arguments

Call by valueCall by reference

Page 25: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

// A function to add two integersint add(int a, int b){ int result= a+b;

return result; }

struct complex { int r; int i; };

// A function to add two complex numbersstruct complex add ( struct complex a, struct

complex b ){ struct complex result;

result.r=a.r+b.r;result.i=a.i+b.i;return result; }

Exercise 1 : Define a function to add two complex numbers

Page 26: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

// A function to print an integersvoid print(int a){ printf(“%d \n”,a); }

struct complex { int r; int i; };

// A function to print a complex numberstruct complex print ( struct complex a){ printf(“%d + i %d \n”, a.r,a.i); }

Exercise 1 : Define a function to print a

complex number

Page 27: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 1 on Call by Value : Define main()

// Structure definitionstruct complex { int r; int i; } ;

// function prototypesstruct complex add ( struct complex a, struct

complex b );void print ( struct complex a);

main(){ struct complex e1={2,3};

struct complex e2 ={1,2};struct complex e3;

print(e1);print(e2);e3=add(e1, e2);print(e3); }

Page 28: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 2 on Call by Valuestruct fraction{ int n; // an integer to represent numerator

int d; // an integer to represent denominator };

struct fraction sum(struct fraction x, struct fraction y){ struct fraction result;

result.d=x.d*y.d;result.n=x.n*y.d+y.n*x.d;return result;

}

struct fraction divide(struct fraction x, struct fraction y)

{ struct fraction result;result.d=x.d*y.n;result.n=x.n*y.dreturn result;

}

Page 29: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 3 on Call by Value

struct rectangular_prism{ int height;

int width;int length; };

……. volume(……){…….}

…… area (……){…….}

Page 30: Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin

Exercise 3 on Call by Value

struct rectangular_prism{ int height;

int width;int length; };

int volume(struct rectangular_prism x){return x.height*x.width*x.length; }

int area (struct rectangular_prism x){ return 2*x.width*x.lenght +

2*x.lenght*x.height +2*x.width*x.lenght;

}