◦ A one-dimensional array in C is declared implicitly by appending brackets to the name of a variable int list[5], *plist[5]; ◦ arrays start at index.

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
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.
Chapter 9 Imperative and object-oriented languages 1.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
CS-240 Data Structures in C Arrays Dick Steflik. Abstract Data Type A collection of pairs where index is an ordered set of integers and are values of.
CS Data Structures Chapter 2 Arrays and Structures.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
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.
Chapter 8 Arrays and Strings
CS Data Structures Chapter 4 Lists.
Chapter 2 Arrays and Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
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.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
CHAPTER 21 ARRAYS AND STRUCTURES. CHAPTER 22 Arrays Array: a set of index and value data structure For each index, there is a value associated with that.
Chapter 2 Arrays and Structures  The array as an abstract data type  Structures and Unions  The polynomial Abstract Data Type  The Sparse Matrix Abstract.
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
 Program data code Data StructureAlgorithm  Data vs. Variables int i = 10; int i[5]={3,5,7,9,10};
Data Structure Sang Yong Han Chung-Ang University Spring
Data Structures & Algorithm Analysis Arrays. Array: a set of pairs (index and value) data structure For each index, there is a value associated with that.
Array, Structure and Union
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CHAPTER 21 ARRAYS AND STRUCTURES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
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.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
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.
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.
Data Structure Sang Yong Han
Computer Programming BCT 1113
CSCE 3110 Data Structures & Algorithm Analysis
CHAPTER 2 ARRAYS AND STRUCTURES
Data Structures 4th Week
Module 2 Arrays and strings – example programs.
Data Structures Unit-2 Engineered for Tomorrow CSE, MVJCE.
 Zero - set zero of a polynomial
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
CS111 Computer Programming
C Structures, Unions, Bit Manipulations and Enumerations
Presentation transcript:

◦ A one-dimensional array in C is declared implicitly by appending brackets to the name of a variable int list[5], *plist[5]; ◦ arrays start at index 0 in C

VariableMemory Address list[0]base address =  list[1]  + sizeof(int) list[2]  + 2·sizeof(int) list[3]  + 3·sizeof(int) list[4]  + 4·sizeof(int)  list[i] in C programs ◦ C interprets it as a pointer to an integer or its value

int *list1;  pointer variable to an int int list2[5];  pointer constant to an int, and five memory locations for holding integers are reserved ◦ (list2+i) equals &list2[i], and *(list2+i) equals list2[i] regardless of the type of the array list2

 Ex 2.1 [1-dimensional array addressing] int one[] = {0,1,2,3,4}; ◦ write a function that prints out both the address of the ith element of the array and the value found at this address one one+1 one+2 one+3 one

void print1(int *ptr,int rows) { printf(“Address Contents\n”); for(i=0;i<rows;i++) printf(“%8u%5d\n”,ptr+i,*(ptr+i)); printf(“\n”); } ◦ One-dimensional array accessed by address  address of i th element : ptr + i  obtain the value of the i th value : *(ptr + i)

◦ struct  structure or record  the mechanism of grouping data  permits the data to vary in type  collection of data items where each item is identified as to its type and name

struct { char name[10]; int age; float salary; } person;  creates a variable whose name is person and has three fields 1) a name that is a character array 2) an integer value representing the age of the person 3) a float value representing the salary of the individual

 use of the. as the structure member operator strcpy(person.name, ”james”); person.age = 10; person.salary = 35000; ◦ select a particular member of the structure

◦ typedef statement create our own structure data type typedef struct human_being { char name[10]; int age; float salary; }; or typedef struct { char name[10]; int age; float salary; } human_being;

 human_being ◦ the name of the type defined by the structure definition human_being person1, person2; if(strcmp(person1.name, person2.name)) printf(“The two people do not have the same\ name\n”); else printf(“The two people have the same\ name\n”);

 Assignment ◦ permits structure assignment in ANSI C person1 = person2; ◦ but, in most earlier versions of C, assignment of structures is not permitted strcpy(person1.name,person2.name); person1.age=person2.age; person1.salary=person2.salary;

 equality or inequality ◦ Cannot be directly checked ◦ Function to check equality of struct int humans_equal(human_being person1, human_being person2) { if(strcmp(person1.name,person2.name)) return FALSE; if(person1.age!=person2.age) return FALSE; if(person1.salary!=person2.salary) return FALSE; return TRUE; }

 Embedding of a structure within a structure typedef struct { int month; int day; int year; } date; typedef struct human_being { char name[10]; int age; float salary; date dob; };

eg) A person born on February 14, 1988  person1.dob.month = 2;  person1.dob.day = 14;  person1.dob.year = 1988;

 Unions ◦ similar to a structure, but the fields of a union must share their memory space ◦ only one field of the union is “active” at any given time

typedef struct sex_type { enum tag_field {female,male} sex; union { int children; int beard; } u; }; typedef struct human_being { char name[10]; int age; float salary; date dob; sex_type sex_info; }; human_being person1,person2;

◦ assign values to person1 and person2 person1.sex_info.sex = male; person1.sex_info.u.beard = FALSE; and person2.sex_info.sex = female; person2.sex_info.u.children = 4;

struct { int i, j; float a, b; }; or struct { int i; int j; float a; float b; };  stored in the same way ◦ increasing address locations in the order specified in the structure definition  size of an object of a struct or union type ◦ the amount of storage necessary to represent the largest component

 one or more of its components is a pointer to itself  usually require dynamic storage management routines to explicitly obtain and release memory typedef struct list { char data; list *link; }; ◦ the value of link  address in memory of an instance of list or null pointer

list item1, item2, item3; item1.data = ‘a’; item2.data = ‘b’; item3.data = ‘c’; item1.link = item2.link = item3.link = NULL; ‘a’ item1 ‘b’ item2 ‘c’ item3

 attach these structure together item1.link=&item2; item2.link=&item3; ‘a’ item1 ‘b’ item2 ‘c’ item3

 representing matrix by using array ◦ m by  n (m rows, n columns) ◦ use two-dimensional array ◦ space complexity  S(m, n) = m *  n

 Sparse matrix A[6,6]

 common characteristics ◦ most elements are zero’s ◦ inefficient memory utilization  solutions ◦ store only nonzero elements ◦ using the triple ◦ must know  the number of rows  the number of columns  the number of non-zero elements

#define MAX_TERMS 101 typedef struct { int col; int row; int value; } term; term a[MAX_TERMS]; ◦ a[0].row: the number of rows ◦ a[0].col: the number of columns ◦ a[0].value: the total number of non-zoros ◦ choose row-major order

 sparse matrix as triples  space complexity ◦ S(n, m) = 3 *  t, where t : the number of non-zero’s  independent of the size of rows and columns

 transpose a matrix ◦ interchange rows and columns ◦ move a[i][j] to b[j][i] Hanyang University

 Algorithm BAD_TRANS for each row i take element ; store it as element of the transpose; end; ◦ problem: data movement  Algorithm TRANS for all elements in column j place element in element end; ◦ problem: unnecessary loop for each column

void transpose(term a[],term b[]) { int n,i,j,currentb; n = a[0].value; b[0].row = a[0].col; b[0].col = a[0].row; b[0].value = n; if(n > 0) { currentb = 1; for(i = 0; i < a[0].col; i++) for(j = 1; j <= n; j++) if(a[j].col == i) { b[currentb].row = a[j].col; b[currentb].col = a[j].row; b[currentb].value = a[j].value; currentb++; }

 Time complexity : O(cols·elements)  create better algorithm by using a little more storage ◦ row_terms  the number of element in each row ◦ starting_pos  the starting point of each row

void fast_transpose(term a[],term b[]) { int row_terms[MAX_COL]; int starting_pos[MAX_COL]; int i,j; int num_cols = b[0].row = a[0].col; int num_terms = b[0].value = a[0].value; b[0].col = a[0].row; if(num_terms > 0) { for(i = 0; i < num_cols; i++) row_terms[i] = 0; for(i = 1; i <= num_terms; i++) row_terms[a[i].col]++; (to be continued)

starting_pos[0] = 1; for(i = 1; i < num_cols; i++) starting_pos[i] = starting_pos[i-1] + row_terms[i-1]; for(i = 1; i <= num_terms; i++) { j=starting_pos[a[i].col]++; b[j].row = a[i].col; b[j].col = a[i].row; b[j].value = a[i].value; }

◦ Time complexity : O(cols + elements)

 Internal representation of multidimensional arrays ◦ how to state n-dimensional array into 1-dimensional array ? ◦ how to retrieve arbitrary element in a[upper 0 ][upper 1 ]···[upper n-1 ]  the number of element in the array n-1  upper i i=0 ◦ eg) for a[10][10][10], 10*10*10 = 1000 (units)

 represent multidimensional array by ◦ what order ?  row-major-order ◦ store multidimensional array by rows a[0][0]···[0] a[0][0]···[1] ··· a[0][0]···[0][upper n-1 -1] a[0][0]···[1][0] ··· a[0][1]···[upper n-1 -1] a[0][2]···[0] ··· a[1][0]···[0] ··· :: starting address

 how to retrieve ◦ starting-address + offset-value ◦ assume  : starting-address  1-dimensional array a[u 0 ] a[0]:  a[1]:  + 1 · · a[u 0 -1]:  + (u 0 - 1) a[i] =  + i

 2-dimensional array a[u 0 ][u 1 ] a[i][j] =  + i·u 1 + j i j ?

 3-dimensional array a[u 0 ][u 1 ][u 2 ] a[i][j][k] =  + i·u 1 ·u 2 + j·u 2 + k =  + u 2 [i·u 1 + j] + k  general case a[u 0 ][u 1 ]···[u n-1 ] a[i 0 ][i 1 ]···[i n-1 ] =  +  i j ·a j where a j =  u k 0 < j < n-1 a n-1 = 1 n-1 j=0 k=j+1 n-1