user defined data types - structures in c chapter 4

18
User Defined Data Types - Structures in C CHAPTER 4

Upload: randell-waters

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: User Defined Data Types - Structures in C CHAPTER 4

User Defined Data Types - Structures in C

CHAPTER 4

Page 2: User Defined Data Types - Structures in C CHAPTER 4

C.092

Structures» An ARRAY allow the programmer to define variables that associated several data items of the same kind.

» A STRUCTURE allow the programmer to define variables that associated several data items of different kind.

» The STRUCTURE in C is similar to the RECORD in languages such as Pascal (Delphi), COBOL.

» Each student Identity structure (record) is containing some members (fields) like, “student_no”, “name”, “birth_date”, …)

Page 3: User Defined Data Types - Structures in C CHAPTER 4

C.093

Defining a Structure» A structure is defined in terms of members (fields).

struct tag { member 1; member 2; … member n;

};

» struct is a key word.» tag is a name that identify the structures having this composition.» member 1, … , member n are individual member declarations.» Members can be ordinary variables, pointers, arrays or other structures.» Member names within the particular structure must be distinct from one another.

Page 4: User Defined Data Types - Structures in C CHAPTER 4

C.094

Defining a Structure» Member names can be same as the name of a variable defined outside of the structure, or with the name of a member defined under a different structure (not preferred).

» Once the composition of a structure has been defined, individual structure type variables can be declared as follows:

struct tag variable 1, variable 2, … , variable n;

» Where variable 1, …, variable n are variables that are declared in struct tag data type.

Page 5: User Defined Data Types - Structures in C CHAPTER 4

C.095

Defining a Structure» Example :struct account {

int acct_no; char acct_type;

char acct_name[81];float balance;

};struct account oldcustomer, newcustomer;

The oldcustomer and the newcustomer are variables of type account. (They are called structure-type variables).

» It is possible to combine the declaration of the structure composition with that of the structure-variables.

struct account { int acct_no; char acct_type;

char acct_name[81];float balance;

} oldcustomer, newcustomer;

Page 6: User Defined Data Types - Structures in C CHAPTER 4

C.096

Defining a Structure» A structure may also be defined as the member of another structure.

»In this case the declaration of the embedded structure must come before the declaration of the container structure.»e.g :

struct date { int day; int month;

int year; };struct account {

int acct_no; char acct_type;

char acct_name[81];float balance;struct date lastpayment;

} oldcustomer, newcustomer;

Page 7: User Defined Data Types - Structures in C CHAPTER 4

C.097

Defining a Structure» On the following example, the structure account is containing another structure date as one of its member.

»e.g :struct date {

int day, month, year; };struct account {

int acct_no; char acct_type;

char acct_name[81];float balance;struct date firstpayment;struct date lastpayment;

} oldcustomer, newcustomer;

Page 8: User Defined Data Types - Structures in C CHAPTER 4

C.098

Initializing a Structurestruct date {

int day, month, year; };struct account {

int acct_no; char acct_type;

char acct_name[81];float balance;struct date lastpayment;

} ; struct account customer = {12345, ‘R’, “Salaries”, 500.3, 28, 5, 2002};

» customer is a structure variable of type account.» Assignments:acct_no = 12345, acct_type = ‘R’, acct_name = “Salaries”balance = 500,3 lastpayment (day = 28, month = 5, year = 2002)

Page 9: User Defined Data Types - Structures in C CHAPTER 4

C.099

Array of Structure»It is possible to define an array of structures.

struct date { int day, month, year;

};struct account {

int acct_no; char acct_type;

char acct_name[81];float balance;struct date lastpayment;

} customer[100];

» In this example, the customer is declared as an array of struct account with 100 elements.

Page 10: User Defined Data Types - Structures in C CHAPTER 4

C.0910

Array of Structure»An array of structures can be initialized just as any othe array can be.

struct date { char name[21]; int day, month, year;

};struct date birthdate[3] = { “Ali”, 20, 7, 1974,

“Ayse”, 18, 1, 1982, “Fatma”, 8, 12, 1983 };

or

struct date birthdate[3] = { {“Ali”, 20, 7, 1974}, {“Ayse”, 18, 1, 1982}, {“Fatma”, 8, 12, 1983} };

Page 11: User Defined Data Types - Structures in C CHAPTER 4

C.0911

Processing a Structure»The members of the structures must be accessed and processed separately.

variable.member

»variable : name of the structure-type variable.

»member : The name of the member within the structure.

»The period (.) is an operator of highest priority and has left-to-right associatively.

Page 12: User Defined Data Types - Structures in C CHAPTER 4

C.0912

Processing a Structuree.g:struct date { int day, month, year;

};struct account {

int acct_no; char acct_type;

char acct_name[81];float balance;struct date lastpayment;

} customer;…

customer.acct_no = 1221; // sets cursomer’s account no to 1121 customer.balance = 0; // sets cursomer’s balance to 0 printf(“%s”,customer.acct_name); // displays the cursomer’s account name ++customer.balance; // Increments the customer’s balance by one

Page 13: User Defined Data Types - Structures in C CHAPTER 4

C.0913

Processing a Structuree.g (continuing) : scanf(“%d”,&customer.lastpayment.year);

// reading the cursomer’s last payment date year

printf(“Last Payment : %2d-%2d-%d “, customer.lastpayment.day, customer.lastpayment.month, customer.lastpayment.year);

// displays the last payment date of the customer

printf(“%c”,customer.acct_name[2]);// displays the 3rd character of the cursomer’s account name

customer.acct_type=‘I’; // Sets the account type of the customer to ‘I’.

total + = customer.balance;// Adds the balance of the customer to the variable “total”

Page 14: User Defined Data Types - Structures in C CHAPTER 4

C.0914

Processing a Structuree.g : struct date { int day, month, year; };struct account { int acct_no; char acct_type;

char acct_name[81];float balance;struct date lastpayment;

} customer[100];…

customer[13].balance=0; // Sets the balance of the 14th customer to 0.++customer[5].lastpayment.day;

// Incrementing the last payment date by one day of the 6th customer.

Page 15: User Defined Data Types - Structures in C CHAPTER 4

C.0915

ExampleProblem: Write a program including structures to read mt, final and quiz scores of the class with 25 students and will display the score list of the class with total grade of each student and the average of the class.Note that, each student has to have a record with the following fields in the class: std_no (Student Number) std_name (Student Name)

mt (Midterm Score) final (Final Score)quiz (Quiz Score)grd (Total Grade)

The weights of the scores are given as:30 % Midterm, 50% Final, and 20% Quiz

Page 16: User Defined Data Types - Structures in C CHAPTER 4

C.0916

Example#include <stdio.h> 1.#include <conio.h>struct student { char stdno[7]; char name[21]; int mt, final, quiz; float grd; };

void main(){ struct student c213[2]; int totscores[3]={0,0,0}, i; float totgrd=0; char blank[2];

// Reading Student Information 2. for (i=0; i< 2; i++) { clrscr(); printf("\n%4d. Student Data Entry\n\n", i+1); printf("\n Student No :"); gets(c213[i].stdno); printf("\n Student Name :"); gets(c213[i].name); printf("\n Midterm Score:"); scanf("%d",&c213[i].mt); printf("\n Final Score:"); scanf("%d",&c213[i].final); printf("\n Quiz Score:"); scanf("%d",&c213[i].quiz); gets(blank);

Page 17: User Defined Data Types - Structures in C CHAPTER 4

C.0917

Examplec213[i].grd = 0.30*c213[i].mt + 0.50*c213[i].final 3. + 0.20*c213[i].quiz; totscores[0]+=c213[i].mt; // Midterm Scores Total totscores[1]+=c213[i].final; // Final Scores Total totscores[2]+=c213[i].quiz; // Quiz Scores Total totgrd+= c213[i].grd; // Grades total } // Displaying the Class Scores List clrscr(); printf("\n Class Scores List\n\n"); printf("\n STD_NO NAME MT FIN QUI GRADE"); printf("\n==================================================="); for (i=0; i< 2; i++) { printf("\n%7s %20s %3d %3d %3d %6.2f", c213[i].stdno,c213[i].name, c213[i].mt, c213[i].final, c213[i].quiz, c213[i].grd); }

Page 18: User Defined Data Types - Structures in C CHAPTER 4

C.0918

Example4.

// The Score Averages part of the Listprintf("\n===================================================");printf("\nAverages %3d %3d %3d %6.2f", totscores[0]/2, totscores[1]/2, totscores[2]/2, totgrd/2); getch();}