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]

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Structure.
Pointer to Structure. Structure variable can be access using pointers int a=10,*p; Here p  is an integer type pointer variable, p can hold the address.
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.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
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
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
 2006 Pearson Education, Inc. All rights reserved Arrays.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Prepared By Ms.R.K.Dharme Head Computer Department.
Computer Science 210 Computer Organization Arrays.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Paging Example What is the data corresponding to the logical address below:
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
Array, Structure and Union
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Data Structures - Part I CS 215 Lecture 7. Motivation  Real programs use abstractions like lists, trees, stacks, and queues.  The data associated with.
Struct Data Type in C++ What Are Structures?
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
REEM ALMOTIRI Information Technology Department Majmaah University.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
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.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Two-Dimensional Arrays
Programming Languages and Paradigms
DATA HANDLING.
7 Arrays.
Chapter 1: Introduction to Data Structures(8M)
Abstract Data Types, Elementary Data Structures and Arrays
C Programming Lecture-13 Structures
C Programming Pointers
EECE.2160 ECE Application Programming
Structures, Unions, and Enumerations
Arrays and Pointers.
Introduction to Pointers
Presentation transcript:

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] specifies an array of 100 integers – Two basic operations: Extraction e.g. a[i] Storage e.g. a[i]=x;

1-D Array Element Access Address of A[i]=base(A)+i*esize Here base(A)=100 and esize=4 A[0] A[1] A[2] A[4] A[5] A[6] 100 Addresses of Individual Elements in int A[7]

Implementing 1-D Array In C, an array variable is implemented as a pointer variable. E.g. in int b[100], the type of b is “pointer to int” or “int *”.

2-D Arrays Rows as well as columns e.g. int a[3][5] The number of rows or columns is called range of that dimension A 2-D array only a logical data structure, because physical hardware does not have such facility (i.e. memory is linear)

2-D Arrays Thinking in 2 dimensions is convenient to programmers in many situations, e.g. – A map – A checkerboard – Any set of values that are dependent on two inputs; a departmental store that has 20 branches each selling 30 items int sales[20][30];

Implementing a 2-D array Mapping from 2-D logical space to 1-D physical space Two approaches – Row major order – Column major order

Row-Major Order The first row of the array occupies the first set of memory locations reserved for the array, the second row occupies the second set, and so on. A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] Row 0 Row 1 Representation of A[2][3] in Row-major Order

Address of an Element in Row-Major Order Suppose int ar[r1][r2] is stored in row-major order with base address base(ar) and element size esize. The address of element ar[i1][i2] can be calculated by calculating the address of the first element of row i1 and adding the quantity i2*esize

Address of an Element in Row-Major Order The address of first element of row i1 is base(ar)+i1*r2*esize Therefore the address of ar[i1][i2]is base(ar)+(i1*r2)*esize+i2*esize or base(ar)+(i1*r2+i2)*esize

Element Access in Row-Major Order Address of A[i1][i2]=base(A)+(i1*r2+i2)*esize Here base(A)=100, r1=2, r2=3, esize=4 A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] Row 0 Row 1 Representation of A[2][3] in Row-major Order

EXISTING DATA STRUCTURES IN C/C++ STRUCTURES – A structure is a group of items in which each item is identified by its own name – Each item is called a member (or field) of the structure

Structures struct nametype { char first[10]; char midinit; char last[20]; }; struct nametype sname, ename; variables Data type

struct Member Access Dot operator (‘.’), e.g. cout<<sname.first; ename.midinit=‘m’; for (i=0;i<20;i++) sname.last[i]=ename.last[i];

Struct Legal Operations Assignment between struct type variables is allowed (ANSI C) struct nametype { char first[10]; char mid; char last[20]; }; struct nametype n1, n2; // you can omit the keyword struct strcpy (n1.first,"Muhammad"); n1.mid='Y'; strcpy (n1.last,"Javed"); n2=n1; //struct assignment cout<<n2.first<<" "<<n2.mid<<". "<<n2.last;

Nested Structures struct addrtype { char straddr[40]; char city[10]; char state[3]; char zip[6]; }; struct nmadtype { struct nametype name; struct addrtype address; }; struct nmadtype nmad1, nmad2;

Structure Implementation The amount of storage set aside for a structure is the sum of storage specified by each of its member types. For instance, struct structtype { int field1; float field2; char field3[10]; }; struct structtype r; Suppose size of int is 4 bytes, size of float is 4, and size of char is 1 byte respectively. Then amount of memory allocated for variable r is ?

Structure Implementation Answer: 18 bytes Usually consecutive storage locations are allocated to members of structure Associated with each member of struct is an offset that is added to the base address of the struct to reach that field.