Lab 4X - Intro to MOOS Programming HINTS

Size: px
Start display at page:

Download "Lab 4X - Intro to MOOS Programming HINTS"

Transcription

1 Lab 4X - Intro to MOOS Programming HINTS Unmanned Marine Vehicle Autonomy, Sensing and Communications Spring, 2018 Michael Benjamin, mikerb@mit.edu Henrik Schmidt, henrik@mit.edu Department of Mechanical Engineering MIT, Cambridge MA Overview and Objectives 3 2 The podometry Exercise 3 3 The pprimefactor Exercise: Strongly recommended C++ Hints String Parsing String to Number, and Number to String Conversion Hint: Use STL Lists to Manage Unprocessed Entries The pprimefactor Exercise: Architecture Hints If Needed Calculating the set of Prime Factors for a Given Number Hint: Build a class to represent a prime number solution-in-progress

2 2

3 1 Overview and Objectives In the past years of 2.680, the Prime Factors lab stood out as one of the most time consuming labs of the course. But it also stood out as a lab that cemented some important understanding of how MOOS works, and what it takes to build MOOS apps. The act of building a MOOS app also helps gain insight into how existing MOOS apps work when needed in later labs. The bottom line is that we don t feel like we can remove the prime factors exercise without compromising key concepts we want you to have and you will need in later parts of the course. But we also want this lab to be more digestible in the one week you have to work on it (and other classes). So the compromise is to provide substantial hints. So, feel free to try this lab without the hints, but also feel free to use this lab addendum to guide your work. Even though these hints were not provided in previous years, much of the insight below was indeed discussed at the white board to help students along in previous years. 2 The podometry Exercise The only gotcha to be aware of in this exercise is that there are perhaps several updates to NAV X and NAV Y each time you read your mail. Each new position represents a new leg. Make sure your odometry calculation calculates each leg. 3 The pprimefactor Exercise: Strongly recommended C++ Hints 3.1 String Parsing In this lab you will need to do some fairly routing string parsing, using some tools that you may not want to re-invent from scratch. See the course wiki LabUpdates page for some tips on string parsing. Namely the below three functions: vector<string> parsestring(string, char); string bitestring(string, char); string stripblankends(string); They can be found here: String to Number, and Number to String Conversion In this lab you will need to perform some conversions between strings and numerical values. To represent a 64 bit integer you will want to use the type unsigned long int x = 0; In this lab, if you use the utimerscript script provided in the lab, the numbers will be posted to the variable NUM VALUE as string value. For example if the posting were: NUM VALUE=" ". To convert from the string, str=" " to an unsigned long int, use: 3

4 #include <cstdlib> // for the strtoul() function unsigned long int x = strtoul(str.c_str(), NULL, 0); In the other direction, converting numerical values into strings, you can use the C++ stringstream for this: // Include the stringstream header file #include <sstream>... // Create the biggest unsigned long int possibe unsigned long int ival = ; // Convert it to a string stringstream ss; ss << ival; string str = ss.str(); // Confirm it works cout << "Value:" << str << endl; 3.3 Hint: Use STL Lists to Manage Unprocessed Entries In this lab a key feature of the prime factorization handler is that it be able to handle (accept, solve and post) shorter numbers while the application may be still working on larger numbers. Here are two hints you may wish to consider: Create your own little C++ class for defining an entry. This may include the prime itself, the time received, partial results, and an indication of where the solution algorithm left off the last time this prime was worked on. See Section 4.2 below. Use an STL list (or similarly capable data structure if you want) for handling entries. The STL list is nice since it allows insertions at both ends, and deletions from both ends and the middle. Since lists are doubly linked, deletions from the middle are efficient, O(1), unlike deletions from the middle of say vectors. Review of Simple Usage of a List of Strings The first snippet below just reminds us how to declare, populate and iterate through an STL list of strings: 1 #include <list> 2 #include <string> 3 4 list<string> my_strings; 5 my_strings.push_back("apples"); 6 my_strings.push_back("watermelons"); 7 my_strings.push_back("pears"); 8 my_strings.push_back("blackberry"); 9 10 list<string>::iterator p; 4

5 11 for(p=my_strings.begin(); p!=my_strings.end(); p++) { 12 string str = *p; 13 if(str.length() > 6) 14 cout << "Long fruit name: " << str << endl; 15 } 16 cout << "List size: " << my_strings.size() << endl; The code above does not modify the list once it s been built, but detects list entries with string length longer than six characters. It should produce the output: Long fruit name: watermelons Long fruit name: blackberry List size: 4 Removing a List Item While Iterating Through the List Although the documentation for lists is quite good in most C++ text books or at cplusplus.com, the syntax for deletion of list items from the middle of the list, while iterating through may be a bit tricky so we cover it here. The next code segment is similar to the above example, but this time it removes elements of the list with string length greater than six: 1 #include <list> 2 #include <string> 3 4 list<string> my_strings; 5 my_strings.push_back("apples"); 6 my_strings.push_back("watermelons"); 7 my_strings.push_back("pears"); 8 my_strings.push_back("blackberry"); list<string>::iterator p; 11 for(p=my_strings.begin(); p!=my_strings.end(); ) { 12 string str = *p; 13 if(str.length() > 6) { 14 cout << "Removing long fruit: " << str << endl; 15 p = my_strings.erase(p); 16 } 17 else 18 ++p; 29 } 20 cout << "List size: " << my_strings.size() << endl; Note how line 11 has changed between the two listings. The p++ in the first listing is left out in the second listing. The iterator instead is incremented on line 18. This code should produce the below output. Removing long fruit: watermelons Removing long fruit: blackberry List size: 2 5

6 Accessing a Reference to a List Element vs a Copy In the above two examples, on line 12, the first thing done while iterating through the list of strings is to derefernce the contents of the iterator. The line: string str = *p; makes a copy of the string in the list. Let s suppose we wanted modify the strings in the list. For example we may want to go through the list of strings and truncate to ten characters, all strings greater than ten characters. You might be inclined to try something like: list<string>::iterator p; for(p=my_strings.begin(); p!=my_strings.end(); ) { string str = *p; if(str.length() > 10) { str = str.substr(0,10); } } The above will not work because we truncated a copy of the string in the list. This original list of strings may still contain strings of length greater than ten. You instead need to use the line: string& str = *p; The extra ampersand now means that str is the element/string in the list, not a copy of it. 4 The pprimefactor Exercise: Architecture Hints If Needed Sometimes the best strategy in building something complex is the identification of relatively easier distinct stages where each stage is (a) verifiable, (b) closer to the overall solution, and (c) doesn t require a bunch of re-working between each stage. With that in mind, we offer four distinct stages for the Prime Factors exercise. A non-moos C++ program to simply calculate the prime factors of a given number. A MOOS app to read the given number from mail, but each new number has its prime factors calculated completely before handling the next number. A MOOS app that reads a give number from its mail, puts it into a queue during the OnNewMail() mail handling method, and then handles one number at a time from the queue during the Iterate() method. Each number again is calculated to completion before proceeding to the next The final stage. Same as before, but each number on the queue is processed only a fixed number of steps. If all the primes are found within this fixed number of steps, it is removed from the queue, otherwise it is left on the queue until the next time the Iterate() method is invoked. But presumably new numbers may be added in the meanwhile as the OnNewMail() function is once again invoked. 6

7 4.1 Calculating the set of Prime Factors for a Given Number You can find several descriptions on the web of the algorithm for calculating the prime factors of a given number. We describe it in our own words here, partly because we want to emphasize the notion of partially calculating the answer. Initially this isn t our focus, but later on it will be important - we will want a algorithm that we can pause and resume where we left off. First, let s discuss the notion of the problem state. The state consists of two parts: A list L of prime factors, initially empty, A number N with a perhaps presently unknown set of prime factors. As the algorithm progresses, the state will evolve with the list L growing and the number N shrinking until L holds the list of prime factors, and N is 1. For example: L = {}, N=2310 <- Initial condition with input 2310 L = {2}, N=1155 L = {2,3}, N=385 L = {2,3,5}, N=77 L = {2,3,5,7}, N=11 L = {2,3,5,7,11}, N=1 <- Final condition, solution {2,3,5,7,11} How do you find the next prime factor at each stage? One simple way to do this is to iterate between i = 2 and the square root of N and checking if i divides evenly into N. Checking if a number divides evenly into another number, in C++, is done with the modulo operator % (a % b == 0) // if b divides evenly into a Going beyond the square root is redundant. For example checking whether (35%7)==0 would never be necessary since (35%5)==0 would have been caught first as i iterates higher and hits 5 before 7. You may notice that the simple for-loop has some inefficiencies in it. For example, checking if (N%4)==0 is not necessary if we already know (N%2)!=0. In fact that for-loop should only include prime numbers i=2,3,5,7,11,13... rather than i=2,3,4,5,6,7... Here it may become tempting to keep a look-up table of small primes to make your for-loop faster. Resist this temptation. This isn t what the lab is about, and it s not the huge performance boost that it would seem at the outset. This key aspect here for our lab is the idea of iterating step-by-step through a for-loop. Think of a step in the above algorithm as a single a%b calculation. Rather than proceeding from 2 to the square root of N without pause, the algorithm could instead be paused along the way, stopping at some number k between 2 and the square root of N. When the calculation resumes, the for-loop simply picks up where we left off. Normally we wouldn t be interested in calculating things in this manner, but this approach allows us to pause our work on big potentially-hard-to-solve numbers, to quickly handle an easy number that has arrived on the scene while working on that big number. Recursion This algorithm naturally lends itself to a recursive solution. A recursive algorithm is just one that calls itself as part of the algorithm, with a condition for termination. A well known one 7

8 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... Fib(n) = Fib(n-1) + Fib(n-2) Fib(1) = 0 Fib(2) = 1 Primes(n) = k + Primes(n/k), if n is composite Primes(n) = n, if n is prime 4.2 Hint: Build a class to represent a prime number solution-in-progress It will be helpful to build a C++ class to store instances of partially calculated entries. One example of this class is given below, leaving the class implemenation up to you. Key parts of this class are: A holder of the original prime number (m orig), A holder of prime factors found so far (m factors), The index at which it was received (m received index), The index at which it was solved (m calculated index), The index in the for-loop to start searching for prime (m start index), A function for finding the next prime, given a maximum number of steps to search (factor()), A function for generating a string report of the results (getreport()), 8

9 class PrimeEntry { public: PrimeEntry(); ~PrimeEntry() {}; void setoriginalval(unsigned long int v); void setreceivedindex(unsigned int v) {m_received_index=v;}; void setcalculatedindex(unsigned int v) {m_calculated_index=v;}; void setdone(bool v) {m_done=v;}; bool bool done() {return(m_done);}; factor(unsigned long int max_steps); std::string getreport(); protected: unsigned long int unsigned long int bool unsigned int unsigned int m_start_index; m_orig; m_done; m_received_index; m_calculated_index; std::vector<unsigned long int> m_factors; }; #endif Your MOOS app may contain a list of such elements. It will grow as new mail comes in. And it will shrink during the Iterate() loop as work is done on each element. The Iterate() loop should try to perform some number of steps on each element, then scan the list for those marked as done. For any done element, remove it from the list, and generate a posting to the MOOSDB of the results. 9

Lab 5 - MOOS Programming - Calculating Prime Factors

Lab 5 - MOOS Programming - Calculating Prime Factors Lab 5 - MOOS Programming - Calculating Prime Factors 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications Feb 22nd, 2018 Michael Benjamin, mikerb@mit.edu Henrik Schmidt, henrik@mit.edu Department

More information

Lab 4 - Introduction to MOOS Programming

Lab 4 - Introduction to MOOS Programming Lab 4 - Introduction to MOOS Programming 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications February 15th, 2018 Michael Benjamin, mikerb@mit.edu Henrik Schmidt, henrik@mit.edu Department

More information

C++ Lab 02 - Command Line Arguments and Strings

C++ Lab 02 - Command Line Arguments and Strings C++ Lab 02 - Command Line Arguments and Strings 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications Spring 2015 Michael Benjamin, mikerb@mit.edu Department of Mechanical Engineering Computer

More information

C++ Lab 04 - File I/O, Arrays and Vectors

C++ Lab 04 - File I/O, Arrays and Vectors C++ Lab 04 - File I/O, Arrays and Vectors 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications Spring 2018 Michael Benjamin, mikerb@mit.edu Department of Mechanical Engineering Computer Science

More information

C++ Lab 03 - C++ Functions

C++ Lab 03 - C++ Functions C++ Lab 03 - C++ Functions 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications Spring 2018 Michael Benjamin, mikerb@mit.edu Department of Mechanical Engineering Computer Science and Artificial

More information

Lab 2 - Introduction to C++

Lab 2 - Introduction to C++ Lab 2 - Introduction to C++ 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications February 8th, 2018 Michael Benjamin, mikerb@mit.edu Henrik Schmidt, henrik@mit.edu Department of Mechanical

More information

Help Topic: MOOS-IvP String Utilities

Help Topic: MOOS-IvP String Utilities Help Topic: MOOS-IvP String Utilities Spring 2018 Michael Benjamin, mikerb@mit.edu Department of Mechanical Engineering, CSAIL MIT, Cambridge MA 02139 MOOS-IvP String Utilities The below describe a set

More information

Lab Instructor : Jean Lai

Lab Instructor : Jean Lai Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.

More information

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Monday, February 10th, 2014 from 6-7:50pm, Lab sections 1-5 and

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

C++ Lab 08 - Class Inheritance in C++

C++ Lab 08 - Class Inheritance in C++ C++ Lab 08 - Class Inheritance in C++ 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications Spring 2018 Michael Benjamin, mikerb@mit.edu Department of Mechanical Engineering Computer Science

More information

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion Lecture 13 1 What is? A method of programming in which a method refers to itself in order to solve a problem Never necessary In some situations, results in simpler and/or easier-to-write code Can often

More information

Lab 14 - Introduction to Writing Behaviors for the IvP Helm

Lab 14 - Introduction to Writing Behaviors for the IvP Helm Lab 14 - Introduction to Writing Behaviors for the IvP Helm 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications April 19th 2018 Michael Benjamin, mikerb@mit.edu Henrik Schmidt, henrik@mit.edu

More information

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions User Defined Functions In previous labs, you've encountered useful functions, such as sqrt() and pow(), that were created by other

More information

Problem 1: Building and testing your own linked indexed list

Problem 1: Building and testing your own linked indexed list CSCI 200 Lab 8 Implementing a Linked Indexed List In this lab, you will be constructing a linked indexed list. You ll then use your list to build and test a new linked queue implementation. Objectives:

More information

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let

More information

Programming Iterative Loops. for while

Programming Iterative Loops. for while Programming Iterative Loops for while What was an iterative loop, again? Recall this definition: Iteration is when the same procedure is repeated multiple times. Some examples were long division, the Fibonacci

More information

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) HW6 NOTE: Do not use the STL map or STL pair for HW6. (It s okay to use them for the contest.)

More information

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0. CSCI0330 Intro Computer Systems Doeppner Lab 02 - Tools Lab Due: Sunday, September 23, 2018 at 6:00 PM 1 Introduction 0 2 Assignment 0 3 gdb 1 3.1 Setting a Breakpoint 2 3.2 Setting a Watchpoint on Local

More information

C++ Arrays and Vectors

C++ Arrays and Vectors C++ Arrays and Vectors Contents 1 Overview of Arrays and Vectors 2 2 Arrays 3 2.1 Declaring Arrays................................................. 3 2.2 Initializing Arrays................................................

More information

For example, let s say we define an array of char of size six:

For example, let s say we define an array of char of size six: Arrays in C++ An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location, we specify the name and then the positive index into

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Tuesday, September 18th, 2012 from 2-3:50pm in West Hall Auditorium.

More information

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Today CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation Another vector operation: pop back Erasing items from vectors is inefficient! Iterators and iterator operations

More information

C++ for Python Programmers

C++ for Python Programmers C++ for Python Programmers Adapted from a document by Rich Enbody & Bill Punch of Michigan State University Purpose of this document This document is a brief introduction to C++ for Python programmers

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Lecture 8 Intro to the IvP Helm and Behaviors. Payload UUV Autonomy (3 Architecture Principles)

Lecture 8 Intro to the IvP Helm and Behaviors. Payload UUV Autonomy (3 Architecture Principles) MIT 2.680 UNMANNED MARINE VEHICLE AUTONOMY, SENSING, AND COMMUNICATIONS Lecture 8 Intro to the and s March 15th, 2018 Web: http://oceanai.mit.edu/2.680 Email: Mike Benjamin, mikerb@mit.edu Henrik Schmidt,

More information

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately, erasing items

More information

Bruce Merry. IOI Training Dec 2013

Bruce Merry. IOI Training Dec 2013 IOI Training Dec 2013 Outline 1 2 3 Outline 1 2 3 You can check that something is true using assert: #include int main() { assert(1 == 2); } Output: test_assert: test_assert.cpp:4: int main():

More information

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples Test 1 102 total points on the test. Therefore, your score is out of 102. Class average: 89.6 / 102 Distribution: Solutions are posted

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

CSCI 262 Data Structures. Recursive Function Analysis. Analyzing Power. Analyzing Power. Analyzing Power 3/31/2018

CSCI 262 Data Structures. Recursive Function Analysis. Analyzing Power. Analyzing Power. Analyzing Power 3/31/2018 CSCI Data Structures 1 Analysis of Recursive Algorithms, Binary Search, Analysis of RECURSIVE ALGORITHMS Recursive Function Analysis Here s a simple recursive function which raises one number to a (non-negative)

More information

A RISC class (extended abstract)

A RISC class (extended abstract) A RISC class (extended abstract) Andrew Koenig AT&T Bell Laboratories Murray Hill, New Jersey 07974 The state of the art Over the past several years, C+ + class authors have spent a lot of effort on container

More information

APCS-AB: Java. Recursion in Java December 12, week14 1

APCS-AB: Java. Recursion in Java December 12, week14 1 APCS-AB: Java Recursion in Java December 12, 2005 week14 1 Check point Double Linked List - extra project grade Must turn in today MBCS - Chapter 1 Installation Exercises Analysis Questions week14 2 Scheme

More information

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

PIC 10A Flow control. Ernest Ryu UCLA Mathematics PIC 10A Flow control Ernest Ryu UCLA Mathematics If statement An if statement conditionally executes a block of code. # include < iostream > using namespace std ; int main () { double d1; cin >> d1; if

More information

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Review from Lecture 8 CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists Designing our own container classes Dynamically allocated memory in classes Copy constructors, assignment operators,

More information

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently. The Science of Computing I Lesson 4: Introduction to Data Structures Living with Cyber Pillar: Data Structures The need for data structures The algorithms we design to solve problems rarely do so without

More information

Unit 2: The string class

Unit 2: The string class : class Programming 2 2015-2016 Index 1 characters in C 2 Input / output 3 Conversion between arrays of characters and strings 4 Comparison with arrays of characters 5 characters in C characters in C have

More information

9. The Disorganized Handyman

9. The Disorganized Handyman 9. The Disorganized Handyman A bad handyman always blames his tools. Famous Proverb. What if my hammer is made of paper? Can I blame it then? Author Unknown. Programming constructs and algorithmic paradigms

More information

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type. CS 7A - Fall 2016 - Midterm 1 10/20/16 Write responses to questions 1 and 2 on this paper or attach additional sheets, as necessary For all subsequent problems, use separate paper Do not use a computer

More information

COMP Lecture Notes Iterative and Recursive Procedures for Strings

COMP Lecture Notes Iterative and Recursive Procedures for Strings COMP 161 - Lecture Notes - 11 - Iterative and Recursive Procedures for Strings Spring 2016 In these notes we look at developing iterative and recursive implementations of procedures for Strings. These

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

6. Pointers, Structs, and Arrays. 1. Juli 2011

6. Pointers, Structs, and Arrays. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

Section 1: True / False (2 points each, 30 pts total)

Section 1: True / False (2 points each, 30 pts total) Section 1: True / False (2 points each, 30 pts total) Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer will be considered

More information

CSE 2123 Recursion. Jeremy Morris

CSE 2123 Recursion. Jeremy Morris CSE 2123 Recursion Jeremy Morris 1 Past Few Weeks For the past few weeks we have been focusing on data structures Classes & Object-oriented programming Collections Lists, Sets, Maps, etc. Now we turn our

More information

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I Review from Lecture 15 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

Lecture 6 CS2110 Spring 2013 RECURSION

Lecture 6 CS2110 Spring 2013 RECURSION Lecture 6 CS2110 Spring 2013 RECURSION Recursion 2 Arises in three forms in computer science Recursion as a mathematical tool for defining a function in terms of its own value in a simpler case Recursion

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors Arrays Returning arrays Pointers Dynamic arrays Smart pointers Vectors To declare an array specify the type, its name, and its size in []s int arr1[10]; //or int arr2[] = {1,2,3,4,5,6,7,8}; arr2 has 8

More information

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011 March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

G52CPP C++ Programming Lecture 17

G52CPP C++ Programming Lecture 17 G52CPP C++ Programming Lecture 17 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Exceptions How to throw (return) different error values as exceptions And catch the exceptions

More information

C++ Lab 05 - C Structures and C++ Classes

C++ Lab 05 - C Structures and C++ Classes C++ Lab 05 - C Structures and C++ Classes 2.680 Unmanned Marine Vehicle Autonomy, Sensing and Communications Spring 2018 Michael Benjamin, mikerb@mit.edu Department of Mechanical Engineering Computer Science

More information

Chapter 4. Computation. Bjarne Stroustrup.

Chapter 4. Computation. Bjarne Stroustrup. Chapter 4 Computation Bjarne Stroustrup www.stroustrup.com/programming Abstract Today, I ll present the basics of computation. In particular, we ll discuss expressions, how to iterate over a series of

More information

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Recursion What is recursion? Week 10 Generally, when something contains a reference to itself Gaddis:19.1-19.5 CS 5301 Spring 2014 Jill Seaman 1 Math: defining a function in terms of itself Computer science:

More information

CSE 333 Midterm Exam 7/25/16. Name UW ID#

CSE 333 Midterm Exam 7/25/16. Name UW ID# Name UW ID# There are 7 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? Recursion Chapter 8 CS 3358 Summer I 2012 Jill Seaman What is recursion? Generally, when something contains a reference to itself Math: defining a function in terms of itself Computer science: when a function

More information

Unit 14. Passing Arrays & C++ Strings

Unit 14. Passing Arrays & C++ Strings 1 Unit 14 Passing Arrays & C++ Strings PASSING ARRAYS 2 3 Passing Arrays As Arguments Can we pass an array to another function? YES!! Syntax: Step 1: In the prototype/signature: Put empty square brackets

More information

Computer Science II Lecture 2 Strings, Vectors and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion 1 Overview of Lecture 2 Computer Science II Lecture 2 Strings, Vectors and Recursion The following topics will be covered quickly strings vectors as smart arrays Basic recursion Mostly, these are assumed

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

CS11 Introduction to C++ Fall Lecture 6

CS11 Introduction to C++ Fall Lecture 6 CS11 Introduction to C++ Fall 2006-2007 Lecture 6 Today s Topics C++ exceptions Introduction to templates How To Report Errors? C style of error reporting: return values For C Standard Library/UNIX functions,

More information

6.096 Introduction to C++

6.096 Introduction to C++ MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 6.096 Lecture 3 Notes

More information

Array Elements as Function Parameters

Array Elements as Function Parameters Arrays Class 26 Array Elements as Function Parameters we have seen that array elements are simple variables they can be used anywhere a normal variable can unsigned values [] {10, 15, 20}; unsigned quotient;

More information

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Thursday, Sept 20th, 2018 from 6-7:50pm Students will be randomly

More information

Local and Global Variables

Local and Global Variables Lecture 10 Local and Global Variables Nearly every programming language has a concept of local variable. As long as two functions mind their own data, as it were, they won t interfere with each other.

More information

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself. Recursion Chapter 8 CS 3358 Summer II 2013 Jill Seaman What is recursion?! Generally, when something contains a reference to itself! Math: defining a function in terms of itself! Computer science: when

More information

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program: Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

How to approach a computational problem

How to approach a computational problem How to approach a computational problem A lot of people find computer programming difficult, especially when they first get started with it. Sometimes the problems are problems specifically related to

More information

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016 COMP 250 Winter 2016 5 generic types, doubly linked lists Jan. 28, 2016 Java generics In our discussion of linked lists, we concentrated on how to add or remove a node from the front or back of a list.

More information

The Dynamic Typing Interlude

The Dynamic Typing Interlude CHAPTER 6 The Dynamic Typing Interlude In the prior chapter, we began exploring Python s core object types in depth with a look at Python numbers. We ll resume our object type tour in the next chapter,

More information

Practice Midterm Exam #1

Practice Midterm Exam #1 Eric Roberts Handout #23 CS106B January 28, 2013 Practice Midterm Exam #1 Review session: Sunday, February 3, 7:00 9:00 P.M., Hewlett 201 (next door) Midterm #1: Tuesday, February 5, 3:15 5:15 P.M., Braun

More information

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

COM S 213 PRELIM EXAMINATION #2 April 26, 2001 COM S 213 PRELIM EXAMINATION #2 April 26, 2001 Name: Student ID: Please answer all questions in the space(s) provided. Each question is worth 4 points. You may leave when you are finished with the exam.

More information

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 14 for ENCM 339 Fall 2015 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2015 SN s ENCM 339 Fall 2015 Slide Set 14 slide

More information

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017 CS 137 Part 2 Loops, Functions, Recursion, Arrays September 22nd, 2017 Loops We will finish this week with looping statements We already discussed one such structure, namely while loops. while (expr) statement

More information

Lecture Notes on Binary Decision Diagrams

Lecture Notes on Binary Decision Diagrams Lecture Notes on Binary Decision Diagrams 15-122: Principles of Imperative Computation William Lovas Notes by Frank Pfenning Lecture 25 April 21, 2011 1 Introduction In this lecture we revisit the important

More information

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1 RACKET BASICS, ORDER OF EVALUATION, RECURSION 1 COMPUTER SCIENCE 61AS 1. What is functional programming? Give an example of a function below: Functional Programming In functional programming, you do not

More information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points) Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis: Recursion What is recursion? Week 10 Gaddis:19.1-19.5 CS 5301 Spring 2017 Jill Seaman 1 l Generally, when something contains a reference to itself l Math: defining a function in terms of itself l Computer

More information

ATOMS. 3.1 Interface. The Atom interface is simple: atom.h #ifndef ATOM_INCLUDED #define ATOM_INCLUDED

ATOMS. 3.1 Interface. The Atom interface is simple: atom.h #ifndef ATOM_INCLUDED #define ATOM_INCLUDED 3 ATOMS An atom is a pointer to a unique, immutable sequence of zero or more arbitrary bytes. Most atoms are pointers to null-terminated strings, but a pointer to any sequence of bytes can be an atom.

More information

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software. Introduction to Netbeans This document is a brief introduction to writing and compiling a program using the NetBeans Integrated Development Environment (IDE). An IDE is a program that automates and makes

More information

PIC 10A. Lecture 23: Intro to STL containers

PIC 10A. Lecture 23: Intro to STL containers PIC 10A Lecture 23: Intro to STL containers STL STL stands for Standard Template Library There are many data structures that share similar features These data structures can be manipulated by a common

More information

Lecture Notes on Binary Search Trees

Lecture Notes on Binary Search Trees Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning Lecture 17 1 Introduction In the previous two lectures we have seen how to exploit the structure of binary

More information

Lab 9: Pointers and arrays

Lab 9: Pointers and arrays CMSC160 Intro to Algorithmic Design Blaheta Lab 9: Pointers and arrays 3 Nov 2011 As promised, we ll spend today working on using pointers and arrays, leading up to a game you ll write that heavily involves

More information

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements Submitty iclicker registration is still open. Even if you already registered on the iclicker website,

More information

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Assignment 1: grid. Due November 20, 11:59 PM Introduction CS106L Fall 2008 Handout #19 November 5, 2008 Assignment 1: grid Due November 20, 11:59 PM Introduction The STL container classes encompass a wide selection of associative and sequence containers. However,

More information

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers What is Iteration? CMPT-101 Week 6 Iteration, Iteration, Iteration, Iteration, Iteration, Iteration,... To iterate means to do the same thing again and again and again and again... There are two primary

More information

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

More information

CSCE 2014 Final Exam Spring Version A

CSCE 2014 Final Exam Spring Version A CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers

More information

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10; Solving a 2D Maze Let s use a 2D array to represent a maze. Let s start with a 10x10 array of char. The array of char can hold either X for a wall, for a blank, and E for the exit. Initially we can hard-code

More information

EE 355 Lab 4 - Party Like A Char Star

EE 355 Lab 4 - Party Like A Char Star 1 Introduction In this lab you will implement a "hangman" game where the user is shown blanks representing letter of a word and then tries to guess and fill in the letters with a limited number of guesses

More information

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists Review from Lecture 9 Explored a program to maintain a class enrollment list and an associated waiting list. Unfortunately,

More information

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like? Chapter 3 - Simple JavaScript - Programming Basics Lesson 1 - JavaScript: What is it and what does it look like? PP presentation JavaScript.ppt. Lab 3.1. Lesson 2 - JavaScript Comments, document.write(),

More information

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues Alan J. Hu (Slides borrowed from Steve Wolfman) Be sure to check course webpage! http://www.ugrad.cs.ubc.ca/~cs221 1 Lab 1 is available.

More information

Lab 6 Vectors and functions

Lab 6 Vectors and functions CMSC160 Intro to Algorithmic Design Blaheta Lab 6 Vectors and functions 11 October 2016 The drill for this lab is another part of the Chapter 4 drill. Come to lab on Tuesday either with it completed or

More information

Trombone players produce different pitches partly by varying the length of a tube.

Trombone players produce different pitches partly by varying the length of a tube. Trombone players produce different pitches partly by varying the length of a tube. 7 Variables A variable is a connection between a name and a value.* That sounds simple enough, but some complexities arise

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm. CS52 - Assignment 8 Due Friday 4/15 at 5:00pm https://xkcd.com/859/ This assignment is about scanning, parsing, and evaluating. It is a sneak peak into how programming languages are designed, compiled,

More information

CSE wi: Practice Midterm

CSE wi: Practice Midterm CSE 373 18wi: Practice Midterm Name: UW email address: Instructions Do not start the exam until told to do so. You have 80 minutes to complete the exam. This exam is closed book and closed notes. You may

More information