Structures and Unions.

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

C Language.
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
Array, Structure and Union
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Computer Programming II
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Arrays Chapter 7.
LINKED LISTS.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Structures, Unions, Enumerations
EGR 2261 Unit 10 Two-dimensional Arrays
An Introduction to Programming with C++ Sixth Edition
CS1010 Programming Methodology
Chapter 10-1: Structure.
Pointers, Enum, and Structures
Objectives Identify the built-in data types in C++
Program to search an element of array using linear search.
TMF1414 Introduction to Programming
Programming Structures.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Visit for more Learning Resources
CS150 Introduction to Computer Science 1
Module 2 Arrays and strings – example programs.
JavaScript: Functions.
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
Arrays, For loop While loop Do while loop
S. Kiran, PGT (CS) KV, Malleswaram
Arrays An Array is an ordered collection of variables
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Derived types.
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Chapter 7: User-Defined Functions II
Programming Logic and Design Fifth Edition, Comprehensive
By Yogesh Neopaney Assistant Professor Department of Computer Science
Structures In C Programming By Rajanikanth B.
Data Structures & Algorithms
Lec17 Structs.
Chapter 6 Part 1.
Engineering H192 Winter 2005 Structs and Enumeration Prof. Almas Ansari Dept of Computer Science Engineering Anjuman College of Engineering & Technology..
Structures Structured Data types Data abstraction structs ---
Chapter 10 C Structures and Unions
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 11 Structure and Union Types.
Structures, Unions, and Enumerations
Presentation transcript:

Structures and Unions

Growth of Bacteria Problem Depending on the growth requirements of the bacteria and the information that the scientist hopes to gain, various types of growth media are available for culturing bacteria. Culturing means cultivating bacteria in a microbiology laboratory environment Basic concept for the MPN (Most Probable Number) method is to dilute the sample to such a degree that inocula in the tubes will sometimes (but not always) contain viable organisms. By replicates, and dilution series, this will result in a fairly accurate estimate of the most probable number of cells in the sample

Growth of Bacteria Problem Three sets of five tubes are taken. First set of five tubes of nutrient medium receives 10 ml of the sample. Second set of five tubes receives 1 ml of sample per tube, and in each of a third set of five tubes, only 0.1 ml of sample is placed. Each tube in which bacterial growth is observed is recorded as a positive. Numbers for the three groups are combined to create a triplet such as 5-2-1.

Growth of Bacteria Problem 5-2-1 means that all five tubes receiving 10 ml of sample show bacterial growth, only two tubes in the 1-ml group show growth, and only one of the 0.1-ml group is positive. A microbiologist would use this combination-of-positives triplet as an index to a table like the table below to determine that the most probable number of bacteria per 100 ml.

Growth of Bacteria Problem For the combination of positives of 5-2-1, most probable number of bacteria per 100 ml of the sample is 70 and 95 percent of the samples yielding this triplet contain between 30 and 210 bacteria per 100 ml. Given the table and a combination-of-positives triplet, develop an algorithm and write a C program to return the index of combination-of-positives that component matches the target or -1 if not found.

Algorithm for Bacteria Growth Problem Read the number of entries in the table For each entry in the table read Combination of positives, MPN Index/100ml, lower and upper limit values Read the combination to be checked Search the table Print the MPN Index, lower and upper limit values

Implementation in C Have to represent each row in the table Then group the rows Each row contains four fields combination of positives - String MPN Index - Int lower limit - Int upper limit - Int and here heterogeneous data should be grouped

Structures in C Heterogeneous data can be grouped together by structures in C Syntax of a struture struct <structure name> { datatype element 1 ; datatype element 2 ; datatype element 3 ; ...... } ;

Example for Structures struct book { char name ; float price ; int pages ; } ; struct book b1, b2, b3 ; Same as

Example for Structures struct book { char name ; float price ; int pages ; } b1, b2, b3 ; Same as

Anonymous Structures – Structures without name struct { char name ; float price ; int pages ; } b1, b2, b3 ;

Initialization of Structures struct book { char name[10] ; float price ; int pages ; } ; struct book b1 = { "Basic", 130.00, 550 } ; struct book b2 = { "Physics", 150.80, 800 } ;

Accessing Structure Elements To refer pages, b1.pages refer to price we would use, b1.price

Values of One Structure Variable to Another struct employee { char name[10] ; int age ; float salary ; } ; struct employee e1 = { "Sanjay", 30, 5500.50 } ; struct employee e2, e3 ;

Values of One Structure Variable to Another /* piece-meal copying */ strcpy ( e2.name, e1.name ) ; e2.age = e1.age ; e2.salary = e1.salary ;

Values of One Structure Variable to Another /* copying all elements at one go */ e3 = e2 ; printf ( "\n%s %d %f", e1.name, e1.age, e1.salary ) ; printf ( "\n%s %d %f", e2.name, e2.age, e2.salary ) ; printf ( "\n%s %d %f", e3.name, e3.age, e3.salary ) ; }

How Structure Elements are Stored struct book { char name ; float price ; int pages ; } ; struct book b1 = { 'B', 130.00, 550 } ;

Array of Structures struct book { char name ; float price ; int pages ; } ; struct book b[100] ; for ( i = 0 ; i <= 99 ; i++ ) printf ( "\n Enter name, price and pages " ) ; scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ; }

typedef in C typedef is a keyword used in C language to assign alternative names to existing types. Its mostly used with user defined data types, when names of data types get slightly complicated. Following is the general syntax for using typedef: typedef existing_name alias_name Example typedef unsigned long ulong; ulong i, j ;

typedef in C typedef struct { type member1; type member2; } type_name ; Useful in structures – keyword struct need not used everywhere if it is typedef-ed.

Passing to Functions Default is passed by value What about array in a structure? Passed by value – Only... But array of structures is passed by reference

Declaration of Structure and Functions

Main function of Bacteria Program

Function to Read and Search Table

Surprise Gift Problem Details of employees (emp ID, name, joining date and mobile number) of a company is stored and maintained by the company’s IT department. On his birthday, the GM of the company wants to give a surprise gift of Rs.5000 for his employees who joined before 01/01/2010 and whose employee id is divisible by 5. Develop an algorithm and write a C program to display the name of the employees who are eligible to receive the gift and their mobile number

Algorithm for Surprise Gift Problem Read the number of employees Read the details of the employees Traverse the employee records Check if emp id is divisible by 5 If it is divisible then check if the employee has joined the company before 01/01/2010 Display employee names and mobile numbers of employees if condition satisfied

Implementation in C We have to store employee id, name, date of joining and mobile number Date of joining has to be stored as date an user defined data type So we have to include a structure variable as member of structure

Nested Structures in C A structure variable can be placed inside another structure

Nested Structures in C A structure variable can be placed inside another structure

Identity Card for Railway Reservation In railway reservation system, identity of the person is got by either aardhar card number or voter id. Given Aardhar card number as a number and voter id as a string. Write a C program to store the details of thousands of people. It is important to store such that less memory is wasted.

Using Structures? Structure can be used to store the details but the memory occupied will be sum of memory required for both the fields. A large wastage when the program is used to store details of many people Since either one of the detail is going to be stored, it is sufficient to allocate memory for the largest member Supported in C as Unions!

Union in C Special data type available in C that allows to store different data types in the same memory location. Union may have many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.

Syntax of Union union [union tag] { member definition; ... } [one or more union variables];

Space Occupied by Union

Features of Union Similar to structures Can have array Can be nested inside structures Structures can be nested within union