2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
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 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.
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 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Arrays.
Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I.
Arrays CS 308 – Data Structures. One-Dimensional Arrays A list of values with the same data type that are stored using a single group name (array name).
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
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.
Arrays Multi-dimensional initialize & display Sorting Part II.
Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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.
Arrays.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Module 1: Array ITEI222 - Advance Programming Language.
Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers.
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.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
ME 142 Engineering Computation I More Loops & Arrays.
CS 1430: Programming in C++.
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.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-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.
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)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
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.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Two Dimensional Array Mr. Jacobs.
CSCE 210 Data Structures and Algorithms
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
EKT150 : Computer Programming
Counting Loops.
Multidimensional array
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Presentation transcript:

2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006

Declarations datatype array_name [num_of_rows] [num_of_col]; int array_1[10][10]; // indexed from (0,0) to (9,9) char array_2[3][4]; //indexed from (0,0) to (2,3) float array_3[2][10]; //indexed from (0,0) to (1,9) The first index always indicates the row, the second the column.

Declarations char letters[3][4]; char letters[3][4]; or or const int row = 3; const int col = 4; char letters[row][col]; //preferred way const int row = 3; const int col = 4; char letters[row][col]; //preferred way

1-D vs. 2-D Arrays ,00,10,20,30,41,01,11,21,31,4 2,02,12,22,32,4 3,03,13,23,33,4 4,04,14,24,34,4

int table[4][3]; table[2][2]

Examples float array_A[12][2]; char array_B[7][3]; int array_C[4][10];

Row-Major Order |72|45|15|62|10|0|19|99|27|38|24|89| ? | ? | |72|45|15|62|10|0|19|99|27|38|24|89| ? | ? |

Initialization int nums[3][4] = {2,4,6,8,1,3,5,7,5,2,7,10}; int nums[3][4] = {2,4,6,8,1,3,5,7,5,2,7,10}; int nums[3][4] = {2,4,6,8, int nums[3][4] = {2,4,6,8, 1,3,5,7, 1,3,5,7, 5,2,7,10}; 5,2,7,10}; int nums[3][4] = { {2,4,6,8}, {1,3,5,7}, {1,3,5,7}, {5,2,7,10} }; {5,2,7,10} };

Accessing All Elements of an Array for ( i = 0; i < row; i++ ) { for( j=0; j<column; j++ ) cout << bunch_of_nums[i][j] << ‘\t’; cout << endl; cout << endl;}

Accessing Individual Elements int num; int bunch_of_nums[10][10]; bunch_of_nums[0][0] = 25; num = bunch_of_nums[3][5]; int num; int bunch_of_nums[10][10]; bunch_of_nums[0][0] = 25; num = bunch_of_nums[3][5];

Using Subscript Expressions result = result + value[ i + 2][ i * num] ; result = result + value[ i + 2][ i * num] ; cout << num[ i % row ][col-6]; cout << num[ i % row ][col-6];

Worksheet  Declare an array to store 5x5 matrix of integers.  List all of the elements on the major diagonal.  List all of the elements on the minor diagonal.  Write a code segment that would display all of the elements on the major diagonal.  Write a code segment that would display all of the elements on the minor diagonal.

const int size = 5; int square[size][size]; // display major diagonal in one loop for (int i = 0; i < size; i++) cout << square[i][i] << endl; // display minor diagonal in one loop for (int i = 0; i < size; i++) cout << square[i][size-1-i] << endl;

const int size = 5; int square[size][size]; // display major diagonal in two loops for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (i == j) cout << square[i][j] << endl; // display minor diagonal in two loops for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) if (i + j == size-1) cout << square[i][j] << endl;

Worksheet Write a code segment that declares Write a code segment that declares 45 x 33 array of integers and initializes all elements to 0. The program should then prompt the user for a location in that array ( row number, column number) and tell the user whether it’s a valid location ( i.e. within bounds). For example (4, 16) is a valid, (-3, 5) is invalid, (13, 56) is invalid. If location is valid, display the contents of that location. 45 x 33 array of integers and initializes all elements to 0. The program should then prompt the user for a location in that array ( row number, column number) and tell the user whether it’s a valid location ( i.e. within bounds). For example (4, 16) is a valid, (-3, 5) is invalid, (13, 56) is invalid. If location is valid, display the contents of that location.

const int nrow = 45; const int ncol = 33; int arr[nrow][ncol] = { 0 }; do { cout << "Enter row and column: "; cout << r << c; } while (r = nrow || c = ncol); c = ncol); cout << "The element in (" << r << ", " << c << ") is " << arr[r][c];

2-D Arrays in Functions void ProcessValues (int [ ][5], int, int); //works with ALL 2-d arrays that have //works with ALL 2-d arrays that have // exactly 5 columns: i.e. 10x5, 2x5 …. // exactly 5 columns: i.e. 10x5, 2x5 ….

int max(int[ ][2], int, int); void main() { const int nrow = 4, ncol = 2; int nums[nrow][ncol] = {3,2,5,13,1,7,9,4}; cout << "The max value of the array is " cout << "The max value of the array is " << max(nums, nrow, ncol); << max(nums, nrow, ncol);} int max(int vals[ ][2], int nr, int nc) { int i, j, max = vals[0][0]; for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) if (max < vals[i][j]) max = vals[i][j]; for (i = 0; i < nr; i++) for (j = 0; j < nc; j++) if (max < vals[i][j]) max = vals[i][j]; return max; return max;}

Worksheet Write a function that receives an array of floating point values, number of rows and number of columns (known to be 7) in that array, and a “special” number between 0 and 6. The function should return the sum of all elements in the column that corresponds to the “special” number. Write a function that receives an array of floating point values, number of rows and number of columns (known to be 7) in that array, and a “special” number between 0 and 6. The function should return the sum of all elements in the column that corresponds to the “special” number.