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)

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)

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

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

CS 216 Exam 1 Fall SOLUTION

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

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

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

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

CSE030 Fall 2012 Final Exam Friday, December 14, PM

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

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

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

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

Ch. 17: Linked Lists. Introduction to Linked Lists

CS171 Midterm Exam. October 29, Name:

CSCE 2014 Final Exam Spring Version A

Name CPTR246 Spring '17 (100 total points) Exam 3

Definition of Stack. 5 Linked Structures. Stack ADT Operations. ADT Stack Operations. A stack is a LIFO last in, first out structure.

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

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

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

Dynamic Data Structures

Largest Online Community of VU Students

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Week 3: Pointers (Part 2)

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

CMSC 341 Lecture 7 Lists

Common Misunderstandings from Exam 1 Material

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

CS24 Week 4 Lecture 2

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Programming Assignment 2

Midterm Exam (REGULAR SECTION)

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

LECTURE 03 LINKED LIST

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

Largest Online Community of VU Students

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,

Linked List using a Sentinel

Memory and Pointers written by Cathy Saxton

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

CMSC 341 Lecture 10 Binary Search Trees

Data Structures and Algorithms

Data Structure (CS301)

Data Structures (CS301) LAB

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

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

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

Midterm Review. PIC 10B Spring 2018

CS201 Some Important Definitions

Exam 3 Chapters 7 & 9

mypoly

Largest Online Community of VU Students

Pointers, Dynamic Data, and Reference Types

CS24 Week 3 Lecture 1

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

Computer Science & Engineering 150A Problem Solving Using Computers

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

Introduction to Computer Science Midterm 3 Fall, Points

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

ADTs Stack and Queue. Outline

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

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

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

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.

CS201- Introduction to Programming Current Quizzes

Cpt S 122 Data Structures. Data Structures

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Data Structures and Algorithms Winter Semester

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

Review for Test 1 (Chapter 1-5)

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

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

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

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

"apple" "grape" "grape" "grape" "apple"

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

CSIS 10B Lab 2 Bags and Stacks

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

UNIVERSITY REGULATIONS

CS250 Final Review Questions

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Class and Function Templates

Programming in Haskell Aug-Nov 2015

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

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

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Transcription:

Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) l Assign a category based on ranges (wind speed) 1 - Tropical Depression: <38mph - Tropical Storm: 39-73mph - Hurricane: >=74mph 2 Write C++ code to: Chapters 1-7 + 11 l Pass arguments by value and reference - Return multiple values from a function: compute the area AND perimeter of a rectangle l Process an Array: - find maximum/minimum value - sum/average/count values passing a test (>100) l Define a structure for weight: lbs and ozs (1lb=16 ozs) 3 Finding Errors l Input validation for password, 2 errors: PasswordManager pwm; string pw; bool testa; do{ cout << "Please enter a new password." << endl; cin>> pw; bool testa = pwm.setnewpassword (pw); while(testa = false); l Calculate average with decimals, 2 errors: double average (int a, int b, int c) { double result = a + b + c / 3; return result; 4

Finding Errors l Sum all values between 0 and 100, 3 errors: int sum (int a[], int size) { int total; for (int i=1; i<size; i++) { if (a[i] >=0 a[i] <=100) total = total + a[i]; return total; Binary Search Example The target of your search is 39. Given the following array of integers (values on top, indexes on bottom), record the values that 39 is compared to in each iteration of a binary search. 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. Solution for 39 (which is not in the list): 67 14 42 20 5 65 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 0 1 2 3 4 5 6 7 7 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 does n represent? - what does the function inside the ( ) describe? 8

Algorithm Efficiency Give the efficiency of each using big-o notation l Linear search l Binary search on an already sorted list l Bubble sort l Selection sort l Access one element in an array l Array processing: - sum, average, show list, find max/min - delete all elements Algorithm Efficiency Give the efficiency of each using big-o notation l Linked list operations: - insert at head - append (with no tail pointer) - delete a node with a given value - destructor ( delete all nodes) - access one element (by index) - sum, average, show list, find max/min (traversal) - the sort we did in PA5 if you have to traverse the list, it s O(n). if you have to traverse the list once for each element, it s O(n 2 ) 9 10 Pointers l 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; 11 Pointers l Rewrite using pointer notation instead of []: for (int x = 0; x < 100; x++) cout << array[x] << endl; l Write a function to swap the values of its parameters, using pointers instead of pass by reference. l 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? 12

Pointers to structs/obj: l Given the following definitions: struct A {int m; int *x; A a, *s; int z; l 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. 13 Linux Commands l 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? 14 Classes l What is the error? (hint: it s a syntax error) class Time { private: int hour; int minute; public: void addminute(); void addhour(); ; void addminute() { if (minute==59) { minute=0; addhour(); else minute++; 15 Classes l Give the class declaration, and function implementations: Consider a Student class, which stores a student using three values: a name (string) an ID number (int) and a grade (double). It has 2 constructors (one default, one with 3 arguments), and set and get function for the name and grade. The default student has an empty string for the name, and 0 for the other two variables. The other constructor takes the initial name, ID number, and grade. There is a function print() that outputs the three values. The student also has a function letter() that calculates and returns the letter grade corresponding to the grade ( A if grade is 90 or more, B if grade is 80 or more (but less than 90), etc). 16

Linked Lists l 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. 17 Linked Lists l 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; 18 Stacks and Queues exercises 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): 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; push(3); push(5); push(9); x = pop(); push(2); x = pop(); push(0); 19 int x; enqueue(3); enqueue(5); enqueue(9); x = dequeue(); enqueue(2); x = dequeue(); enqueue(0); 20

Stacks and Queues programming Implement push, pop, isempty and isfull for both of these (and similarly for a Queue): class Stack { private: static const int size = 100; int stack[size]; int top; public: Stack() {top = -1; void push (int); int pop(); bool isempty(); bool isfull(); ; class Stack { private: struct Node { int value; Node *next; Node *top public: Stack() {top = NULL; void push (int); int pop(); bool isempty(); bool isfull(); ; 21 Disclaimer l The exercises in this lecture do not cover ALL of the material that will be on the final exam. l These exercises do provide some sample exam questions (though many will be in multiple choice format on the exam). l There will be questions that will require writing programs or functions or class declarations and implementations. l There will be questions (mostly multiple choice) that will test your understanding of the concepts we have covered (vocabulary, etc). 22