1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm.
Searching Arrays Linear search Binary search small arrays
Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project.
CSE202: Lecture 14The Ohio State University1 Arrays.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CS 1430: Programming in C++.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Objectives You should be able to describe: One-Dimensional Arrays
CS Computer Science IA: Procedural Programming
The Ohio State University
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
New Structure Recall “average.cpp” program
Quiz Next Monday.
Data type List Definition:
Review for Final Exam.
File Review Declare the File Stream Object Name
Arrays Syntax: type variableName[size];
CS150 Introduction to Computer Science 1
Review for Final Exam.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm

2 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.)

3 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].

4 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

5 Function Pass by Value #include 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); }

6 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!

7 displayChar() Function void displayChar(char charPassed, int times) { int i; for(i = 0; i < times; i++) cout << charPassed; }

8 File Review (Question: What does this program do?) #include 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(); }

9 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: type variableName[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

10 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)

11 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.)

12 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] << " "; }

13 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;

14 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.)

15 A driver program calls a routine for testing purposes. "Driver" Program 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; }

16 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].

17 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.

18 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; }