Structures. 21 July 2009 Programming and Data Structure 1

Similar documents
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

What is a Structure? related data items. Examples: n Used for handling a group of logically

Structures. 5 th March Spring 2012 Programming and Data Structure 1

Programming & Data Structure

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

User Defined Data: Product Constructor

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

Object oriented programming

IV Unit Second Part STRUCTURES

Unit IV & V Previous Papers 1 mark Answers

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

AMCAT Automata Coding Sample Questions And Answers

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

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

3.3 Structures. Department of CSE

Self-referential Structures and Linked List. Programming and Data Structure 1

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Data Types and Variables in C language

Informatik I (D-ITET) Übungsstunde 12,

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

OBJECT ORIENTED PROGRAMMING

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

C Programming Review CSC 4320/6320

Pointers and Arrays 1

Pointers and File Handling

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

CSE 230 Intermediate Programming in C and C++ Functions

VARIABLES AND CONSTANTS

Lecture 3: C Programm

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Lectures 5-6: Introduction to C

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

C: How to Program. Week /Mar/05

Slides adopted from T. Ferguson Spring 2016

Pointers Part 1. Introduction

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

CS240: Programming in C

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

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

CSCI 171 Chapter Outlines

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Decision Making -Branching. Class Incharge: S. Sasirekha

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

DC54 DATA STRUCTURES DEC 2014

Chapter 2 - Introduction to C Programming

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

DS Assignment I. 1. Set a pointer by name first and last to point to the first element and last element of the list respectively.

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Functions. Systems Programming Concepts

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

PROGRAMMAZIONE I A.A. 2017/2018

For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit

COMPUTER APPLICATION

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Compiling and Running a C Program in Unix

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 02 C FUNDAMENTALS

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

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

Lecture 2: C Programm

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Computer Systems Principles. C Pointers

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Functions. Arash Rafiey. September 26, 2017

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

M.EC201 Programming language

Programming Fundamentals (CS-302 )

Lectures 5-6: Introduction to C

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

15 FUNCTIONS IN C 15.1 INTRODUCTION

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CSE2301. Arrays. Arrays. Arrays and Pointers

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Data types, variables, constants

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Fundamentals of Programming

C Language Coding Standard

BSM540 Basics of C Language

C PROGRAMMING Lecture 4. 1st semester

ASSIGNMENT BOOKLET. M.Sc.(Mathematics with Applications in Computer Science) Programming and Data Structures (1 st January, 2018 December, 2018)

Transcription:

Structures 21 July 2009 Programming and Data Structure 1

What is a Structure? It is a convenient tool for handling a group of logically related data items. Student name, roll number, and marks Real part and complex part of a complex number This is our first look at a non-trivial data structure. Helps in organizing complex data in a more meaningful way. The individual structure elements are called members. 21 July 2009 Programming and Data Structure 2

Defining a Structure The composition of a structure may be defined as: struct tag { member 1; member 2; : member m; }; struct is the required keyword. tag is the name of the structure. member 1, member 2, are individual member declarations. 21 July 2009 Programming and Data Structure 3

Contd. The individual members can be ordinary variables, pointers, arrays, or other structures. The member names within a particular structure must be distinct from one another. A member name can be the same as the name of a variable defined outside of the structure. Once a structure has been defined, individual structure-type variables can be declared as: struct tag variable_1, variable_2,, variable_n; 21 July 2009 Programming and Data Structure 4

Example A structure definition: struct student { char name[30]; int roll_number; int total_marks; char dob[10]; }; Defining structure variables: struct student a1, a2, a3; A new data-type 21 July 2009 Programming and Data Structure 5

A Compact Form It is possible to combine the declaration of the structure with that of the structure variables: struct tag { member 1; member 2; : member m; } variable_1, variable_2,, variable_n; In this form, tag is optional. 21 July 2009 Programming and Data Structure 6

Example struct student { char name[30]; int roll_number; int total_marks; char dob[10]; } a1, a2, a3; struct { char name[30]; int roll_number; int total_marks; char dob[10]; } a1, a2, a3; Equivalent declarations 21 July 2009 Programming and Data Structure 7

Processing a Structure The members of a structure are processed individually, as separate entities. A structure member can be accessed by writing variable.member where variable refers to the name of a structure-type variable, and member refers to the name of a member within the structure. Examples: a1.name, a2.name, a1.roll_number, a3.dob; 21 July 2009 Programming and Data Structure 8

Example: Complex number addition #include <stdio.h> main() { struct complex { float real; float complex; } a, b, c; Scope restricted within main() scanf ( %f %f, &a.real, &a.complex); scanf ( %f %f, &b.real, &b.complex); c.real = a.real + b.real; c.complex = a.complex + b.complex; } printf ( \n %f + %f j, c.real, c.complex); Structure definition And Variable Declaration Reading a member variable Accessing members 21 July 2009 Programming and Data Structure 9

Comparison of Structure Variables Unlike arrays, group operations can be performed with structure variables. A structure variable can be directly assigned to another structure variable of the same type. a1 = a2; All the individual members get assigned. Two structure variables can be compared for equality or inequality. if (a1 = = a2). Compare all members and return 1 if they are equal; 0 otherwise. 21 July 2009 Programming and Data Structure 10

Arrays of Structures Once a structure has been defined, we can declare an array of structures. struct student class[50]; The individual members can be accessed as: class[i].name class[5].roll_number 21 July 2009 Programming and Data Structure 11

Arrays within Structures A structure member can be an array: struct student { char name[30]; int roll_number; int marks[5]; char dob[10]; } a1, a2, a3; The array element within the structure can be accessed as: a1.marks[2] 21 July 2009 Programming and Data Structure 12

Defining data type: using typedef One may define a structure data-type with a single name. General syntax: typedef struct { member-variable1; member-variable2;. member-variablen; } tag; tag is the name of the new data-type. 21 July 2009 Programming and Data Structure 13

typedef : An example typedef struct{ float real; float imag; } _COMPLEX; _COMPLEX a,b,c; 21 July 2009 Programming and Data Structure 14

Structure Initialization Structure variables may be initialized following similar rules of an array. The values are provided within the second braces separated by commas. An example: _COMPLEX a={1.0,2.0}, b={-3.0,4.0}; a.real=1.0; a.imag=2.0; b.real=-3.0; b.imag=4.0; 21 July 2009 Programming and Data Structure 15

Parameter Passing in a Function Structure variables could be passed as parameters like any other variable. Only the values will be copied during function invokation. void swap(_complex a, _COMPLEX b) { _COMPLEX tmp; } tmp=a; a=b; b=tmp; 21 July 2009 Programming and Data Structure 16

#include <stdio.h> typedef struct{ float real; float imag; } _COMPLEX; An example program void swap(_complex a, _COMPLEX b) { _COMPLEX tmp; } tmp=a; a=b; b=tmp; 21 July 2009 Programming and Data Structure 17

Example program: contd. void print(_complex a) { printf("(%f, %f) \n",a.real,a.imag); } main() { _COMPLEX x={4.0,5.0},y={10.0,15.0}; print(x); print(y); swap(x,y); print(x); print(y); } 21 July 2009 Programming and Data Structure 18

Returning structures It is also possible to return structure values from a function. The return data type of the function should be as same as the data type of the structure itself. _COMPLEX add(_complex a, _COMPLEX b) { _COMPLEX tmp; } tmp.real=a.real+b.real; tmp.imag=a.imag+b.imag; return(tmp); Direct arithmetic operations are not possible with Structure variables. 21 July 2009 Programming and Data Structure 19