TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914, 20112012.

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

CHAPTER 10 ARRAYS II Applications and Extensions.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
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.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
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.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
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 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Chapter 8 Arrays and Strings
Arrays- Part 2 Spring 2013Programming and Data Structure1.
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.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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.
Arrays & Vectors Week 5. The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list.
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.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
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.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
 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.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Arrays.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Module 1: Array ITEI222 - Advance Programming Language.
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.
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.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
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.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
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.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
13.4 Product of Two Matrices
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Basic Array Definition
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays of Two-Dimensions
Array Data Structure Chapter 6
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

TK1914: C++ Programming Array II

Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,

Two-Dimensional Arrays A collection of a fixed number of components arranged in two dimensions –All components are of the same type Declaration syntax: dataType arrayName[intexp1][intexp2]; where intexp1 and intexp2 are expressions yielding positive integer values, which specify number of rows and number of columns respectively Sometimes called matrices or tables 3FTSM :: TK1914,

mark [0] [1] [2] [3] [0][1][2][3][4] Example: int mark[4][5]; 4FTSM :: TK1914,

Initialization Like one-dimensional arrays –Two-dimensional arrays can be initialized when they are declared Example: –int mark [][] = { {87, 86, 88, 82, 89}, {64, 69, 60, 63, 66}, {91, 90, 94, 98, 93}, {77, 74, 75, 72, 70} }; mark [0] [1] [2] [3] [0][1][2][3][4] inner braces can be omitted 5FTSM :: TK1914,

Accessing Array Components The syntax to access a component of a two- dimensional array is: arrayName[indexexp1][indexexp2] where indexexp1 and indexexp2 are expressions yielding nonnegative integer values indexexp1 specifies the row position and indexexp2 specifies the column position 6FTSM :: TK1914,

Accessing Array Components Example: temp = mark[2][4] Example: find sum of elements in second row. row = 1; sumRow = 0; for (int col = 0; col < 5; col++) sumRow = sumRow + mark[row][col]; mark [0] [1] [2] [3] [0][1][2][3][4] FTSM :: TK1914,

Processing Two-Dimensional Arrays A two-dimensional array can be processed in three different ways: 1.Process the entire array 2.Process a particular row of the array, called row processing 3.Process a particular column of the array, called column processing 8FTSM :: TK1914,

Processing Two-Dimensional Arrays (continued) Each row and each column of a two-dimensional array is a one-dimensional array When processing a particular row or column of a two-dimensional array –we use algorithms similar to processing one- dimensional arrays 9FTSM :: TK1914,

prog09.9.cpp 10FTSM :: TK1914,

11FTSM :: TK1914,

12FTSM :: TK1914,

13FTSM :: TK1914,

Passing Two-Dimensional Arrays as Parameters to Functions Two-dimensional arrays can be passed as parameters to a function By default, arrays are passed by reference The base address, which is the address of the first component of the actual parameter, is passed to the formal parameter When declaring a two-dimensional array as a formal parameter –can omit size of first dimension, but not the second Number of columns must be specified 14FTSM :: TK1914,

15 #include using namespace std; const int NUMBER_OF_ROWS = 6; const int NUMBER_OF_COLUMNS = 5; void printMatrix(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void sumRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void largestInRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); #include using namespace std; const int NUMBER_OF_ROWS = 6; const int NUMBER_OF_COLUMNS = 5; void printMatrix(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void sumRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void largestInRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); Example 9-10 (pg 528) prog09.10.cpp FTSM :: TK1914,

16 int main() { int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS] = {{23, 5, 6, 15, 18}, {4, 16, 24, 67, 10}, {12, 54, 23, 76, 11}, {1, 12, 34, 22, 8}, {81, 54, 32, 67, 33}, {12, 34, 76, 78, 9}}; //Line 1 printMatrix(board, NUMBER_OF_ROWS); //Line 2 cout << endl; //Line 3 sumRows(board, NUMBER_OF_ROWS); //Line 4 cout << endl; //Line 5 largestInRows(board, NUMBER_OF_ROWS); //Line 6 } int main() { int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS] = {{23, 5, 6, 15, 18}, {4, 16, 24, 67, 10}, {12, 54, 23, 76, 11}, {1, 12, 34, 22, 8}, {81, 54, 32, 67, 33}, {12, 34, 76, 78, 9}}; //Line 1 printMatrix(board, NUMBER_OF_ROWS); //Line 2 cout << endl; //Line 3 sumRows(board, NUMBER_OF_ROWS); //Line 4 cout << endl; //Line 5 largestInRows(board, NUMBER_OF_ROWS); //Line 6 } FTSM :: TK1914,

Example: Matrix manipulation #include using namespace std; const int N = 3; void inputMatrix(int mat[][N], int n); void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int n); void outputMatrix(int mat[][N], int n); void main() { int matrix1[N][N], matrix2[N][N], output[N][N]; cout << "Input matrix 1: " << endl; inputMatrix(matrix1, N); cout << "Input matrix 2: " << endl; inputMatrix(matrix2, N); addMatrix(matrix1, matrix2, output, N); cout << "Answer for matrix1 add matrix2: " << endl; outputMatrix(output, N); } #include using namespace std; const int N = 3; void inputMatrix(int mat[][N], int n); void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int n); void outputMatrix(int mat[][N], int n); void main() { int matrix1[N][N], matrix2[N][N], output[N][N]; cout << "Input matrix 1: " << endl; inputMatrix(matrix1, N); cout << "Input matrix 2: " << endl; inputMatrix(matrix2, N); addMatrix(matrix1, matrix2, output, N); cout << "Answer for matrix1 add matrix2: " << endl; outputMatrix(output, N); } 17FTSM :: TK1914,

void inputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) cin >> mat[i][j]; } void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) mat3[i][j] = mat1[i][j] + mat2[i][j]; } void outputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) cout << mat[i][j]; cout << endl; } void inputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) cin >> mat[i][j]; } void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) mat3[i][j] = mat1[i][j] + mat2[i][j]; } void outputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) cout << mat[i][j]; cout << endl; } 18FTSM :: TK1914,

Exercise Write matrix manipulation functions as listed below: –substract two matrices –multiply two matrices –to calculate transpose of a matrix Write function calls in the main program to test your matrix manipulation functions 19FTSM :: TK1914,

Summary In a two-dimensional array, the elements are arranged in a table form To access an element of a two-dimensional array, you need a pair of indices: one for the row position and one for the column position In row processing, a two-dimensional array is processed one row at a time In column processing, a two-dimensional array is processed one column at a time 20FTSM :: TK1914,

Summary (continued) In row processing, a two-dimensional array is processed one row at a time In column processing, a two-dimensional array is processed one column at a time 21FTSM :: TK1914,