lecture12-feb16 - Announcements Lecture 12 Assignment 3 out...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Announcements Lecture 12 • Assignment 3 – out today – designs due Friday in recitation • Inspect your algorithm looking for potential sub-processes that have to be done more than once; or which naturally seem to be an identifiable subtask (chunking) • Exam 1 on Friday – Sample exam is on the BB – Be on time!!, bring your student ID, pencil(s), – Bring Assn 2 data • SLOCs(#statements), hours, logic defects • Topics for today – More on functions (and arrays) – Modular sorting program example – Recursive functions /* average is a function that computes and returns the average of the values in a given (passed) integer array of given length */ double average (int data [ ] , int length) { if (length = = 0) return 0; double sum = 0; int i; for (i = 0; i < length; i++) sum = sum + data[i]; return sum / length; } Passing an Array as an Argument data exam1scores Array names are passed as a pointer Called from your main function, e.g. double avg; int exam1scores [CLASS_SIZE]; /* assume that the array gets its values somehow */ avg = average (exam1scores, CLASS_SIZE); • /* This function fills in an integer array with random values, each in the range from 0 to n-1 */ void randomData (int data [ ], int length, int n) {int i; for (i = 0; i < length; i++) data[i] = rand ( ) % n; return; } • Called in the main for example as randomData (exam1scores, CLASS_SIZE, 101); Returning a Filled-in Array rand( ) returns a random positive int; see pg 570 Passing Multi-dimensional Arrays • Only the length of the first dimension can be omitted /* sum up the values in a n x 10 matrix of ints */ #define LEN 10 . . . int sum_array(int a[ ] [LEN], int n) { int i, j, sum = 0; for (i = 0; i < n; i++) { for (j = 0; j < LEN; j++) {sum = sum + a[ i ] [ j ] ;} } return sum; } Max Selection Sort - Algorithm Goal - Change an unordered array into an ordered array. Precondition: Given an unordered array named A of length n Postcondition: The array A will now have the values sorted into descending numerical order. Algorithm: 1. Find the largest value among the elements A[0]. .A[n-1] and identify it’s position as max 2. Swap the values of A[0] and A[max] so A[0] now contains the largest value, now A[1]. .A[n-1] are not sorted 3. Next find the largest value among the remaining elements A[1]. .A[n-1] and identify it’s position as max 4. Swap A[1] and A[max] so A[1] contains the second largest value, now A[2]. .A[n-1] are not sorted 5. Repeat this process similarly starting at A[2], A[3], and so on until the final element is reached. The array is now sorted. Exercises
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/24/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 4

lecture12-feb16 - Announcements Lecture 12 Assignment 3 out...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online