All answers will be posted on web site, and most will be reviewed in class.

Similar documents
2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Exam 2. CSC 121 TTH Class. Lecturer: Howard Rosenthal. April 26, 2016

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

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

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Reviewing all Topics this term

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 2: Functions and Control Structures

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

APCS Semester #1 Final Exam Practice Problems

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Computer Science & Engineering 150A Problem Solving Using Computers

Integers and Absolute Value. Unit 1 Lesson 5

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Programming in OOP/C++

Programming in C++ PART 2

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Introduction to Computer Science Midterm 3 Fall, Points

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

1 Short Answer (15 Points Each)

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Chapter 9 Introduction to Arrays. Fundamentals of Java

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Computer Programming: C++

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Lecture 3 Tao Wang 1

n Group of statements that are executed repeatedly while some condition remains true

Exam 3 Chapters 7 & 9

Full file at

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Review of Important Topics in CS1600. Functions Arrays C-strings

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

CS111: PROGRAMMING LANGUAGE II

Problem Solving With Loops

Array. Array Declaration:

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

KENDRIYA VIDYALYA CLRI CHENNAI AUTUMN BREAK HOLIDAY HW MARKS QUESTIONS : DATA STRUCTURE

Ex: If you use a program to record sales, you will want to remember data:

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

Subject: Computer Science

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Pace University. Fundamental Concepts of CS121 1

Loops / Repetition Statements

Final Exam. COMP Summer I June 26, points

Give one example where you might wish to use a three dimensional array

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

CS1004: Intro to CS in Java, Spring 2005

(Not Quite) Minijava

Assignment 1 Expressions Data Types Formatted Printing Variables Scanning CSC 123 Fall 2018 Answer Sheet Short Answers

4. Java language basics: Function. Minhaeng Lee

Module 6: Array in C

Programming with Java

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

The Warhol Language Reference Manual

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Advanced Computer Programming

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

ASSIGNMENT 2 Conditionals, Loops, Utility Bills, and Calculators

Laboratorio di Algoritmi e Strutture Dati

More non-primitive types Lesson 06

1 Lexical Considerations

CS 230 Programming Languages

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

COSC 1P03. Chapter 1 Arrays. Introduction to Data Structures 1.2

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

Arrays in Java Multi-dimensional Arrays

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

About this exam review

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

REPETITION CONTROL STRUCTURE LOGO

Transcription:

Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments. E-mail address is: csc123csudh@gmail.com Each program is to be submitted in a separate file with the file name being the class name with extension.java as shown below. I only need the source file. GeneralAverage.java The other homework file should be submitted with your name using the following format: RosenthalH_Ax.doc, where x is the assignment number. Note: use your name, not mine All answers will be posted on web site, and most will be reviewed in class. Review Chapter 7 in Bravaco Short Answers 1. In an array declaration, this indicates the number of elements that the array will have. a. subscript b. size declarator c. element sum d. reference variable 2. Each element of an array is accessed by a number known as a(n). a. subscript b. size declarator c. address d. specifier 3. The first subscript in an array is always. a. 1 b. 0 c. 21 d. 1 less than the number of elements 4. The last subscript in an array is always. a. 100 b. 0 c. 21 d. 1 less than the number of elements 1

5. This array field holds the number of elements that the array has a. size b. elements c. length d. width 6. This search algorithm steps through an array, comparing each item with the search value. a. binary search b. sequential search c. selection search d. iterative search 7. This search algorithm repeatedly divides the portion of an array being searched in half. a. binary search b. sequential search c. selection search d. iterative search 8. This is the typical number of comparisons performed by the sequential search on an array of N elements (assuming the search values are consistently found). a. 2N b. N c. N 2 d. N/2 9. When initializing a two-dimensional array, you enclose each row s initialization list in. a. braces b. parentheses c. brackets d. quotation marks 10. True or False: Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array. 11. True or False: An array s size declarator can be a negative integer expression. 12. True or False: Both of the following declarations are legal and equivalent: int[] numbers; int numbers[]; 13. True or False: The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array. 14. True or False: The values in an initialization list are stored in the array in the order that they appear in the list. 2

15. True or False: When an array reference is passed to a method, the method has access to the original array. 16. True or False: The first size declarator in the declaration of a two-dimensional array represents the number of columns. The second size declarator represents the number of rows. 17. What are the values stored in the array a after the following code executes? int [] a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = 2*i 1; 18. What are the values stored in the array a after the following code executes? int [] a = new int[10]; a[0] = 1; for (int i = 1; i < a.length; i++) a[i] = 2*a[i-1] 1; Find the error(s) in each of the following code segments for questions 19-24. 19. char[] a = new char[10]; a[10] = s ; a[9] = 76; 20. char[] a = new char[10]; char[9] = s ; 21. char[] a = new int[10]; a[0] = s ; a[1] = 80; 22. int[] a; a = new int[255]; a[0] = s ; a[a[0]] = 35; a[35] = 12345654321; 23. int[] a; a = new int[255]; a[0] = a.length; a[0]--; 3

a.length--; a[a[0]] = 2; 24. int[] b = new int[9]; int[][] a; a = new int[255][]; a[0] = b; a[0][3] = 9; a[3][0] = 9; Homework Programs Do 7 of the first 9. Number 10 is a bonus. 1. ReversePrint Write a program (all in main) Creates an int array of size 100 Reads in up to 100 elements, stopping when you read in -999 (which is not put into the array. Prints out the array elements read in in reverse order. 2. RandomFill Write a program that does the following: In main: Read in the size of an int array Create an array of that size Call randomfill with a reference to the array Print out the elements of the array In randomfill: Fill the array with random numbers from 2 to 12. (Note: you don t need to return anything) 3. SumEvenOddAll Write a program that does the following: In main: Creates an int array of size 100 Calls fillarray which accepts a reference to the created array Calculates the sums of the even numbers, odd numbers and all the numbers in the arrays Prints out the three sums fillarray does the following Accepts a reference to an int array Reads numbers into the array until it reads a -999 or the array is filled up. Return the number of elements read into the array 4

4. CoffeeAcid Write a program that does the following: In main: Read in the length of an array of type double. Call createarray with the length Read in the PH value as floating point number greater than 0.0 and less than 7.0 into the array created in createarray. (assume values read in are correct) Find the average of the elements in the array. Find the element in the array furthest from the average. Print out the average, and the value and index of the element furthest from the average. In createarray: Create an array of type double with the length in the formal parameter. Return the reference to that array. Notes: If more than one value is the furthest can just list the first. Remember to use absolute value when testing the difference from the average. 5. Rainfall Write a Rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The main programs requests and reads in the rainfall by month. Do not accept negative numbers for monthly rainfall figures. The main method also prints out the data returned from two separate methods that return the following: the total rainfall for the year the average monthly rainfall 6. Lottery Write a Lottery class that simulates a lottery. The class should have an array of five integers in the range of 0 through 9 for each element in the array that are generated randomly in a method called generatelotterywinner. The class should have a method called getbettorpicks that is used to request the user s five picks between 0 and 9 and return a reference to that array. The class should also have a method comparenumbers that compares the corresponding elements in the two arrays and return the number of digits that match. Demonstrate the class in a program that asks the user to enter five numbers. The program should display the 5 lotter winners and the number of digits that match the randomly generated lottery numbers. If all of the digits match, display a message proclaiming the user a grand prize winner. 7. TwoDAverage In main; Read in the number of rows and columns of a regular two-dimensional int array. Read in the elements row by row. Call average2d with a reference to the array read in. 5

Print out the average returned by average2d. In average2d Calculate the average as a type double and return as a type double. 8. CountGreaterThanTen Write a class CountGreaterThanTen that does the following: The main method does the following: a. Requests from the user the dimensions for a two dimensional array. b. Calls the method createarray to create a two-dimensional integer array. c. Calls a method fullarray with a reference to the array created by createarray. d. Calls a method called greaterten with a reference to the array created by createarray. e. Prints out the final elements of the two-dimensional array row by row. f. The main method then prints out the final values in the array and a final sentence stating: It is (true/false) that there are more entries in the array greater than ten than less than or equal to ten. createarray does the following: a. Accepts the dimensions of a two dimensional int array as parameters. b. Creates a two-dimensional int array using the input parameters. c. Returns a reference to the array that was created. fullarray does the following: a. Has a two-dimensional int array as its formal parameter. b. Fills a two-dimensional int array with random integers from 1 to 20. greaterten does the following: a. Has a two-dimensional int array as its formal parameter. It then counts the number of entries greater than ten and returns true if there are more numbers greater than ten in the array. Otherwise it returns false. 9. RaggedArrays - Note: Typo in book r is supposed to be i The following code defines a ragged two-dimensional array. The term ragged indicates that the rows are not all the same length. int[][] triangle = new int[5][]; // allocate array of rows for (int i = 0; i < triangle.length; i++) triangle[i] = new int[i+1]; a. Write a method int [][] buildragged(int n) that returns a (possibly) ragged array with n rows. The method reads n + 1 lines of data from the console. The first line contains the number of rows of a ragged array. Each succeeding line specifies one row of a ragged array. The first entry of each line gives the number of items in that row. For example, the input 3 3 1 5 7 6 5 6 8 9 3 2 6

2 5 8 indicates that there are 3 rows and that the first row of the ragged array has 3 entries (1, 5, and 7), the second row 6 entries (5, 6, 8, 9, 3, and 2), and the third row 2 entries (5 and 8). b. Write a method void printarray(int [][] x) that displays a (possibly) ragged array. c. Test your methods in a program using the main(...) method 10. MergedArrays (Bonus) In the main program: Read in the lengths of two arrays. The arrays don t need to be of the same length Create each of the arrays as an int array. For each array call randomfill with a reference to the array. For each array, call maxsort with a reference to the array that sorts the array from least to greatest Call mergearrays with references to each of the two arrays Print out the two original arrays after they have been sorted and the merged array whose reference is returned by mergearrays randomfill: Accepts a reference to an int array. Fills the array with random integers between 1 and 100. maxsort: Accepts a reference to an int array. Sorts the array. You may copy the appropriate code from the MaxSort.java file including the maxsort and max methods mergearrays: Accepts the references from two int arrays Creates a third array that includes all the elements of the first two sorted arrays This third array create has a length equal to the sum of the lengths of two arrays, and has the elements merged from least to greatest. Returns a reference to the merged array. 7