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

Similar documents
Chapter-14 STRUCTURES

Object Oriented Pragramming (22316)


STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

C++ Notes Class XI Structure

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

Unit IV & V Previous Papers 1 mark Answers

Pointer Data Type and Pointer Variables

DE70/DC56 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2014


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

Pointers, Dynamic Data, and Reference Types

IV Unit Second Part STRUCTURES

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

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

Chapter 7 - Notes User-Defined Functions II

Input And Output of C++

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

Darshan Institute of Engineering & Technology for Diploma Studies

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

2. ARRAYS What is an Array? index number subscrip 2. Write array declarations for the following: 3. What is array initialization?

CSI33 Data Structures

Lecture 3 Tao Wang 1

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Downloaded from

MEMORY ADDRESS _ REPRESENTATION OF BYTES AND ITS ADDRESSES

UNIT-2 Introduction to C++

b) Give the output of the following program: 6,70,70 2,70 210,282,59290

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

AC55/AT55 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2013

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

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

BITG 1233: STRUCTURED DATA. LECTURE 11 (Sem 2, 17/18)

Functions, Arrays & Structs

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Pointers II. Class 31

Lecture 3. The syntax for accessing a struct member is

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

JAVA GUI PROGRAMMING REVISION TOUR III

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

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

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Chapter 11: Structured Data

UNIT- 3 Introduction to C++

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Exercise1. // classes first example. #include <iostream> using namespace std; class Rectangle. int width, height; public: void set_values (int,int);

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

UNIT - V STRUCTURES AND UNIONS

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

True or False (15 Points)

Inheritance

BITG 1113: Array (Part 2) LECTURE 9

Arrays. int Data [8] [0] [1] [2] [3] [4] [5] [6] [7]

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

EAS 230 Fall 2002 Section B

Chapter 1. Principles of Object Oriented Programming

Functions, Arrays & Structs

Programming. C++ Basics

Structured Data. CIS 15 : Spring 2007

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

Programming Fundamentals (CS-302 )

True or False (12 Points)

Chapter 11: Structured Data

Functions and Recursion


CS242 COMPUTER PROGRAMMING

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

BRAIN INTERNATIONAL SCHOOL Term-II Class-XI Sub:- Computer Science Revision Sheet

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

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

COMPUTER SCIENCE (083)

Exam 3 Chapters 7 & 9

A structure is an aggregate data type which contains a fixed number of heterogeneous components.

CS2141 Software Development using C/C++ C++ Basics

Chapter 6 - Notes User-Defined Functions I

Multi-Dimensional arrays

ASSIGNMENT CLASS-11 COMPUTER SCIENCE [C++]

Homework Assignment #2 (revised)

LECTURE 11 STRUCTURED DATA

CLASSES AND OBJECT CHAPTER 04 CLASS XII

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

CSCE Practice Midterm. Data Types

CSCE Practice Midterm. Data Types

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

#include<iostream.h> { for(int L=1;L<=N;L++) cout<<m*l; cout<<endl; } void main()

KUWAIT SAHODAYA EXAMINATION FIRST TERM SUBJECT : COMPUTER SCIENCE (083) : CLASS - XII SET - 3 Time : 3 Hours

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Computer Programming

Technical Questions. Q 1) What are the key features in C programming language?

CHAPTER-6 GETTING STARTED WITH C++

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Advanced C Programming and Introduction to Data Structures

True or False (15 Points)

Transcription:

STRUCTURE IN C++ 1

A Structure is a collection of variables of different data types under one name. A structure defines a new user defined data type for your current program using which items of different data types can be can be processed as a single unit. Difference between an Array and a Structure The Array contains elements of a single data type (homogeneous elements) where as a Structure can contain elements of different data types (heterogeneous members). The elements of the Array are contiguous in memory whereas the elements of a Structure may not be contiguous. 2

Syntax to define a Structure: For Example: struct structurename datatype membername1; datatype membername2;... ; struct Student int rollno; char name[20]; float marks; ; Definition of a structure ( i. e. a new user defined data type ) does not allocate any memory. Memory is allocated only when we create variables of this new data type. ( For Eg. Bytes consumed by each variable of Student type=26 Bytes) 3

Student S1, S2 ; // variables of Student type S1.rollno = 101; strcpy ( S1.name, aman ); S1.marks = 95; cout << Enter details for Student 2 ; cout << Enter Student Rollno ; cin >> S2.rollno ; cout << Enter Student Name ; gets ( S2.name ) ; cout << Enter Student Marks ; cin >> S2.marks ; 101 aman 95 S1 cout << S1.rollno << endl; cout << S1.name << endl; cout << S1.marks << endl; 4

Structure variables can also be declared by clubbing Structure definition and Variable declaration together struct Student int rollno; char name[20]; float marks; S1, S2 ; //global variables of Student type The Structure name/tag can be omitted from the above definition, but the drawback is no more variables of its type can be created later on. struct int rollno; char name[20]; float marks; S3, S4 ; // only variables of this structure type 5

Initialization of structure members can be done at the time of creation of structure variable struct date int day; int month; int year; ; date d1= 31,12,2016; date d2= 15,11,2017; // initializing d1 // initializing d2 31 12 2016 d1 15 11 2017 d2 6

We can assign a variable of a structure as a whole to another variable of the same structure type where in each member of the structure variable receives the corresponding members value from the assigned variable struct X int a; float b; S1; struct Y int c; float d; S2, S3; 2 4 S1 3 6 S2 3 6 S3 cin>>s1.a>>s1.b; cin>>s2.c>>s2.d; S1=S2; S3=S2; // Error since different structure types // valid member by member assignment 7

When any member of the structure is a structure variable itself, then it is a nested structure. struct date 101 int day; int month; ravi int year; 55.5 ; struct Student int rollno; char name[20]; 10 float marks; date dob; // variable of date data type ; 08 2001 Student stu1 ; cin >> stu1.rollno ; gets ( stu1.name); stu2 cin >> stu1.marks; cin >> stu1.dob.day >> stu1.dob.month >> stu1.dob.year ; Student stu2= 101, ravi, 55.5, 10, 08, 2001 ; // initialization of stu2 While Initialization in nested structures, internal braces are optional and are ignored by compiler. 8

struct Student int rollno; char name[20]; int marks[5]; ; Student stu ; cin >> stu.rollno ; gets ( stu.name); for( int i=0; i<5 i++) cin >> stu.marks[i]; 101 Sumit 90 95 100 85 92 S.marks[0] S.marks[1] S.marks[2] S.marks[3] S.marks[4] stu 9

Structure can be passed to functions by value or by reference. When passed by value the called function creates its own variables of the specified structure type and assigns the actual variables values member by member to it. When passed by reference the formal structure variables are just an alias name of the actual variables. /* Program to add two times given in hours and minutes using functions */ struct time int hh; int mm; ; time Add(time,time); //fn prototype time t1, t2, t3; cout<<"enter First Time " <<endl; cout<<" Hours "; cin>>t1.hh; cout<<" Minutes "; cin>>t1.mm; cout<<"enter Second Time "<<endl; cout<<" Hours " ; cin>>t2.hh; cout<<" Minutes "; cin>>t2.mm; t3 = Add( t1, t2 ); //fn call cout<<"total Time is " <<t3.hh<<" Hours : " <<t3.mm<<" Minutes"<<endl; time Add( time ft, time st) time tt; tt.hh = ft.hh + st.hh + ( ft.mm + st.mm )/ 60; tt.mm = ( ft.mm + st.mm ) % 60; return tt; 10

#include<iostream.h> struct distance int feet; int inch; ; struct volume distance length; distance breadth; distance height; ; float cubicfeet( volume v) float l= v.length.feet + (float)v.length.inch/12; float b= v.breadth.feet + (float)v.breadth.inch/12; float h= v.height.feet + (float)v.height.inch/12; return l*b*h; volume V; cout<<"enter Length " <<endl; cout<<" Feet "; cin>>v.length.feet; cout<<" Inches "; cin>>v.length.inch; cout<<"enter Breadth " <<endl; cout<<" Feet "; cin>>v.breadth.feet; cout<<" Inches "; cin>>v.breadth.inch; cout<<"enter Height " <<endl; cout<<" Feet "; cin>>v.height.feet; cout<<" Inches "; cin>>v.height.inch; float ans= cubicfeet(v); cout<< "volume is "<<ans<< " cubic feet"; 11

/* Program to display the Average marks of each student */ #define MAX 3 struct student long rollno; char name[20]; float marks[5]; ; void Average(student[]); //prototype student S[MAX]; for ( int i=0 ; i < MAX ; i++ ) cout<<"student "<<(i+1)<<endl; cout<<"enter rollno :"; cin>>s[i].rollno; cout<<"enter name :"; gets(s[i].name); for(int j=0; j<5 ;j++) cout<<"enter marks" <<(j+1) <<" :"; cin>>s[i].marks[j]; Average(S); void Average( student st[]) for ( int i=0 ; i < MAX ; i++ ) cout<<"--------------------"<<endl; cout<<"student "<<(i+1)<<endl; cout<<"rollno :"<<st[i].rollno<<endl; cout<<"name :"<<st[i].name<<endl; int sum=0; for(int j=0; j<5 ;j++) sum+=st[i].marks[j]; cout<<"average Marks :"<<(float)sum/5<<endl; 12