Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 24 Arrays and functions

2 ECE Application Programming: Lecture 24
Lecture outline Announcements/reminders Program 5 due 4/5 Today’s lecture Arrays and functions Character arrays and strings 4/25/2019 ECE Application Programming: Lecture 24

3 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/25/2019 ECE Application Programming: Lecture 24

4 ECE Application Programming: Lecture 24
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 4/25/2019 ECE Application Programming: Lecture 24

5 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; } 4/25/2019 ECE Application Programming: Lecture 24

6 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; } 4/25/2019 ECE Application Programming: Lecture 24

7 ECE Application Programming: Lecture 24
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? 4/25/2019 ECE Application Programming: Lecture 24

8 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; } 4/25/2019 ECE Application Programming: Lecture 24

9 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 < 10; 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; } 4/25/2019 ECE Application Programming: Lecture 24

10 ECE Application Programming: Lecture 24
Passing Arrays to functions (SclAry) Output of program: For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; Function call changed array—why? 4/25/2019 ECE Application Programming: Lecture 24

11 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]) 4/25/2019 ECE Application Programming: Lecture 24

12 ECE Application Programming: Lecture 24
Strings in C Strings in C: null-terminated arrays of characters “Hello”{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, 0} Null character = 0 = ‘\0’ Can declare array to hold string Need space to hold null: char hello[5] would be too small Can use string constants to directly initialize char hello[] = “Hello”; Equivalent to: char hello[6]; hello[0] = ‘H’; hello[1] = ‘e’; hello[2] = ‘l’; hello[3] = ‘l’; hello[4] = ‘o’; hello[5] = OR-- hello[5] = ‘\0’; 4/25/2019 ECE Application Programming: Lecture 24

13 Strings and I/O functions
Can pass string as array or pointer: char * printf(), scanf() take char * as first argument Given string char hello[] from previous slide: Print directly: printf(hello); Print w/formatting using %s: printf(“%s\n”, hello); Print individual character: printf(“%c\n”, hello[1]); Overwrite with new string: scanf(“%s”, hello); Ampersand is unnecessary  array name is already address scanf() will read up to first whitespace character 4/25/2019 ECE Application Programming: Lecture 24

14 ECE Application Programming: Lecture 24
Final notes Next time More on strings –or– Exam 2 Review Reminders: Program 5 due 4/5 4/25/2019 ECE Application Programming: Lecture 24


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google