Vectors and Grids Eric Roberts CS 106B April 8, 2009.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
1 Lecture 10 Chapter 7 Functions Dale/Weems/Headington.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
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.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Basic Elements of C++ Chapter 2.
Java Unit 9: Arrays Declaring and Processing Arrays.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Chapter 7 Functions.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
259 Lecture 13 Spring 2013 Advanced Excel Topics – Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Pointers OVERVIEW.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Collection Classes Eric Roberts CS 106B January 14, 2013 (Part 1: Vectors, Grids, Stacks, and Queues)
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
Data Structures & Algorithms
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Arrays.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Functions and Libraries. Reference parameters void setToZero(int var) { var = 0; } int main() { int var = 0; setToZero(var); cout
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
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)
Collection Classes Eric Roberts CS 106B January 12, 2015 (Part 1: Vectors, Grids, Stacks, and Queues)
User-Written Functions
Functions in C++ Eric Roberts CS 106B January 7, 2015.
Vectors and Grids Chris Piech CS 106B Lecture 2 Jan 9, 2015.
Two Dimensional Arrays
Presentation transcript:

Vectors and Grids Eric Roberts CS 106B April 8, 2009

A Heart-to-Heart Talk about Exams I’ve read enough of the warm-up messages coming in for Assignment #1 to know that there is a lot of fear and loathing about exams, and a wish that things could be different. One comment that seems fairly representative is > I loved the class but really disliked the way we were > graded... having to write code for the midterm and > final did not successfully test my knowledge of the > material at all. My immediate response is: “Be careful what you wish for.” We experimented with laboratory exams for a while in the mid-1990s. We abandoned the experiment mostly because the failure rate was way too high. But why not have a take-home exam or just grade CS106 on the homework assignments? Well, there’s a problem... But why not have a take-home exam or just grade CS106 on the homework assignments?

A Modest Proposal For many years, I’ve been thinking about the possibility of reducing the weight of the final if we can do something collectively about the Honor Code problem. Here is my proposal: The weight of the final will be 15% + 5% for each Honor Code case filed this quarter The weight assigned to the homework will be whatever is left after the announced weights are assigned to the various other components, subject to a minimum of 15%. Thus, if no Honor Code cases come up this quarter, the final will count for 15%, which is exactly the same as the midterm. The homework would count for 60%. If we file three Honor Code cases (as we did last quarter), the final will count for 30% and the homework for 45%. And so on...

The Collection Classes For the next three days, we will be learning about the classes in Chapter 4. These classes, for the most part, contain other objects and are called container or collection classes. Here are some general guidelines for using these classes: –These classes represent abstract data types whose details are hidden. –Each class requires a type specification as described on the next slide. –Declaring variables of these types always invokes a constructor. –Any memory for these objects is freed when its declaration scope ends. –Assigning one value to another copies the entire structure. –To avoid copying, these structures are usually passed by reference.

Template Classes The collection classes are implemented using the template facility in C++, which makes it possible for an entire family of classes to share the same code. Instead of using the class name alone, the Vector, Grid, Stack, Queue, and Map classes require a type parameter that specifies the element type. Thus, Vector represents a vector of integers and Grid represents a two- dimensional array of characters. It is possible to nest classes, so that, for example, you could use the following definition to represent a list of chess positions: Vector > chessPositions;

Simple Call-by-Reference Example /* * Function: SolveQuadratic * Usage: SolveQuadratic(a, b, c, x1, x2); * * This function solves a quadratic equation. The coefficients * are supplied as the arguments a, b, and c, and the roots are * returned in x1 and x2, which are reference parameters. */ void SolveQuadratic(double a, double b, double c, double & x1, double & x2) { if (a == 0) Error("The coefficient a must be nonzero"); double disc = b * b - 4 * a * c; if (disc < 0) Error("The solutions are complex numbers"); double sqrtDisc = sqrt(disc); x1 = (-b + sqrtDisc) / (2 * a); x2 = (-b - sqrtDisc) / (2 * a); }

Methods in the Vector Classes vec.size() Returns the number of elements in the vector. vec.isEmpty() Returns true if the vector is empty. vec[i] Selects the i th element of the vector. vec.add(value) Adds a new element to the end of the vector. vec.insertAt(index, value) Inserts the value before the specified index position. vec.removeAt(index) Removes the element at the specified index. vec.clear() Removes all elements from the vector.

The ReadTextFile Function /* * Reads an entire file into the Vector supplied * by the user. */ void ReadTextFile(ifstream & infile, Vector & lines) { while (true) { string line; getline(infile, line); if (infile.fail()) break; lines.add(line); }

AskUserForInputFile /* * Opens a text file whose name is entered by the user. If * the file does not exist, the user is given additional * chances to enter a valid file. The prompt string is used * to tell the user what kind of file is required. */ void AskUserForInputFile(string prompt, ifstream & infile) { while (true) { cout << prompt; string filename = GetLine(); infile.open(filename.c_str()); if (!infile.fail()) break; cout << "Unable to open " << filename << endl; infile.clear(); }

Methods in the Grid Classes grid.numRows() Returns the number of rows in the grid. grid.numCols() Returns the number of columns in the grid. grid[i][j] Selects the element in the i th row and j th column. resize(rows, cols) Changes the dimensions of the grid and clears any previous contents.

Exercise: Crossword Numbering The main problem for today is to write the code to number a crossword grid. The grid itself comes from a text file that has spaces for blank squares and # signs for dark squares: 15x15 # # # # # # ## # ######### # ## # # # # # #

The End