Presentation is loading. Please wait.

Presentation is loading. Please wait.

User Defined Data Types - Structures in C CHAPTER 4.

Similar presentations


Presentation on theme: "User Defined Data Types - Structures in C CHAPTER 4."— Presentation transcript:

1 User Defined Data Types - Structures in C CHAPTER 4

2 C.09 1 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”, …)

3 C.09 2 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.

4 C.09 3 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.

5 C.09 4 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;

6 C.09 5 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;

7 C.09 6 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;

8 C.09 7 Initializing a Structure struct 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)

9 C.09 8 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.

10 C.09 9 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} };

11 C.09 10 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.

12 C.09 11 Processing a Structure e.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

13 C.09 12 Processing a Structure e.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 3 rd 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”

14 C.09 13 Processing a Structure e.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 14 th customer to 0. ++customer[5].lastpayment.day; // Incrementing the last payment date by one day of the 6 th customer.

15 C.09 14 Example Problem: 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

16 C.09 15 Example #include 1. #include 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 Information2. 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);

17 C.09 16 Example c213[i].grd = 0.30*c213[i].mt + 0.50*c213[i].final3. + 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); }

18 C.09 17 Example 4. // The Score Averages part of the List printf("\n==================================================="); printf("\nAverages %3d %3d %3d %6.2f", totscores[0]/2, totscores[1]/2, totscores[2]/2, totgrd/2); getch(); }


Download ppt "User Defined Data Types - Structures in C CHAPTER 4."

Similar presentations


Ads by Google