Chapter-14 STRUCTURES

Similar documents
Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

IV Unit Second Part STRUCTURES

Syntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Chapter-8 DATA TYPES. Introduction. Variable:

UNIT - V STRUCTURES AND UNIONS

Chapter-13 USER DEFINED FUNCTIONS

3.3 Structures. Department of CSE

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example

Programming. C++ Basics

Subject: Fundamental of Computer Programming 2068

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Creating a C++ Program

Example : Define a structure of student having fields Roll No, Name, Address and fees paid ( Make appropriate assumption about datatypes) Method -1

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 11: Records (structs)

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

CSI33 Data Structures

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Object Oriented Pragramming (22316)

C++ Structures Programming Workshop 2 (CSCI 1061U)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

DELHI PUBLIC SCHOOL TAPI

Structures in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Lecture 3 Tao Wang 1

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

CS100 : Computer Programming

Data Structures using OOP C++ Lecture 3

Unit 3 Decision making, Looping and Arrays

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

6.096 Introduction to C++

ALQUDS University Department of Computer Engineering

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Understanding main() function Input/Output Streams

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

CHAPTER 4 Structures

UNIT-2 Introduction to C++

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

REPETITION CONTROL STRUCTURE LOGO

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

BLUE PRINT SUBJECT: - COMPUTER SCIENCE(083) CLASS-XI. Unit Wise Marks

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Tin học cơ sở 4$ Structures!

CSc Introduction to Computing

Exercises 1. class member { int membernum = 25; float memberpay; public void Input(cin >> membernum >> memberpay); void Output; }

The C++ Language. Arizona State University 1

C++ Notes Class XI Structure

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Structures Unions and Enumerated Datatypes 224

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Guide for The C Programming Language Chapter 4

Padasalai.Net s Model Question Paper

DC54 DATA STRUCTURES DEC 2014

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES

F4104 ALGORITHM & DATA STRUCTURE

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

First of all, it is a variable, just like other variables you studied

Lab # 02. Basic Elements of C++ _ Part1

Short Notes of CS201

Lecture 3. The syntax for accessing a struct member is

Structures. Lecture 15 COP 3014 Spring April 16, 2018

structs // variable declarations

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

H.O.#2 Fall 2015 Gary Chan. Overview of C++ Programming

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

AMCAT Automata Coding Sample Questions And Answers

CS201 - Introduction to Programming Glossary By

UNIT- 3 Introduction to C++

Computer Programming : C++

Structs. Comp Sci 1570 Introduction to C++ Introduction. Aggregate data. Example. General syntax Object initialization Initialization and access

Multi-Dimensional arrays

2 nd Week Lecture Notes

CS201 Some Important Definitions

Variables. Data Types.

C++ 8. Constructors and Destructors

Objectives. In this chapter, you will:

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

PROGRAMMING IN C++ COURSE CONTENT

Darshan Institute of Engineering & Technology for Diploma Studies

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

ARRAYS. Part II Answers to all the questions (2 Marks):

Single Dimension Arrays

Write a C program using arrays and structure

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

CLASSES AND OBJECT CHAPTER 04 CLASS XII

Fundamental Concepts and Definitions

Transcription:

Chapter-14 STRUCTURES Introduction: We have seen variables of simple data types, such as float, char, and int. Variables of such types represent one item of information: a height, an amount, a count, and so on. But just as groceries are students into colleges and words into sentences, it is often convenient to organize simple variables into more complex entities. The C++ construction called the structure is one way to do this. A structure is a collection of simple variables. The variables in a structure can be of same or different types: some can be int, some can be float and so on. The data items in a structure are called the members of the structure. For C++ programmers, structures are one of the two important building blocks in the understanding of objects and classes. The syntax of a structure is almost identical to that of a class. A structure is a collection of data, while a class is a collection of both data and function Defining a structure The process of defining a structure includes giving the structure a name called the tag and telling the compiler the name and the data type of each piece of data you want to be included in a structure. Syntax: Struct structure-name datatype member-name-1; datatype member-name-2;.. datatype member-name-n; o The keyword struct introduces the structure definition. o Next comes the structure name or tag, which is part. o The declarations of the structures members are enclosed in braces. o The list of all structure members is called template. o A semicolon follows the closing braces, terminating the entire structure. 1 P a g e

Example: A structure definition to hold employee information. struct employee int idno; char designation[10]; float salary; This structure is named employee. It contains four members: an integer named idno, a character array to hold 15 characters for a name, another character array to hold 10 characters for designation and float named salary. The structure requires an area in memory, which is 31 bytes long. The figure given below shows the allocation of memory for the individual elements. Idno Name Designation Float Total 31 2 bytes 15 bytes 10 bytes 4 bytes Bytes Declaring a structure or defining a structure variable: A structure can be declared (or a structure variable can be defined) as we defined a basic built-in data type. The general form is structure-name variable; Example 1: student old_student, new_student; old_student and new_student are the two structure variables declared as we declare built-in data types. Example 2: employee emp1, emp2; emp1and emp2 are the two structure variables declared as belong to the user-defined data type employee. We can combine the definition of the structure and definition of structure variables. Example: struct employee int idno char designation[10]; float salary; } emp1, emp2; 2 P a g e

Manager idno name designation salary Note: Assistant idno name designation salary The definition of a structure does not allocate memory space. The definition just defines the blueprint for the creation of variables. A semicolon should follow the last brackets of the structure. The definition of the structure should be come either before the main function or before any other variables are declared. The declaration of the structure variables always comes after the definition. The declaration of the structure creates space to store the values for the variables. Accessing the elements of a structure After the structure variables are defined, we should know how the member of a structure variable is accessed. A member of a structure is always part of a structure and it is not possible to refer the member directly. The member of a structure can be accessed using dot operator. The syntax of dot operator is as follows: structure_variable-name.member_name The structure member is written in three parts: o The name of the structure variable (part1); o The dot operator, which consists of a period (.) o The member name (part3). Note: The real name of the dot operator is member access operator. Example 1: To access the name of the member emp1 of the tag employee, we write emp1.name; Example 2: To access the salary of the member emp2 of the tag employee, we write emp2.salary 3 P a g e

Sample Program: To input and display the information of a student #include<iostream.h> #include<iomanip.h> #include<string.h> void main ( ) struct student char comb[4]; float perc; } st; cout<< Enter the register number of the student: ; cin>>st.regno; cout<< Enter the name of the student: ; cin>>st.name; cout<< Enter the combination of the student: ; cin>>st.comb; cout<< Enter the percentage of the student: ; cin>>st.perc; cout<< Register No: <<st.regno<<endl; cout<< Name: <<st.name<<endl; cout<< Combination: <<st.comb<<endl; cout<< Percentage: <<st.perc<<endl; getch(); } Sample run: Enter the register number of the student: 1234 Enter the name of the student: Nalina M A Enter the combination of the student: CEBA Enter the percentage of the student: 90.05 Example: Consider the assignments: std.regno=12345 Register no: 1234 Name: Nalina M A Combination: CEBA Percentage:90.05 std.name[]= empress college ; std.perc=79.54; 4 P a g e

Initializing a structure: Like any other data type, it is also possible to initialize the structure members of a structure variable. Structures members are initialized when they are declared. Example: A program may contain the following initialization of the structure. employee emp1 = 1234, Lakshmi, Manager, 10500.00 This initialization initializes idno field to 1234, the name field to Lakshmi, the designation field to Manager and the salary field to 10500.00. A structure variable can be assigned to another structure variable. Example: std2 = std1; Here, std1 and std2 are the structure variables of the structure std. The value of each member of std1 is assigned to the corresponding member of std2. Since a large structure can have dozens of members, such an assignment statement can require the computer to do a considerable amount of work. Note that one structure variable can be assigned to another only when they are of the same structure type. If you try to assign a variable of one structure type to variable of another type, the compiler will complain. Nested structures: A structure can be defined as a member of another structure. Such structure where one structure is embedded within another structure is called as a nested structure. During the declaration of nested structure, the definition of the embedded structure must appear before the definition of outer structure. Example: consider the definition of the following structure. struct distance struct room int feet; distance length; float inches; distance breadth; 5 P a g e

The structure room contains another structure distance as one of its members. Whenever nested structures are used, the definition of the internal structure should precede the definition of the outer structure. In the above example, the definition of the structure distance precedes the definition of the structure room. Example: consider the definition of structure student. struct date struct student int dayno; char month[10]; date doa, dob; int year; int marks; }std; To access the field regno of the structure std, we write std.regno. To access the day number of the date of admission of the structure std, we write std.doa.dayno. To access the year of date of birth of the student std, we write std.dob.year. Array of structures: A single variable that represents a structure creates memory space to store one set of information. If we want to create memory space for more instances then we may have to increases the number of variables or use the facility called arrays. An array of structure is an array in which each element of the array is a structure. Thus it is a collection of structures put together as an array. For example, let us consider that there are 10 students studying in a college. We have defined the structure so that it could maintain information about students of the college. If we have to store data of 10 students we need to use arrays rather than a single variable. When create an array of the structure, we are creating an array of the entire template. This can be done as follows. struct student char name [15]; char comb; float fees; } s [10]; 6 P a g e

In the declaration, s is an array of 10 elements. Each element of the array is a separate structure of type of student. The expression: o s[0].name will access the name of the 1 st student, while o s[9].fees access the fees paid by the last student s[0] s[1] s[9] regno name comb fees. Memory allocation for an array of structures An array of structures can be assigned initial values just like any other array. Remember that each element is a structure that must be assigned a corresponding set of initial values. The process of initialization is illustrated below. Example: struct info float fees; info s[ ] = 1, Lavanya, 5500.00, 2, Manasa, 6250.00, 3, Sajay, 6000.00, 4, Sahana, 5900.00, Practical Program 30: Write a program to input the register number, name and class of all the students in a class into a structure and output the data in a tabular manner with proper heading. #include<iostream.h> #include<conio.h> struct student char section[4]; 7 P a g e

void main( ) student s[50]; int i, j, n; clrscr( ); cout<< How many students? <<endl; cin>>n; for(i=0; i<n; i++) cout<< Enter the Reg No of the Student <<i+1<< : ; cin>>s[i].regno; cout<< Enter the Name of the Student <<i+1<< : ; cin>>s[i].name; cout<< Enter the Class of the Student <<i+1<< : ; cin>>s[i].section; } cout<< REG_NO \t NAME \t CLASS \t <<endl; for(i=0; i<n; i++) cout<<s[i].regno<< \t <<s[i].name<< \t << s[i].section<<endl; getch( ); } CHAPTER 14 STRUCTURES BLUE PRINT VSA (1 marks) SA (2 marks) LA (3 Marks) Essay (5 Marks) Total - - 01 Question - 03 Marks ************** 8 P a g e