ECE Application Programming

Slides:



Advertisements
Similar presentations
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Advertisements

1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
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.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
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.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
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.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Two-Dimensional Arrays
ECE Application Programming
Two Dimensional Array Mr. Jacobs.
ECE Application Programming
ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
MATLAB: Structures and File I/O
Engineering Problem Solving with C++, Etter/Ingber
EKT150 : Computer Programming
EECE.2160 ECE Application Programming
Multidimensional Arrays
2D Arrays Just another data structure Typically use a nested loop
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
INC 161 , CPE 100 Computer Programming
EECE.2160 ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C++ Array 1.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

16.216 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 32: 2-dimensional arrays

ECE Application Programming: Lecture 32 Lecture outline Announcements/reminders Program 7 due today Program 8 to be posted ASAP Grading on previous programs to be completed ASAP Today Two-dimensional arrays 6/12/2018 ECE Application Programming: Lecture 32

Two-dimensional arrays Two-dimensional arrays: can be used to represent tabular data Declaration: <type> <name>[<rows>][<cols>] Example (see below): int x[3][4]; Index elements similarly to 1-D arrays Col. 0 Col. 1 Col. 2 Col. 3 Row 0 x[0][0] x[0][1] x[0][2] x[0][3] Row 1 x[1][0] x[1][1] x[1][2] x[1][3] Row 2 x[2][0] x[2][1] x[2][2] x[2][3] 6/12/2018 ECE Application Programming: Lecture 32

ECE Application Programming: Lecture 32 Initializing 2D arrays Can initialize similarly to 1D arrays, but must specify dimensions Each row treated like a 1D array; rows separated by commas: int y[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; 1 2 3 4 5 6 7 8 9 10 11 12 6/12/2018 ECE Application Programming: Lecture 32

ECE Application Programming: Lecture 32 2D arrays and loops Typically use nested loops to work with 2-D arrays One loop inside another: for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { x[i][j] = y[i][j] * 2; } Be careful in loop body—switching your loop indices will cause trouble Using x[j][i] would take you outside of the array! 6/12/2018 ECE Application Programming: Lecture 32

Example: Working with 2-D arrays Complete this program, which counts the # of negative values in each row of a 2-D array (assume the necessary #includes are done): #define NRows 3 // # of rows #define NCols 4 // # of columns int main() { double x[NRows][NCols] = // 2-D array { { 10, 2.5, 0, 1.5}, {-2.3, -1.1, -0.2, 0}, {10.5, -6.1, 23.4, -9.2} }; int negCnt[NRows] = {0}; // Initialize entire row count array to 0 int i, j; // Row and column indices /* INSERT CODE HERE--Visit every element in array x and count the number of negative values in each row */ // Now print the row counts for (i = 0; i < NRows; i++) printf(“Row %d has %d negative values.\n”, i, negCnt[i]); return 0; } 6/12/2018 ECE Application Programming: Lecture 32

ECE Application Programming: Lecture 32 Example solution /* Code to be added to visit every element in array x and count the number of negative values in each row */ for (i = 0; i < NRows; i++) for (j = 0; j < NCols; j++) if (x[i][j] < 0) negCnt[i]++; 6/12/2018 ECE Application Programming: Lecture 32

2-D arrays and functions When passing 2-D array to function, can omit first dimension (rows) but must list columns Example: // Assume n = # of rows int f(int arr[][4], int n); int main() { int x[3][4]; f(x, 3); ... } 6/12/2018 ECE Application Programming: Lecture 32

Example: 2-D arrays and functions Say we have a program that stores student exam scores in a 2-D array: Each row represents an individual student Each column represents one of the 3 exams Write functions to: Calculate the exam average for each student and store it in a 1-D array that is accessible in the main program Assume all exams have equal weight Calculate the average for each exam and store it in a 1-D array that is accessible in the main program Each function takes the same arguments: The 2-D array The # of students in the class The 1-D array that will be used to hold the averages 6/12/2018 ECE Application Programming: Lecture 32

ECE Application Programming: Lecture 32 Example solution void studentAvg(double grades[][3], int nStudents, double averages[]) { int i, j; // Row/column # /* Go through each row, sum all columns, and divide by 3 to get each student’s avg */ for (i = 0; i < nStudents; i++) { averages[i] = 0; // Initialize sum for (j = 0; j < 3; j++) { averages[i] += grades[i][j]; } averages[i] /= 3; 6/12/2018 ECE Application Programming: Lecture 32

Example solution (cont.) void examAvg(double grades[][3], int nStudents, double averages[]) { int i, j; // Row/column # /* Go through each column, sum all rows, and divide by nStudents to get each exam avg */ for (j = 0; j < 3; j++) { averages[j] = 0; // Initialize sum for (i = 0; i < nStudents; i++) { averages[j] += grades[i][j]; } averages[j] /= nStudents; 6/12/2018 ECE Application Programming: Lecture 32

ECE Application Programming: Lecture 32 Next time File I/O 6/12/2018 ECE Application Programming: Lecture 32