1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Two Dimensional Arrays Rohit Khokher
ICS103: Programming in C Searching, Sorting, 2D Arrays
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
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.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
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.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Java Unit 9: Arrays Declaring and Processing Arrays.
1 ICS103 Programming in C Ch7: Arrays. O BJECTIVES What is an Array? Declaring Arrays Visual representation of an Array Array Initialization Array Subscripts.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
A First Book of ANSI C Fourth Edition
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Two Dimensional Arrays
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.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
ICS103: Programming in C Searching, Sorting, 2D Arrays Muhamed F. Mudawar.
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.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
UNIT 10 Multidimensional Arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
2-D Arrays Declaration & Initialization. Declaration You can think of 2D arrays as a matrix rows and columns: – a 4x3 matrix –
Multidimensional Arrays Computer and Programming.
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.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
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.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
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.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Arrays. Arrays are objects that help us organize large amounts of information.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
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.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Programming application CC213
Today’s Material Arrays Definition Declaration Initialization
MULTI-DIMENSIONAL ARRAY
Computer Graphics Matrix
CS1100 Computational Engineering
Lecture 10 Arrays.
EKT150 : Computer Programming
CS111 Computer Programming
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Arrays and Matrices Prof. Abdul Hameed.
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays

2 Outline Introduction to 2-D Arrays Declaration of 2-D Arrays Accessing 2-D Array elements Initialization of 2-D Arrays Processing 2-D Arrays 2-D Arrays as parameters to functions

3 Introduction to 2-D Arrays A 2-D array is a contiguous collection of variables of the same type, that may be viewed as a table consisting of rows and columns. The same reason that necessitated the use of 1-D arrays can be extended to 2-D and other multi-D Arrays. For example, to store the grades of 30 students, in 5 courses require multiple 1-D arrays. A 2-D array allows all these grades to be handled using a single variable. This idea can be easily extended to other higher dimensions. Thus, we shall focus only on 2-D arrays.

4 Declaration of 2-D Arrays A 2-D array variable is declared by specifying the type of elements, the name of the variable, followed by the number of rows and number of columns – each is a separate bracket The following declares a 2-D array, table, having 3 rows and 4 columns. int table[3][4]; Both rows and columns are indexed from zero. So the three rows have indexes 0, 1 and 2 and four the columns have 0, 1, 2, 3. As we saw in 1-D array, it is a good practice to declare the sizes as constants. So a better declaration for the above is: #define ROWS 3 #define COLS 4 int table[ROWS][COLS];

5 Accessing 2-D Array elements A particular element of a 2-D array, table, is referenced by specifying its row and column indexes: table[RowIndex][columnIndex] For example, given the declaration: int table[3][4]; The following stores 64 in the cell with row index 1, column index 3. table[1][3] = 64; We use the same format to refer to an element in an expression: table[2][3] = table[1][3] + 2;

6 Initialization of 2-D Arrays As with 1-D arrays, if we already have the values to assign to the array at the point of declaration, then we can declare and initialize the 2-D array at the same time. The format is similar to 1-D array initialization except that a nested list is used, where each inner list represents a row. For example, the following declares and initializes our table. int table[3][4] = { {1,2,2,1}, {3,4,4,3}, {5,6,6,5} } Like 1-D array, if you provide less values than the declared size, the remaining cells are set to zero. However, unlike 1-D array where you can skip the size, here you must give at least the number of columns. int table[][4] = { {1,2,2,1}, {3,4,4,3}, {5,6,6,5} } //OK int table[][] = { {1,2,2,1}, {3,4,4,3}, {5,6,6,5} } //WRONG!

7 Processing 2-D Arrays To process 2-D array, we need to extend the loop we normally use with 1-D array to nested loops. This can be done either row-wise or column-wise. To process the elements row-wise, we use: for(int rowIndex = 0; rowIndex < ROWS; rowIndex++){ //process row# rowIndex; }  But processing a row involves processing each element of that row; so the complete algorithm is: for(int rowIndex = 0; rowIndex < ROWS; rowIndex++){ // process row# rowIndex for(int columnIndex = 0; columnIndex < COLUMNS; columnIndex++) //process element array[rowIndex][columnIndex] } To process elements of a 2-D array column-wise, we use: for(int columnIndex = 0; columnIndex < COLUMNS; columnIndex++){ // process column# columnIndex for(rowIndex = 0; rowIndex < ROWS; rowIndex++) process element array[rowIndex][columnIndex] }

8 Example 1 /*reads two marices from the user and add them */ #include #define ROWS 10 #define COLS 10 int main (void) { int i, j, a[ROWS][COLS], b[ROWS][COLS], c[ROWS][COLS] = {0}, rows, cols; printf("Enter number of rows for Matrix 1: "); scanf("%d", &rows); printf("Enter number of columns for Matrix 1: "); scanf("%d", &cols); printf("Enter the %d elements of Matrix 1 row-wise: \n", rows * cols); for(i=0; i<rows; i++) { // reading matrix a for(j=0; j<cols; j++) scanf("%d", &a[i][j]); } printf("Enter the %d elements of Matrix 2 row wise: \n", rows * cols); for(i=0; i<rows; i++) { // reading matrix b for(j=0; j<cols; j++) scanf("%d", &b[i][j]); }

9 Example 1 … /* Addition of two matrices */ for(i=0; i<rows; i++) { for(j=0; j<cols; j++) c[i][j]=a[i][j] + b[i][j]; } /*Print sum of two matrices */ printf("The sum of two matrices is: \n"); for(i=0; i<rows; i++) { for (j=0; j<cols; j++) printf("%5d ", c[i][j]); printf("\n"); } system("pause"); return 0; }

10 2-D Arrays as parameters to functions As with 1-D arrays, it is possible to declare functions that take 2-D array as parameter. However, one problem here is that in declaring the prototype of the function, we must specify at least the number of columns of the array, thus making the function less flexible. One solution to this problem is to use a constant defining the maximum number of columns and use additional parameter to receive the actual size of the array: void print_2d_array(int a[][COLS], int rows, int cols); While this solution makes the function a little more flexible, it is not a perfect solution since the function is not self-contained – it depends on the pre-processor constant COLS. Calling functions that take 2-D array as argument is same as calling functions that take 1-D array. Just give the name of the array with no brackets.

11 Example 2 #include #define ROWS 10 #define COLS 10 void read_2d_array(int a[][COLS], int rows, int cols); void add_2d_arrays(int a[][COLS], int b[][COLS], int c[][COLS], int rows, int cols); void print_2d_array(int a[][COLS], int rows, int cols); int main(void) { int i, j, a[ROWS][COLS], b[ROWS][COLS], c[ROWS][COLS]; int rows, cols; printf("Enter number of rows for Matrix 1: "); scanf("%d", &rows); printf("Enter number of columns for Matrix 1: "); scanf("%d", &cols); read_2d_array(a, rows, cols); // reading matrix a read_2d_array(b, rows, cols); // reading matrix b add_2d_arrays(a, b, c, rows, cols); /* Addition of two matrices */ printf("The sum of two matrices is: \n"); print_2d_array(c, rows, cols); /*Print sum of two matrices */ system("pause"); return 0; }

12 Example 2 … void read_2d_array(int a[][COLS], int rows, int cols) { int i, j; printf("Enter the %d elements of the 2-D array row-wise: \n", rows * cols); for(i=0; i<rows; i++) { for(j=0; j<cols; j++) scanf("%d", &a[i][j]); } void add_2d_arrays(int a[][COLS], int b[][COLS], int c[][COLS], int rows, int cols) { int i, j; for (i=0; i<rows; i++) { for (j=0; j<cols; j++) c[i][j] = a[i][j] + b[i][j]; } void print_2d_array(int a[][COLS], int rows, int cols) { int i, j; for(i=0; i<rows; i++) { for (j=0; j<cols; j++) printf("%5d ", a[i][j]); printf("\n"); }