Arrays- Part 2 Spring 2013Programming and Data Structure1.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
Spring Semester 2013 Lecture 5
Programming and Data Structure
Structures Spring 2013Programming and Data Structure1.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Kernighan/Ritchie: Kelley/Pohl:
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
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
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.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Java Unit 9: Arrays Declaring and Processing Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
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.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
POINTERS.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Arrays.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Windows Programming Lecture 03. Pointers and Arrays.
Department of Computer Science Western Michigan University
Programming and Data Structure
Array 9/8/2018.
C Passing arrays to a Function
Programming and Data Structure
Multiple Dimension Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
2-d Arrays.
Arrays Arrays A few types Structures of related data items
C++ Array 1.
Arrays and Matrices Prof. Abdul Hameed.
Presentation transcript:

Arrays- Part 2 Spring 2013Programming and Data Structure1

Two Dimensional Arrays We have seen that an array variable can store a list of values. Many applications require us to store a table of values. Spring 2013Programming and Data Structure Student 1 Student 2 Student 3 Student 4 Subject 1Subject 2Subject 3Subject 4Subject 5

Contd. The table contains a total of 20 values, five in each line. – The table can be regarded as a matrix consisting of four rows and five columns. C allows us to define such tables of items by using two-dimensional arrays. Spring 2013Programming and Data Structure3

Declaring 2-D Arrays General form: type array_name [row_size][column_size]; Examples: int marks[4][5]; float sales[12][25]; double matrix[100][100]; Spring 2013Programming and Data Structure4

Accessing Elements of a 2-D Array Similar to that for 1-D array, but use two indices. – First indicates row, second indicates column. – Both the indices should be expressions which evaluate to integer values. Examples: x[m][n] = 0; c[i][k] += a[i][j] * b[j][k]; a = sqrt (a[j*3][k]); Spring 2013Programming and Data Structure5

How is a 2-D array is stored in memory? Starting from a given memory location, the elements are stored row-wise in consecutive memory locations. x: starting address of the array in memory c: number of columns k: number of bytes allocated per array element – a[i][j]  is allocated memory location at address x + (i * c + j) * k Spring 2013Programming and Data Structure6 a[0]0] a[0][1] a[0]2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] Row 0Row 1Row 2

How to read the elements of a 2-D array? By reading them one element at a time for (i=0; i<nrow; i++) for (j=0; j<ncol; j++) scanf (“%f”, &a[i][j]); The ampersand (&) is necessary. The elements can be entered all in one line or in different lines. Spring 2013Programming and Data Structure7

How to print the elements of a 2-D array? By printing them one element at a time. for (i=0; i<nrow; i++) for (j=0; j<ncol; j++) printf (“\n %f”, a[i][j]); – The elements are printed one per line. for (i=0; i<nrow; i++) for (j=0; j<ncol; j++) printf (“%f”, a[i][j]); – The elements are all printed on the same line. Spring 2013Programming and Data Structure8

Contd. for (i=0; i<nrow; i++) { printf (“\n”); for (j=0; j<ncol; j++) printf (“%f ”, a[i][j]); } – The elements are printed nicely in matrix form. How to print two matrices side by side? Spring 2013Programming and Data Structure9

Example: Matrix Addition Spring 2013Programming and Data Structure10 #include main() { int a[100][100], b[100][100], c[100][100], p, q, m, n; scanf (“%d %d”, &m, &n); for (p=0; p<m; p++) for (q=0; q<n; q++) scanf (“%d”, &a[p][q]); for (p=0; p<m; p++) for (q=0; q<n; q++) scanf (“%d”, &b[p][q]); for (p=0; p<m; p++) for (q=0; q<n; q++) c[p]q] = a[p][q] + b[p][q]; for (p=0; p<m; p++) { printf (“\n”); for (q=0; q<n; q++) printf (“%f ”, a[p][q]); }

Passing Arrays to a Function An array name can be used as an argument to a function. – Permits the entire array to be passed to the function. – Array name is passed as the parameter, which is effectively the address of the first element. Rules: – The array name must appear by itself as argument, without brackets or subscripts. – The corresponding formal argument is written in the same manner. Declared by writing the array name with a pair of empty brackets. Dimension or required number of elements to be passed as a separate parameter. Spring 2013Programming and Data Structure11

The Actual Mechanism When an array is passed to a function, the values of the array elements are not passed to the function. – The array name is interpreted as the address of the first array element. – The formal argument therefore becomes a pointer to the first array element. – When an array element is accessed inside the function, the address is calculated using the formula stated before. – Changes made inside the function are thus also reflected in the calling program. Spring 2013Programming and Data Structure12

Contd. Passing parameters in this way is called call-by-reference. Normally parameters are passed in C using call-by-value. Basically what it means? – If a function changes the values of array elements, then these changes will be made to the original array that is passed to the function. – This does not apply when an individual element is passed on as argument. Spring 2013Programming and Data Structure13

Passing 2-D Arrays Similar to that for 1-D arrays. – The array contents are not copied into the function. – Rather, the address of the first element is passed. For calculating the address of an element in a 2-D array, we need: – The starting address of the array in memory. – Number of bytes per element. – Number of columns in the array. The above three pieces of information must be known to the function. Spring 2013Programming and Data Structure14

Example Usage Spring 2013Programming and Data Structure15 #include main() { int a[15][25], b[15]25]; : add (a, b, 15, 25); : } void add (x, y, rows, cols) int x[][25], y[][25]; int rows, cols; { : } We can also write int x[15][25], y[15][25]; Number of columns

Example: Transpose of a matrix Spring 2013Programming and Data Structure16 void transpose (int x[][100], int n) { int p, q; for (p=0; p<n; p++) for (q=0; q<n; q++) { t = x[p][q]; x[p][q] = x[q][p]; x[q][p] = t; } transpose(a,3) a[100][100]

The Correct Version Spring 2013Programming and Data Structure17 void transpose (int x[][100], n) { int p, q; for (p=0; p<n; p++) for (q=p; q<n; q++) { t = x[p][q]; x[p][q] = x[q][p]; x[q][p] = t; }

Some Exercise Problems to Try Out Multiply two matrices of orders m x n and n x p respectively. Spring 2013Programming and Data Structure18