User Defined Data Types - Structures in C

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

1 Records C++ Structs Chapter 14 2 What to do with records?  Declaring records  Accessing records  Accessing the field of a record  What is a union?
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
Structures Spring 2013Programming and Data Structure1.
Data Types C built-in data types –char, int, float, double, int*, etc. User-defined data types: the programmer can define his/her own data types which.
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.
Advance Use of Structures CHAPTER 5. C.10 1 Using Structures with Functions » A function can return only one value back. » Some of the processes may yield.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Structure A structure is a collection of variables of different data type under one name. It is a derived data type. e.g. struct employee {int empno; char.
Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Computer Programming Control Structure
User Defined Data Types - Structures in C CHAPTER 4.
Struct Data Type in C++ What Are Structures?
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Introduction We will study how to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining.
Structures and Classes
Programmer Defined Types and Classes
Advance Use of Structures
Classes and OOP.
CS1010 Programming Methodology
Chapter 10-1: Structure.
Pointers, Enum, and Structures
C Programming Tutorial – Part I
Structured Data (Lecture 07)
Programming Languages and Paradigms
COMP205 IMPERATIVE LANGUAGES
Objectives Identify the built-in data types in C++
TMF1414 Introduction to Programming
Programming Structures.
Arrays Part-1 Armen Keshishian.
(Structures,Unions, Enumerations )
Programming Paradigms
Visit for more Learning Resources
Structures.
DATA HANDLING.
Buy book Online -
Structures Lesson xx In this module, we’ll introduce you to structures.
Learning Objectives Structures Structure types
S. Kiran, PGT (CS) KV, Malleswaram
Structures.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Structure ការណែនាំអំពី Structure
הגדרת משתנים יום שלישי 27 נובמבר 2018
Operators.
Structured Data Types array union struct class.
Chapter 1: Introduction to Data Structures(8M)
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Chapter 10 Structures and Unions
Structures In C Programming By Rajanikanth B.
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
The Pointers of Structures
Structure (i.e. struct) An structure creates a user defined data type
Multidimensional Arrays Section 6.4
Structures Structured Data types Data abstraction structs ---
CSCE 206 Lab Structured Programming in C
Arrays, Casting & User Defined Variables
Structures, Unions, and Enumerations
Week 9 - Monday CS222.
Exercise 6 – Compound Types und Kontrollfluss
Programming Fundamental
Presentation transcript:

User Defined Data Types - Structures in C CHAPTER 4

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”, …)

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.

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.

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. } oldcustomer, newcustomer;

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;

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;

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)

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.

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}

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.

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

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 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”

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

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

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);

Example c213[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);

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(); }