! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Similar documents
! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Week 3: Pointers (Part 2)

Ch. 17: Linked Lists. Introduction to Linked Lists

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

Introduction to Computer Science Midterm 3 Fall, Points

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

1. Write step by step code to delete element from existing Doubly Linked List. Suppose that all declarations are done

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCE 2014 Final Exam Spring Version A

Pointers, Dynamic Data, and Reference Types

Part I: Short Answer (12 questions, 65 points total)

CS201 Some Important Definitions

Overloading Operators

Review for Test 1 (Chapter 1-5)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS201- Introduction to Programming Current Quizzes

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Midterm Review. PIC 10B Spring 2018

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

! A data type for which: ! An ADT may be implemented using various. ! Examples:

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

CS 311 Data Structures and Algorithms, Fall 2009 Midterm Exam Solutions. The Midterm Exam was give in class on Wednesday, October 21, 2009.

CS 311 Data Structures and Algorithms, Spring 2009 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, March 18, 2009.

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Exam 3 Chapters 7 & 9

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CS201 Latest Solved MCQs

CMSC 341 Lecture 7 Lists

A linear-list Data Structure where - addition of elements to and - removal of elements from are restricted to the first element of the list.

Operator Overloading and Templates. Linear Search. Linear Search in C++ second attempt. Linear Search in C++ first attempt

Common Misunderstandings from Exam 1 Material

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

CS 216 Exam 1 Fall SOLUTION

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Memory and Pointers written by Cathy Saxton

LECTURE 03 LINKED LIST

CS250 Final Review Questions

Suppose that the following is from a correct C++ program:

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

CS24 Week 3 Lecture 1

CS24 Week 4 Lecture 2

ComS 228 Exam 1. September 27, 2004

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Dynamic Memory Allocation

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Dynamic Data Structures

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

FORM 2 (Please put your name and form # on the scantron!!!!)

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Name: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

CPSC 427: Object-Oriented Programming

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CS 115 Exam 3, Spring 2010

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Solution to CSE 250 Final Exam


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

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CMSC 341 Lecture 10 Binary Search Trees

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Transcription:

Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Spring 2013 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)! Assign a category based on ranges (wind speed) 1 - Tropical Depression: <38mph - Tropical Storm: 39-73mph - Hurricane: >=74mph - (extra: there are also 5 levels of hurricanes) 2 Write C++ code to: Chapters 1-7 + 11! Pass arguments by value and reference - Return multiple values from a function: compute the area and perimeter of a rectangle! Process an Array: - find maximum/minimum value - count values passing a test (>100) - sum/average values passing a test (>100)! Define a structure for weight: lbs and ozs (1lb=16 ozs) Write C++ function to: Strings! determine if a string meets the following criteria: - it contains only letters, numbers, and the underscore (but none of these is required) - the first character is not a digit. - You may assume it has at least one character.! Determine if one weight is - equal to another - greater than (or less than) another 3 4

Binary Search Example The target of your search is 39. Given the following list of integers, record the values of first, last, and middle during a binary search. Assume the following numbers are in an array. 1 7 8 14 20 42 55 67 78 101 112 122 170 179 190!! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Repeat the exercise with a target of 179. Example Sorting Problem Given the following list of integers, show what order the integers would be in after executing: a) one complete pass of the bubble sort b) two passes of the selection sort. Assume the following numbers are in an array (subscripts along the bottom).! 112 73 8 140 22 42 88 67 Solution for 39 (which is not in the list): first 0 0 4 4 5 last 14 6 6 4 4 middle 7 3 5 4 x These are indexes!!!! 0 1 2 3 4 5 6 7 5 6 Algorithm Efficiency Big O Notation In order of increasing growth (less efficiency) - O(1) constant - O(log n) logarithmic - O(n) linear - O(n log n) linearithmic - O(n 2 ) quadratic when using big O notation to describe the efficiency of an algorithm: - what is n? Algorithm Efficiency Give the efficiency of each using big-o notation! Linear search! Binary search on an already sorted list! Bubble sort! Selection sort! Access one element in an array! Array processing: - sum, average, show list, find max/min - delete all elements - what does the function inside the ( ) describe? 7 8

Algorithm Efficiency Give the efficiency of each using big-o notation! Linked list operations: - insert at head - append - delete (removeone) - destructor ( delete all nodes) - access one element (by index) if you have to traverse the list, its O(n). if you have to traverse the list once for each element, it s O(n 2 ) - sum, average, show list, find max/min (traversal) - selection sort, as we did in Assign 6 9 Pointers! Tracing code with pointers, what is output? int *ptr1, *ptr2; int foo1, foo2 = 13; ptr1 = &foo1; ptr2 = &foo2; foo1 = 42; cout << "*ptr1 - " << *ptr1 << endl; cout << "*ptr2 - " << *ptr2 << endl; ptr1 = ptr2; cout << "foo1 - " << foo1 << endl; cout << "foo2 - " << foo2 << endl; ptr2 = &foo1; *ptr1 = *ptr2; cout << "foo1 - " << foo1 << endl; cout << "foo2 - " << foo2 << endl; 10 Pointers! Rewrite using pointer notation instead of []: for (int x = 0; x < 100; x++) cout << array[x] << endl;! Write a function to swap the values of its parameters, using pointers instead of pass by reference.! Write a function to double the size of an array, by dynamically allocating a new array that is twice as big, and copying the elements from the original. - who is responsible for deallocating this dynamically allocated array? What statement is used to deallocate it? 11 Pointers to structs/obj:! Given the following definitions: struct A {int m, int *x; A a, *s; int z;! Write the C++ code for the following: - to store the address of a in s. - to store 10 in the m field of a (don t use a, use s). - to store the address of z in the x field of a. - to store 11 in z using only a and x in the statement. - to store 12 in z using only s and x in the statement. 12

Linux Commands! What linux command would you use to: A. List (display) the files in the current directory? B. Display the name of the current directory? C. Make a new directory called Assignments? D. Make Assignments the current directory? E. Edit a file called myfile.txt? F. Compile a file called myprog.cpp? G. View the contents of myprog.cpp on the screen? H. Delete the file myprog.cpp? I. Execute a makefile? J. Compile a file called a.cpp to an object file? 13 Classes! What is the error? (hint: it s a syntax error) class Time { int hour; int minute; void addminute(); void addhour(); ; void addminute() { if (minute==59) { minute=0; addhour(); else minute++; 14 Classes! Given the following Time class: class Time { int hour; int minute; Time(); Time(int, int); Time(Time &); ; Classes! Given a 24 hour time class, implement display: class Time24 { int hour, minute; Time24(int,int); void display(); ; Assume the hour will be between 0 and 23 and the minute will be between 0 and 59. Time24::Time24(int h, int m) { hour = h; minute = m; void Time24::display() {! Circle the copy constructor prototype! If t1 is an existing Time object, use the copy constructor to initialize a new Time object t from t1: 15 Implement this function to output the time in a 12 hour format with am or pm (but with no 0 padding). The output for the example below should be: 2:23pm int main () { Time24 t(14,23); t.display(); 16

Syntax you should know! const in member function definition! setiosflags Syntax you should know class Time { int hour, minute; int gethour() const; Time(); ;! Alternative constructor syntax: const here indicates the function will NOT change any of the member variables values cout << setiosflags(ios::fixed ios::showpoint); cout << fixed << showpoint; These statements are equivalent. Time::Time() : hour(12), minute(0) { is equivalent to: Time::Time () { hour = 12; minute = 0; 17 18 Linked Lists! Write a function that takes an array of ints and converts it to a linked list by inserting each element to the front of the list (the order will be reversed). Return a pointer to the first node. struct Node { int data; Node *next; ; Node *convertreverse (int list[], int size) { Spoiler alert! Solution on next slide. 19 Linked Lists! Write a function that takes an array of ints and converts it to a linked list by inserting each element to the front of the list (the order will be reversed). struct Node { int data; Node *next; ; Node *convertreverse (int list[], int size) { Node *head = NULL; for (int i=0; i<size; i++) { Node *newnode = new Node; newnode->data = list[i]; newnode->next = head; head = newnode; return head; 20

Stacks and Queues Stacks and Queues exercises Describe in English how to use a stack to:! determine if the brackets are matched in a string (or a file). Suppose the following operations are performed on an empty stack. Insert numbers in the diagram to show what will be stored in the stack after the operations have executed (label the top):! evaluate expressions written in post-fix notation. 21 int x; push(3); push(5); push(9); pop(x); push(2); pop(x); push(0); 22 Stacks and Queues exercises Suppose the following operations are performed on an empty queue. Insert numbers in the diagram to show what will be stored in the queue after the operations have executed (label the front+rear): int x; enqueue(3); enqueue(5); enqueue(9); dequeue(x); enqueue(2); dequeue(x); enqueue(0); Specify which location is the front, and which location is the rear. Don t just put the labels at the top or bottom of the drawing. 23 Disclaimer! The exercises in this lecture do not cover ALL of the material that will be on the final exam.! These exercises do provide some sample exam questions.! There will be questions that will require writing programs or functions or class declarations and implementations.! There will be questions (short answer, multiple choice) that will test your understanding of the concepts we have covered (vocabulary, etc). 24