Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger & Peilong Li Spring 2016 Lecture 20 Arrays and functions
2
ECE Application Programming: Lecture 20
Lecture outline Announcements/reminders Program 6 due 3/28 Exam 2 in class 3/30 Will be allowed one double-sided 8.5” x 11” note sheet Review One-dimensional arrays Two-dimensional arrays Today’s lecture Arrays and functions 4/21/2019 ECE Application Programming: Lecture 20
3
ECE Application Programming: Lecture 20
Review: arrays Arrays: groups of data with same type x[10] has 10 elements, x[0] through x[9] Can also define with initial values e.g. double list[] = {1.2, 0.75, }; Compiler will determine size of array from list If initialization list has fewer values than size given, remaining values = 0 i.e. int list[5] = {1, 2, 3} same as int list[5] = {1, 2, 3, 0, 0} Must be sure to access inside bounds You can access x[12] or x[-1], for example Will access whatever’s at those locations 4/21/2019 ECE Application Programming: Lecture 20
4
ECE Application Programming: Lecture 20
Review: 2D arrays Declared similarly to 1D arrays Example (see below): int x[3][4]; Index elements similarly to 1-D arrays Initialize: int y[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; Typically used with nested for loops 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] 4/21/2019 ECE Application Programming: Lecture 20
5
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; } 4/21/2019 ECE Application Programming: Lecture 20
6
ECE Application Programming: Lecture 20
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]++; 4/21/2019 ECE Application Programming: Lecture 20
7
Passing arrays to functions
Do not need to specify array size (for reasons I’ll explain shortly) Compiler will actually ignore 1-D array size, even if you put it in prototype Therefore cannot check array size inside function Prototype typically has array name and brackets to indicate you’re dealing with array e.g. int findAvg(int arr[ ], int n); n = # elements in array 4/21/2019 ECE Application Programming: Lecture 20
8
ECE Application Programming: Lecture 20
Final notes Next time (M 3/21) Finish discussing arrays and functions Character arrays and strings Enjoy your break! Reminders: Program 6 due 3/28 Exam 2 in class 3/30 Will be allowed one double-sided 8.5” x 11” note sheet 4/21/2019 ECE Application Programming: Lecture 20
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.