1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.

Slides:



Advertisements
Similar presentations
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Advertisements

Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 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
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
One-Dimensional Arrays. Introduction A one-dimensional array is a sequence of homogenous elements stored in contigous memory locations Motivating example:
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Chapter 9 Introduction to Arrays Fundamentals of Java.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
Objectives You should be able to describe: One-Dimensional Arrays
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
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 BCT 1113
Arrays Arrays A few types Structures of related data items
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

1 Two-Dimensional Arrays

2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values in a matrix are organized in rows and columns The number of rows and columns in a matrix are called the dimensions of the matrix Rows and columns are numbered 0,1,2, … An example of 2X3 matrix of double values:

3 Examples of matrices A matrix containing the scores of 3 exams for 5 students: A matrix containing the distances between 4 cities:

4 Declaring matrices The general form to declare a matrix: data-type array-name[expression1][expression2]; data-type is the type of array elements. Examples: int, double, char,… array-name must be a valid identifier expression1 and expression2 must evaluate to integers and cannot contain variables The value of expression1 is the number of rows The value of expression2 is the number of columns

 2003 Prentice Hall, Inc. All rights reserved. 5 Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –Tables with rows and columns –Specify row, then column –“Array of arrays” a[0] is an array of 4 elements a[0][0] is the first element of that array Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript

 2003 Prentice Hall, Inc. All rights reserved. 6 Multiple-Subscripted Arrays To initialize –Default of 0 –Initializers grouped by row in braces int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Row 0Row 1

 2003 Prentice Hall, Inc. All rights reserved. 7 Multiple-Subscripted Arrays Referenced like normal cout << b[ 0 ][ 1 ]; –Outputs 0 –Cannot reference using commas cout << b[ 0, 1 ]; Syntax error Function prototypes –Must specify sizes of subscripts First subscript not necessary, as with single-scripted arrays –void printArray( int b[][ 3 ] );

 2003 Prentice Hall, Inc. All rights reserved. 8 Declaration Examples int a[][]; //compiler error int a[][10]; //compiler error int a[5][]; //compiler error int a[5][10]; //ok The number of columns must always be provided. int a[2][]={{1,2},{3,4}}; //compiler error int a[][2] ={{1,2},{3,4}}; //ok int a[2][2]={{1,2,3,4}}; //ok int x; cin>>x; int a[x][10]; //compiler error

9 Example: Given float B[4][5]={ …}; Write a code segment to read the values of elements in B row by row. int I, J; for( I=0; I <=3; I++ ) for (J=0; J<=4; J++) cout<<B[I][J]<<endl;

10 Example: Given float B[4][5]={ …}; Write a code segment to read the values of elements in B column by column. int I, J; for( J=0; J <=4; J++ ) for(I=0; I<=3; I++) cin<<B[I][J]<<endl;

11 Example: Given float B[4][4]={ …}; Write a code segment to find the maximum in B. int I, J; float max=B[0][0]; for( I=0; I <=3; I++ ) for(J=0; J<=3; J++) if (B[I][J]>max) max=B[I][J]; cout<<max;

12 Example: Given float B[4][4]={ …}; Write a code segment to find the location of the maximum in B. int I, J, row, column; float max=B[0][0]; row=column=0; for( I=0; I <=3; I++ ) for(J=0; J<=3; J++) if (B[I][J]>max){ max=B[I][J]; row=I; column=J; } cout<<max<<row<<column;

13 Example: Given float B[4][4]={ …}; Write a code segment to print the elements on the main diagonal in B. int I, J; for( I=0; I <=3; I++ ) for(J=0; J<=3; J++) if (I==J) cout<<B[I][J];

14 Example: Given float B[4][4]={ …}; Write a code segment to print the elements above the main diagonal in B. int I, J; for( I=0; I <=3; I++ ) for(J=0; J<=3; J++) if (I<J) cout<<B[I][J];

15 Example: Given float B[4][4]={ …}; Write a code segment to print the elements below the main diagonal in B. int I, J; for( I=0; I <=3; I++ ) for(J=0; J<=3; J++) if (I>J) cout<<B[I][J];

16 Example: Given float B[4][4]={ …}; Write a code segment to compute the sum of elements on the second row in B. int J; float sum=0; for(J=0; J<=3; J++) sum+=B[1][J];

17 Example: Given float B[4][4]={ …}; Write a code segment to compute the sum of elements on the last column in B. int I; float sum=0; for(I=0; I<=3; I++) sum+=B[I][3];

18 Example: Given float B[4][4]={ …}; Write a code segment to exchange the first two columns in B. int I; float temp; for(I=0; I<=3; I++){ temp=B[I][0]; B[I][0]=B[I][1]; B[I][1]=temp; }

19 Example: Given float B[4][4]={ …}; float C[4][4]={ …}; float A[4][4]; Write a code segment to add B and C and store the result in A. int I, J; for(I=0; I<=3; I++) for(J=0; J<=3; J++) A[I][J]=B[I][J]+C[I][J];

 2003 Prentice Hall, Inc. All rights reserved. 20 Matrix multiplication * = A (pXq) B (qXr) AB (pXr) 2X4 4X3 2X3

21 Example: Given float B[4][8]={ …}; float C[8][5]={ …}; float A[4][5]; Write a code segment to multiply B and C and store the result in A. int I, J,K; double sum; for(I=0; I<=3; I++)//to populate all A for(J=0; J<=4; J++){//to populate the 1 st line of A sum=0; for(K=0,K<=7;K++) sum+=B[I][K]*C[K][J];//each element in A A[I][J]=sum; }

22 Two-Dimensional Arrays As Parameters Matrices are passed by reference; similar to one- dimensional arrays The number of columns in the formal parameter matrix must be specified The number of rows in the formal parameter matrix is usually omitted Some examples: void f(int a[][]){a[0][0]=10;}//compiler error void f(int a[][10]){a[0][0]=10;}//ok void f(int a[10][]){a[0][0]=10;}//compiler error

23 Example: write a function to set the elements of a 5X7 matrix of integers to 100. void store100(int X[5][7]){ // The 5 can be omitted int I, J; for(I=0; I<=4; I++) for(J=0; J<=6; J++) X[I][J]=100; }

24 Example: write a function to print the elements of a 5X7 matrix of integers. void printMatrix(int X[5][7]){ int I, J; for(I=0; I<=4; I++){ for(J=0; J<=6; J++) cout<<X[I][J]<<" "; cout<<endl; }

25 Example: write a function to print the elements of a matrix of integers. The function should receive matrices of varying number of rows and 7 columns. #include void printMatrix(int [][7], int); void printMatrix(int X[ ][7], int rows){ int I, J; for(I=0; I<=rows-1; I++){ for(J=0; J<=6; J++) cout<<X[I][J]<<" "; cout<<endl; } //an example of using printMatrix void main(){ int A[2][7]={{1,2,2,2,4,5,4}, {5,5,5,9,9,0,0}}; int B[3][7]={{1,2,2,2,4,5,4}, {5,5,5,9,9,0,0},{0,0,0,0,1,1,1}}; printMatrix(A,2); printMatrix(B,3); }

26 Example: write a function to compute the average of elements of a matrix of integers. The function should receive matrices of varying number of rows and 7 columns. #include double matrixAvg(int [][], int); double matrixAvg(int X[ ][7], int rows){ int I, J, sum=0; for(I=0; I<=rows-1; I++) for(J=0; J<=6; J++) sum+=X[I][J]; return (double)sum/(rows*7); } //an example of using matrixAvg void main(){ int A[2][7]={{1,2,2,2,4,5,4}, {5,5,5,9,9,0,0}}; int B[3][7]={{1,2,2,2,4,5,4}, {5,5,5,9,9,0,0},{0,0,0,0,1,1,1}}; cout<<matrixAvg(A,2); cout<<matrixAvg (B,3); }

27 Matrices of Characters Used to store a list of names in memory. Example: char N[3][6]={“ali”, “ahmad”, “sami”}; char M[3][6]={{'a', 'l', 'i', NULL}, {'a', 'h', 'm', 'a', 'd',NULL}, {‘s', ‘a', 'm', ‘i', NULL}}; char M[][6]={{'a', 'l', 'i', NULL}, {'a', 'h', 'm', 'a', 'd',NULL}, {‘s', ‘a', 'm', ‘i', NULL}}; cout<<N[1][2]; //m cout<<N[1]; //ahmad cin>>N[1]; //ok

28 Example : Write a code segment to read a list of 100 names and store it in memory. Assume each name does not contain more than 20 characters.. char N[100][20]; for(int I=0; I<=99; I++) cin>>N[I];

29 Example : Given char N[100][20]={…}; Write a code segment to print the names stored in A. for(int I=0; I<=99; I++) cout<<N[I];

30 Example : Write a function that receives a matrix containing a list of names and returns the number of names starting with the letter 'd'. int countNames (char X[][20], int rows){ int count=0; for(int I=0; I<=rows-1; I++) if(X[I][0]=='d') count++; return count; }

31 Matrices as Arrays of Arrays A matrix like int A[4][3]; is a one dimensional array of 4 elements: A[0], A[1], A[2], A[3]. Each of these arrays is a one-dimensional array of 3 elements. For example, A[0] is a one-dimensional array containing the elements A[0][0], A[0][1], A[0][2]. Rows of a matrix can be treated as one dimensional array. For example, int sum(int X[ ], int sz){…} void main(){ int A[10][20]={…}; cout<<sum(A[0],20); }

32 Example: Write a function to compute the length of a string stored in a one-dimensional array. Then, write another function that receives a matrix containing a list of names and prints the longest name in the matrix. The second function should call the first function. int length(char N[]){ int i=0; for(i=0; N[i]; i++); return i; } void printLongest(char X[][20], int m){ int Loc=0; int maxLength=length(X[0]); for(int I=0; I<=m-1; I++) if(length(X[I])>maxLength){ maxLength=length(X[I]); Loc=I; } cout<<X[Loc]; }