Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
1 Various Methods of Populating Arrays Randomly generated integers.
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Arrays part 2 Applications & such. Returning an array from a method A method can return an array, just like it can return any other kind of variable;
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Problem: Given an array of positive integers, find a pair of integers in the array that adds up to 10 (or indicate none exist). Your code should be as.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Arrays Chapter 6 Chapter 6.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Jan Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Quiz 1 What is this? (explain the use of the reserved word “this” in a class method). Answer each question briefly. – What is a Constructor? –Under what.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
A: A: double “4” A: “34” 4.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Multidimensional Arrays Computer and Programming.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Lecture 18: Nested Loops and Two-Dimensional Arrays
SELECTION STATEMENTS (1)
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
2/26/12 Quiz Bowl.
CSC 142 Computer Science II
Arrays Declare the Array of 100 elements Notes
CSS161: Fundamentals of Computing
Building Java Programs Chapter 7
Building Java Programs
CS139 October 11, 2004.
Arrays review.
Question 1a) What is printed by the following Java program? int s;
Building Java Programs
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Sorting Algorithms.
Presentation transcript:

Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin 1265 No computer use or other references are permitted, except that you may use 1 (2-sided) page of your own notes, which you should hand in with your exam. 5 problems: 2 “what does this do” and 3 writing methods You may use up to 2 hours (in 1 block) for the exam These practice problems (and solutions) are at

Practice Problem 1 Write a method that takes a String as input. The method should return the number of vowels in the input string. Count the letter ‘y’ as a vowel when there is no vowel before or after it. Otherwise, count ‘y’ as a consonant. input: yellowy input: slyly input: yesterday output returned:

Practice Problem 2 What values do the variables c and A have at the end of this block of code? int c = 0; int[] A = new int[4]; A[0] = 2; A[1] = 0; A[2] = 3; A[3] = 1; for (int i=3 ; i>0 ; --i) { for (int j=0 ; j<i ; ++j) { if (A[j] > A[i]) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; c++; }

Practice Problem 3 Write a method with the following signature: First, the method should make sure its input array has one more row than columns. (If not, it should simply return 0). If so, it should replace the last element in each column with the whole column’s sum. It should return the largest value in the original array. input: int spreadsheet(int[][] A) result: returns: 42

Pr. Problem 4 public static void main(String[] s) { int[] A = new int[2]; A[0] = 7; A[1] = 4; H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); A[0] = match(A[1], A[0]); H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); } What does this code print? public static void mix(int[] A) { A[1] += A[0]; A[0]--; } public static int match(int x, int y) { if ( x < y ) return (x+y); else return (x-y); }

Practice Problem 5 Write a java method harmonic that takes a double as input. It should returns the (integer) number of initial terms in the harmonic sequence that need to be added up so that the sum is greater than the input double … The harmonic sequence +++++

Solutions are on the following slides

Practice Problem 1 int numVowels(String s) { int sum = 0; int L = s.length(); for (int i=0 ; i<L ; ++i) { if (isV(s.charAt(i))) sum++; if (s.charAt(i)==‘y’) { if (i==0 && L>1 && isV(s.charAt(1)) sum--; // front else if (i==L-1 && L>1 && isV(s.charAt(i-1)) sum--; // back else if (i!=0 && i!=L-1 && // middle ( isV(s.charAt(i-1)) || isV(s.charAt(i+1) )) sum--; } return sum; } boolean isV(char c) { if (c==‘a’||c==‘e’||c==‘i’||c==‘o’||c==‘u’||c==‘y’) return true; else return false; }

Practice Problem 2 int c = 0; int[] A = new int[4]; A[0] = 2; A[1] = 0; A[2] = 3; A[3] = 1; for (int i=3 ; i>0 ; --i) { for (int j=0 ; j<i ; ++j) { if (A[j] > A[i]) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; c++; } i=3, j=0 (at end) i=3, j=1 (at end) i=3, j=2 (at end) i=2, j=0 (at end) i=2, j=1 (at end) i=1, j=0 (at end) A[0]A[1]A[2]A[3] c = 1 c = c = intially c = 0 it sorts the array and counts the number of swaps needed to do so!

Practice Problem 3 int spreadsheet(int[][] A) { if (A.length != A[0].length+1) return 0; int max = A[0][0]; // make the first element the max int BOT = A.length-1; // the BOTtom row for (int c=0 ; c<A[0].length ; ++c) // for each column, c { A[BOT][c] = 0; // set the BOTtom to 0 for (int r=0 ; r<BOT ; ++r) // and row, r { A[BOT][c] += A[r][c]; // add the elements above if (A[r][c] > max) max = A[r][c]; } return max; }

Practice Problem 4 public static void main(String[] s) { int[] A = new int[2]; A[0] = 7; A[1] = 4; H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); A[0] = match(A[1], A[0]); H.pl(A[0] + “ ” + A[1]); mix( A ); H.pl(A[0] + “ ” + A[1]); } public static void mix(int[] A) { A[1] += A[0]; A[0]--; } public static int match(int x, int y) { if ( x < y ) return (x+y); else return (x-y); } 7 4 A[0]A[1]

Practice Problem 5 int harmonic(double d) { int N = 1; double sum = 0.0; while (sum < d) { sum += 1.0/N; ++N; } return N; }