Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE Application Programming

Similar presentations


Presentation on theme: "ECE Application Programming"— Presentation transcript:

1 16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Summer 2012 Lecture 8 1D and 2D arrays Arrays, pointers, and functions Exam 2 Review

2 ECE Application Programming: Lecture 8
Lecture outline Announcements/reminders Program 6 due today Lecture this Friday (8/10) Program 7 due Friday, 8/10 Program 8 due Tuesday, 8/14 Exam 3 Thursday, 8/16 Today’s lecture: arrays One-dimensional arrays Arrays, pointers, and functions Two-dimensional arrays 5/30/2018 ECE Application Programming: Lecture 8

3 Review of scalar variables
Variables (up to now) have: name type (int, float, double, char) address value N 28C4 (int) q 28C8 (float) r 28CC (float) 35 3.14 8.9 e.g. Name type address value N integer 28C4 35 q float 28C8 3.14 r float 28CC 8.9 5/30/2018 ECE Application Programming: Lecture 8

4 ECE Application Programming: Lecture 8
Intro to Arrays Any single element of x may be used like any other scalar variable x[0] 45 3044 x[1] 55 3048 x[2] 25 304C x[3] 85 3050 x[4] 75 3054 printf("%d",x[3]); // prints 85 printf("%d",x[7]+x[1]); // prints 115 x[5] 65 3058 x[6] 100 305C x[7] 60 3060 5/30/2018 ECE Application Programming: Lecture 8

5 ECE Application Programming: Lecture 8
Declaring Arrays Define an 8 element array: int x[8]; Elements numbered 0 to 7 Arrays in C always start with location 0 (zero based) The initial value of each array element is unknown (just like scalar variables) x[0] ? 3044 x[1] ? 3048 x[2] ? 304C x[3] ? 3050 x[4] ? 3054 x[5] ? 3058 x[6] ? 305C x[7] ? 3060 5/30/2018 ECE Application Programming: Lecture 8

6 Declaring/defining Arrays
double A[]={ 1.23, , 2.718, }; A[0] 1.23 4430 You can also define the values to be held in the array and instruct the compiler to figure out how many elements are needed. Not putting a value within the [] tells the compiler to determine how many locations are needed. A[1] 4438 A[2] 2.718 4440 A[3] 0.7071 4448 5/30/2018 ECE Application Programming: Lecture 8

7 Working with Arrays (input)
#include <stdio.h> void main(void) { int x[8]; int i; // get 8 values into x[] for (i=0; i<8; i++) { printf("Enter test %d:",i+1); scanf("%d",&x[i]); } } 5/30/2018 ECE Application Programming: Lecture 8

8 Working with Arrays (input)
Sample run (user input underlined): Enter test 1:80 Enter test 2:75 Enter test 3:90 Enter test 4:100 Enter test 5:65 Enter test 6:88 Enter test 7:40 Enter test 8:90 x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 5/30/2018 ECE Application Programming: Lecture 8

9 ECE Application Programming: Lecture 8
Pitfalls What happens if we change previous code to: #include <stdio.h> void main(void) { int x[8]; int i; float sum, avg; // used later // get 8 values into x[] for (i=0; i<=8; i++) { printf("Enter test %d:",i+1); scanf("%d",&x[i]); } } 5/30/2018 ECE Application Programming: Lecture 8

10 ECE Application Programming: Lecture 8
Pitfalls (cont.) Although x has 8 elements, x[8] is not one of those elements! Compiler will not stop you from accessing elements outside the array Must make sure you know the size of the array 5/30/2018 ECE Application Programming: Lecture 8

11 ECE Application Programming: Lecture 8
Example What does the following program print? int main() { int arr[10]; int i; printf("First loop:\n"); for (i = 0; i < 10; i++) { arr[i] = i * 2; printf("arr[%d] = %d\n", i, arr[i]); } printf("\nSecond loop:\n"); for (i = 0; i < 9; i++) { arr[i] = arr[i] + arr[i + 1]; return 0; 5/30/2018 ECE Application Programming: Lecture 8

12 ECE Application Programming: Lecture 8
Example solution First loop: arr[0] = 0 arr[1] = 2 arr[2] = 4 arr[3] = 6 arr[4] = 8 arr[5] = 10 arr[6] = 12 arr[7] = 14 arr[8] = 16 arr[9] = 18 Output continued: Second loop: arr[0] = 2 arr[1] = 6 arr[2] = 10 arr[3] = 14 arr[4] = 18 arr[5] = 22 arr[6] = 26 arr[7] = 30 arr[8] = 34 5/30/2018 ECE Application Programming: Lecture 8

13 Passing arrays to functions
Do not need to specify array size (for reasons I’ll explain shortly) Compiler will actually ignore 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[ ]); 5/30/2018 ECE Application Programming: Lecture 8

14 ECE Application Programming: Lecture 8
Example Write a function for each of the following Given an array of doubles (arr) and the # of elements in the array (n), find the average of all array elements Given an array of ints (arr) and the # of elements (n), find the largest element in the array Given an array of test scores (tests), the # of elements in the array (n), and an amount to scale those scores by (s), add s to every element in tests Do not print scores in function; we’ll print in main program 5/30/2018 ECE Application Programming: Lecture 8

15 Passing Arrays to functions (findAvg)
//******************************************* // function findAvg // On Entry: // arr[] - array with values to avg // n number of values to avg // On Exit: // returns avg of first n elements of test[] double findAvg(double arr[], int n) { int i; double sum=0; double avg; for (i=0; i<n; i++) sum+=arr[i]; avg = sum / n; return avg; } 5/30/2018 ECE Application Programming: Lecture 8

16 Working with Arrays (find biggest)
// WAY 1-initialize biggest one so // far to zero big=0; for (i=0; i<8; i++) { // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } } printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ??? What happens if there are only negative values in the array ??? 5/30/2018 ECE Application Programming: Lecture 8

17 Working with Arrays (find biggest)
// WAY 2-initialize biggest one so // far to smallest possible value big= ; for (i=0; i<8; i++) { // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } } printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ???What happens if program is ported to another system??? ??? (where integers are stored in 2 or 8 bytes)??? 5/30/2018 ECE Application Programming: Lecture 8

18 Working with Arrays (find biggest)
// WAY 3-initialize biggest one so // far to smallest possible value big= INT_MIN; // BUT use symbol for (i=0; i<8; i++) { // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } } printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 The above works, but there is one more (slightly better) way 5/30/2018 ECE Application Programming: Lecture 8

19 Working with Arrays (find biggest)
// WAY 4-initialize biggest one so // far to the first array element big= x[0]; for (i=1; i<8; i++) // start at 1 { // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } } printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ----- 5/30/2018 ECE Application Programming: Lecture 8

20 Passing Arrays to functions (findBig)
//******************************************* // function findBig // On Entry: // arr[] - array with values // n number of elements to examine // On Exit: // returns biggest value in the first n // elements of test[] int findBig(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/30/2018 ECE Application Programming: Lecture 8

21 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 tests[], int n, int s) { int i; for (i=0; i<n; i++) test[i]=test[i]+s; // or use test[i]+=s; } 5/30/2018 ECE Application Programming: Lecture 8

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

23 Passing Arrays to functions (SclAry)
Output of program: For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; ??? What's wrong with this picture ??? 5/30/2018 ECE Application Programming: Lecture 8

24 Passing Arrays to functions (SclAry)
Output of program: For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; ??? What's wrong with this picture ??? The array in the main program was UPDATED ... (say "Hmmmm") Does this seem contrary to all we know about functions? (say "Yes") Is this how it really works? (Yep, it is) Is your head getting ready to explode? (say "Almost") ??? SO WHAT IS GOING ON ??? 5/30/2018 ECE Application Programming: Lecture 8

25 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/30/2018 ECE Application Programming: Lecture 8

26 ECE Application Programming: Lecture 8
Arrays and pointers Array name is a pointer to first array element Can use pointers and arrays interchangeably You can use [] to “index” a pointer Example: int myArr[] = {1, 3, 5, 7, 9}; int *aPtr; aPtr = myArr; for(int i =0; i < 5; i++) printf(“%d”, aPtr[i]); What does this print?  contents of array! 5/30/2018 ECE Application Programming: Lecture 8

27 ECE Application Programming: Lecture 8
Pointer arithmetic When using pointers/arrays interchangeably, can make use of pointer arithmetic Can’t change where array name points, but you can change pointer If p is a pointer, p++ means “point to next element” “Next element” determined by base type Can compare pointers p == NULL  pointer points nowhere p == q  p and q point to same location Example int num[4] = {1,2,3,4}, *p; p = num; //same as p = &num[0]; printf(“%d\n”, *p); ++p; 5/30/2018 ECE Application Programming: Lecture 8

28 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] 5/30/2018 ECE Application Programming: Lecture 8

29 ECE Application Programming: Lecture 8
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/30/2018 ECE Application Programming: Lecture 8

30 ECE Application Programming: Lecture 8
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/30/2018 ECE Application Programming: Lecture 8

31 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/30/2018 ECE Application Programming: Lecture 8

32 ECE Application Programming: Lecture 8
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/30/2018 ECE Application Programming: Lecture 8

33 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); ... } 5/30/2018 ECE Application Programming: Lecture 8

34 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 5/30/2018 ECE Application Programming: Lecture 8

35 ECE Application Programming: Lecture 8
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; 5/30/2018 ECE Application Programming: Lecture 8

36 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; 5/30/2018 ECE Application Programming: Lecture 8

37 Exam 2 stats & grade distribution
Question-by-question averages: Q1: 14.2 / 20 (71%) Q2: 26.3 / 40 (66%) Q3: 23.7 / 40 (59%) Extra credit 6 students completed 6.1 average Average: 64.2 Median: 69 Std. deviation: 22.8 Max: 93 5/30/2018 ECE Application Programming: Lecture 8

38 ECE Application Programming: Lecture 8
Next time Character arrays and strings PE5: Arrays and strings Reminders Program 6 due today Lecture this Friday (8/10) Program 7 due Friday, 8/10 Program 8 due Tuesday, 8/14 Exam 3 Thursday, 8/16 5/30/2018 ECE Application Programming: Lecture 8


Download ppt "ECE Application Programming"

Similar presentations


Ads by Google