EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UNIT 10 Multidimensional Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE 264 Object-Oriented Software Development
ECE Application Programming
ECE Application Programming
Programming application CC213
Computer Programming BCT 1113
ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
ECE Application Programming
EKT150 : Computer Programming
4.9 Multiple-Subscripted Arrays
Functions with arrays.
Beginning C for Engineers
Multidimensional Arrays
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
Arrays Arrays A few types Structures of related data items
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
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ Array 1.
EECE.2160 ECE Application Programming
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.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.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2018 Lecture 22 Arrays and functions

ECE Application Programming: Lecture 22 Lecture outline Announcements/reminders Program 5 due 11/2  11/7 Exam 2 in class Monday, 11/5 Will cover lectures 14-24 (except lecture 16) Lec. 25: Exam 2 Preview (Fri. 11/2) Today’s lecture Review: one- and two-dimensional arrays Arrays and functions 5/14/2019 ECE Application Programming: Lecture 22

ECE Application Programming: Lecture 22 Review: arrays Arrays: groups of data with same type int x[10] has 10 elements, x[0] through x[9] Can also define with initial values e.g. double list[] = {1.2, 0.75, -3.233}; 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 5/14/2019 ECE Application Programming: Lecture 22

Review: 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 Typically use nested for loops to access 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] 5/14/2019 ECE Application Programming: Lecture 22

Review: 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 5/14/2019 ECE Application Programming: Lecture 22

ECE Application Programming: Lecture 20 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! 5/14/2019 ECE Application Programming: Lecture 20

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; } 5/14/2019 ECE Application Programming: Lecture 20

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]++; 5/14/2019 ECE Application Programming: Lecture 20

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 5/14/2019 ECE Application Programming: Lecture 22

ECE Application Programming: Lecture 22 Example Write a function for each of the following findAvg(): Given an array of doubles (arr) and the # of elements in the array (n), find the average of all array elements findMax(): Given an array of ints (arr) and the # of elements (n), find the largest (i.e., most positive) element in the array 5/14/2019 ECE Application Programming: Lecture 22

Passing Arrays to functions (findAvg) double findAvg(double arr[], int n) { int i; double sum = 0; for (i=0; i < n; i++) sum += arr[i]; return sum / n; } 5/14/2019 ECE Application Programming: Lecture 22

Passing Arrays to functions (findMax) int findMax(int arr[], int n) { int i, big; big = arr[0]; for (i=1; i < n; i++) { if (arr[i] > big) big = arr[i]; } return big; } 5/14/2019 ECE Application Programming: Lecture 22

ECE Application Programming: Lecture 22 SclAry() function Consider function that takes as arguments An array The array size A scaling factor to add to each element Function can’t “return” array … so is there any point to it? 5/14/2019 ECE Application Programming: Lecture 22

Passing Arrays to functions (SclAry) //******************************************* // function SclAry // On Entry: // tests[] - array with values to scale // n - number of values to scale // s - number of points to scale // On Exit: // The first n values of tests[] are // scaled by s points void SclAry(int test[], int n, int s) { int i; for (i=0; i<n; i++) test[i]=test[i]+s; // or use test[i]+=s; } 5/14/2019 ECE Application Programming: Lecture 22

Passing Arrays to functions (SclAry) #include <stdio.h> void SclAry(int test [], int n, int s); void main(void) { int i; int x[]={ 51,62,73,84,95,100,66,57,48,79 }; SclAry(x,10,10); for (i=0; i<N; i++) printf("%d ",x[i]); printf("\n"); } void SclAry(int test[], int n, int s) { int i; for (i=0; i<n; i++) test[i]=test[i]+s; // or use test[i]+=s; } 5/14/2019 ECE Application Programming: Lecture 22

ECE Application Programming: Lecture 22 Passing Arrays to functions (SclAry) Output of program: 61 72 83 94 105 110 76 67 58 89 For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; Function call changed array—why? 5/14/2019 ECE Application Programming: Lecture 22

Passing Arrays to functions Before call to SclAry After call to SclAry test[0] 51 3044 test[0] 61 3044 test[1] 62 3048 test[1] 72 3048 test[2] 73 304C test[2] 83 304C test[3] 84 3050 test[3] 94 3050 test[4] 95 3054 test[4] 105 3054 test[5] 100 3058 test[5] 110 3058 test[6] 66 305C test[6] 76 305C test[7] 57 3060 test[7] 67 3060 test[8] 48 3064 test[8] 58 3064 test[9] 79 3068 test[9] 89 3068 Passing the name only (i.e. test vs. test[4]) passes the ADDRESS of element zero of the array. Put another way: myfunc(ary) same as myfunc (&ary[0]) 5/14/2019 ECE Application Programming: Lecture 22

ECE Application Programming: Lecture 22 Final notes Next time Character arrays and strings Reminders: Program 5 due 11/2  11/7 Exam 2 in class Monday, 11/5 Will cover lectures 14-24 (except lecture 16) Lec. 25: Exam 2 Preview (Fri. 11/2) 5/14/2019 ECE Application Programming: Lecture 22