Array, Structure and Union

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
UNIONS IN C.  Union Data Type Union Data Type  Defining of Union Defining of Union  Memory Space Allocation Memory Space Allocation  Example of Union.
EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Structure.
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.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Programming with Collections Collections in Java Using Arrays Week 9.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Introduction to Computers and Programming Class 21 Arrays Professor Avi Rosenfeld.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
UNIT II. -set of homogeneous data items. Eg: int arrays can hold only integers and char arrays can only hold characters. Arrays have a type, name, and.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
ICS103 Programming in C Lecture 11: Arrays I
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Structures Declarations.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
COMP 1402 Winter 2008/Summer 2008 Tutorial #10 Enumerations, Unions, Linked Lists.
 2005 Pearson Education, Inc. All rights reserved Arrays.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 10-1: Structure.
Computer Programming BCT 1113
Module 2 Arrays and strings – example programs.
C++ Arrays.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Unions A union is a custom data type used for storing several variables in the same memory space. Although you can access any of those variables at any.
Chapter 1: Introduction to Data Structures(8M)
MSIS 655 Advanced Business Applications Programming
Dr Tripty Singh Arrays.
Lecture 14: Problems with Lots of Similar Data
C Programming Lecture-14 Unions
Structures, Unions, and Enumerations
Presentation transcript:

Array, Structure and Union

Array, Structure and Union Group of related data items which shares a common name More than one items, with same name and same data type If we need more variables of same type, then we can use array An array can be declared as int a[10]; a integer array with 10 elements data type array name [size];

Array, Structure and Union Each elements in the array can access using its relative position, called index of the array or subscript of the array Index of first element is 0, second element is 1 and so on int a[10]; Index a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] 10 20 30 40 50 60 70 80 90 100

Array, Structure and Union Each elements in the array can be access using a loop Reading an array for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } Printing an array printf(“%d “,a[i]); Where nsize of the array

Array, Structure and Union A structure is a collection of one or more variables, possibly of different types, grouped together under a single name Grouping logically related data, and giving this set of variables a higher-level name .

Array, Structure and Union A structure is defined by the keyword struct followed by a set of variables enclosed in braces. Consider the following structure to represent a person’s details. struct Personnel { char name[100]; int age; double height; }; The variables name, age and height are called members of the structure type Personnel.

Array, Structure and Union There are two ways to define variables of a particular structure type. Declare them at the structure definition. struct Personnel { char name[100]; int age; double height; } p1, p2, p3; /* Define 3 variables */ Define the variables at some point after the structure definition. struct Personnel p1, p2, p3; /* Define 3 variables */

Array, Structure and Union Union is same as the structure Like structure, union also contains members of different data type Union can be declared using the keyword union Syntax: union unionname{ member 1; member 2; . . . . }; Like structure a union variable can be declare Syn: union unionname variable list; popo

Array, Structure and Union The memory space occupy for a union variable is the size of largest member In structure each members has its own memory locations In union all members shares a common memory location In structure we can access all members at a time In union we can access only one member at a time popo

Array, Structure and Union Eg Stuct check { int age; char name[5]; }s; The memory space of struct variable s=sum of size of all members Ie sizeof(age)+sizeof(name) => 2+5 = 7 bytes The structure variable has separate memory locations for each members Can access all members at a time age (2 bytes) name (5 bytes) popo

Array, Structure and Union Eg union check { int age; char name[5]; }u; The memory space of union variable s=size of largest members Ie sizeof(name) => 5 = 5 bytes The union variable has only one memory locations for all members Can access only one member at a time name (5 bytes) popo