180 likes | 196 Views
Prepare for your upcoming exam by reviewing array declarations, functions, loops, and file handling. Study materials and examples provided.
E N D
CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm
Exam 2 • Exam 2 Next Week • 33% of Exam in Lab • Must complete a program in lab [50 min]. • 67% of Exam in Lecture • 50 minutes • Know your programming • Study Quiz 1 and 2, Exam 1 • Know everything through today • Files!! Functions!! Loops!! • (Array declaration, Array element assignment — less emphasis; maybe 15% of Exam.)
Exam 2 • Please be sure to study: • Ch 5 Exercises 12, 16, 18 (Loops) • Ch 8 Exercise 9 (string usage) • Ch 3 Exercises 3, 5 (I/O, I/O failure) • Ch 3 Pgm'g exercises 1–3 (filestream I/O) • Ch 6 Exercises 2, 4, 5 (functions) • Ch 7 Exercises 2, 5 (functions) • Ch 10 Exercise 2 (arrays) [we'll do this tonight].
Function Review • Normally parameters are Pass by Value • A copy of the value is passed into function. • If copy is modified in function, original variable is unchanged. • Exception: Passing an array into a function
Function Pass by Value • #include <iostream> • using namespace std; • bool even(int num); • main() • { • int originalNum = 10; • bool retVal; • retVal = even(originalNum); • cout << originalNum; • } • bool even(int numPassed) • { • bool valToRet; • if (numPassed % 2 == 0) • valToRet = true; • else • valToRet = false; • numPassed = 0; • return (valToRet); • }
Display Line Function • Write a function that displays a character some number of times to the display. The function is passed the character and the number of times to display it. • 5 minutes: GO!
displayChar() Function • void displayChar(char charPassed, int times) • { • int i; • for(i = 0; i < times; i++) • cout << charPassed; • }
File Review • (Question: What does this program do?) • #include <iostream> • #include <fstream> • using namespace std; • main() • { • ofstream outputStream; //ofstream object • int num1, num2, num3; • cout << “Enter 3 Numbers for datafile: “; • cin >> num1 >> num2 >> num3; • outputStream.open(“datafile.txt”); • outputStream << num1 << “ “ • << num2 << “ “ << num3; • outputStream.close(); • }
Arrays • Why arrays? To have one name for a family of related values; we can select individual elements depending on runtime conditions. • Syntax to declare an array:typevariableName[size]; • Memory is set aside for size items of type • Each variable location in array is accessed by offsetting/indexing into the array with an integer expression. • Legitimate offsets/indexes are 0 to size-1
Array Example 1 • int values[5] = {2, 4, 6, 8, 1}; • (Declare an array with initial values) • values[0] = values[0] + values[2]; • cout << values[0]; • (Use specific elements of an array) • int i; • cin >> i >> values[i]; • (Which element to use can vary at runtime)
Array Example 2 • char lastname[100]; • (Declare a character array, uninitialized.) • lastname[0] = 'H'; • lastname[1] = 'a'; • cout << lastname[0] << lastname[1] << endl; • (Set elements of a character array and print them.) • lastname[2] = '\0'; • cout << lastname << endl; • (Printing a character array is treated like printing a string; the null character '\0' indicates the end of the “string”) • char name1[4] = {'S','a','m','\0'}; • char name2[4] = "Sam"; • cout << name1 << endl; • cout << name2 << endl; • (Character arrays can be initialized with strings.)
Array Example • // Declare an array of integers numArray of size • // ARRAY_SIZE (a constant). • // • const int ARRAY_SIZE = 10; • int numArray[ARRAY_SIZE]; • // Use a loop to set every element of numArray to its index. • // • int index; • for(index = 0; index < ARRAY_SIZE; index++) • { • numArray[index] = index; • } • // Print numArray (all on one line) • // • for (index = 0; index < ARRAY_SIZE; index++) • { • cout << numArray[index] << " "; • }
Array Example • const int ARRAY_SIZE = 10; • int index, sum = 0, average; • int numArray[ARRAY_SIZE]; • for(index = 0; index < ARRAY_SIZE; index++) • { • cout << “Enter Score ” << index << “ : “; • cin >> numArray[index]; • } • for(index = 0; index < ARRAY_SIZE; index++) • { • sum = sum + numArray[index]; • } • average = sum / ARRAY_SIZE;
Passing an Array to a Function • Pgm'g Ex 9.2: Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element of the array. Also, write a function to test your function. • E.g., given • int values[6] = { 0, -1, 4, 3, -1, 7 }; • we'd like smallestIndex(values,6) to return 2 (Why?) • Function prototype: • int smallestIndex(int a[], int len); • (At runtime, we'll use values as the value of array a.)
"Driver" Program • A driver program calls a routine for testing purposes. • int main() • { • int values[6] = { 0, -1, 4, 3, -1, 7 }; • int indexFound = smallestIndex(values, 6); • cout << "Index found: " << indexFound • << ", value: " << values[indexFound] • << endl; • return 0; • }
Think about smallestIndex • Basic idea: • Use a loop to traverse array a. • Keep track of the smallest value we've seen and its index. • Whenever we find a new smallest value, we update our variables that hold the smallest value and its index. • Initially, the smallest value is a[0] and the index of the smallest value is 0. • We have to look at elements a[1], a[2], …, a[len-1].
Pseudocode for smallestIndex • smallestValue=a[0] • smallestIndex=0 • for i = 1, 2, …, len-1 • if a[i] is smaller than smallestValue • update smallestValue=a[i] and smallestIndex=i; • otherwise we're done looking at a[i] • When loop ends, return smallestIndex.
Code for smallestIndex • // smallestIndex(a, len) searches a[0], a[1], ..., • // a[len-1] to find the smallest value; it returns • // the index of the smallest value. If the smallest • // value occurs more than once, we return the index • // of the leftmost occurrence. • // • int smallestIndex(int a[], int len) • { • int smallestValue, smallestIndex; • smallestValue = a[0]; • smallestIndex = 0; • int i; • for (i = 1; i < len ; i++) • { • if (a[i] < smallestValue) • { • smallestValue = a[i]; • smallestIndex = i; • } • } • return smallestIndex; • }