CSE 1223: Introduction to Computer Programming in Java Chapter 6 Arrays

Similar documents
Controls Structure for Repetition

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Chapter 8 Multi-Dimensional Arrays

Chapter 7 User-Defined Methods. Chapter Objectives

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

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Array. Prepared By - Rifat Shahriyar

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

2. [20] Suppose we start declaring a Rectangle class as follows:

LAB 12: ARRAYS (ONE DIMINSION)

Arrays Chapter 6 Chapter 6 1

String. Other languages that implement strings as character arrays

CSE 1223: Exam II Autumn 2016

1 Short Answer (15 Points Each)

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

CS242 COMPUTER PROGRAMMING

COS 126 General Computer Science Spring Written Exam 1

Arrays. Chapter The Java Array Object. Goals

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Introduction to Programming Using Java (98-388)

Introduction to Programming (Java) 4/12

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

LAB 13: ARRAYS (ONE DIMINSION)

Chapter 2: Basic Elements of Java

Example Program. public class ComputeArea {

Intermediate Programming

Chapter 5 Lab Methods

H212 Introduction to Software Systems Honors

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

CSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN

CS212 Midterm. 1. Read the following code fragments and answer the questions.

TEST (MODULE:- 1 and 2)

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

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

Arrays and Pointers (part 1)

Arrays and Pointers (part 1)

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Introduction to Computer Programming

Final Exam Practice. Partial credit will be awarded.

Activity 3: Data Types

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

Introduction to Computer Science Midterm 3 Fall, Points

Selected Questions from by Nageshwara Rao

Building Java Programs

Arrays. Eng. Mohammed Abdualal

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

One Dimension Arrays 1

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

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

CMPS 134: Computer Science I Fall 2011 Test #1 October 5 Name Dr. McCloskey

Fundamentals of Programming Data Types & Methods

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

CSSE 220. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysListPractice from SVN

CSE 114 Computer Science I

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Chapter 4: Control Structures I

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS 101 Fall 2006 Midterm 1 Name: ID:

Conditions, logical expressions, and selection. Introduction to control structures

1 Definitions & Short Answer (5 Points Each)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Study Guide for Test 2

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Computational Expression

Midterm Examination (MTA)

Chapter 5: Arrays. Chapter 5. Arrays. Java Programming FROM THE BEGINNING. Copyright 2000 W. W. Norton & Company. All rights reserved.

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

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

Computer Science is...

Name CIS 201 Midterm II: Chapters 1-8

Higher National Diploma in Information Technology First Year, Second Semester Examination 2015

Pointers and scanf() Steven R. Bagley

Chapter 5 Lab Methods

Manipulating One-dimensional Arrays

Chapter 2. Elementary Programming

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

COMPUTER APPLICATIONS

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Lecture 5: Arrays. A way to organize data. MIT AITI April 9th, 2005

Question 1 [20 points]

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

Chapter 9 Introduction to Arrays. Fundamentals of Java

1 Short Answer (15 Points Each)

Last Class. While loops Infinite loops Loop counters Iterations

CSIS-120 Final Exam Fall 2015 Name:

Arrays. Introduction. Figure 3: Examples for Arrays.

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

Transcription:

CSE 1223: Introduction to Computer Programming in Java Chapter 6 Arrays 1

A New Problem Consider the following task: Input N real numbers representing temperature measurements and compute the following: the average, minimum, and maximum temperature the number of temperatures that are above the average and the number of temperatures that are below the average the median temperature 2

What s the Problem? We need to store all the temperatures before we can perform some of the computation How many variables do we need? What are we going to name them? 3

The Solution: Arrays An array is a sequence of variables of the same data type (any type can be used: e.g., int, double, boolean, String, etc.) Each variable in the array is called an element Array elements are accessed through an index (their position in the array) 4

An Example int N = 5; double [] temperatures; temperatures = new double[n]; Scanner in = new Scanner(System.in); int pos = 0; while (pos < N) { } temperatures[pos] = Double.parseDouble(in.nextLine()); pos = pos + 1; 5

An Example int N = 5; double [] temperatures; temperatures = new double[n]; declare an array variable called temperatures whose elements will be double variables Scanner in = new Scanner(System.in); int pos = 0; while (pos < N) { } temperatures[pos] = Double.parseDouble(in.nextLine()); pos = pos + 1; 6

An Example int N = 5; double [] temperatures; temperatures = new double[n]; Scanner in = new Scanner(System.in); int pos = 0; while (pos < N) { } temperatures[pos] = Double.parseDouble(in.nextLine()); pos = pos + 1; declare an array variable called temperatures whose elements will be double variables create an actual array with N=5 elements of type double 7

An Example int N = 5; double [] temperatures; temperatures = new double[n]; Scanner in = new Scanner(System.in); int pos = 0; while (pos < N) { } temperatures[pos] = Double.parseDouble(in.nextLine()); pos = pos + 1; declare an array variable called temperatures whose elements will be double variables create an actual array with N=5 elements of type double access the element at position pos in array temperatures 8

Declaring Arrays Arrays are declared like normal variables with the addition of [ ] between type and name, e.g. double [] temperatures; int [] scores; boolean [] answers; String [] names; The declaration does not specify the number of elements Declaring an array does not reserve (allocate) memory for the array elements 9

Allocating The Array To allocate memory space for the array elements we need to use the new operator, e.g., temperatures = new double[10]; int numscores = 8; scores = new int[numscores]; answers = new boolean[4]; names = new String[5]; new is followed by the type of the elements and the number of elements between [ ] The number of elements can be any expression that evaluates to a positive integer value 10

Accessing Array Elements Elements of an array are accessed by using the name of the array variable followed by the index (position in the array) of the element between [ ], e.g., temperatures[2] scores[numscores-1] answers[0] names[3] The index can range between 0 and the number of elements in the array - 1 11

Array Length Once we declare and allocate an array, we can retrieve the array length (the number of elements in the array) from a variable called arrayname.length, e.g., temperatures.length is 10 scores.length is 8 answers.length is 4 names.length is 5 12

Your Turn Declare and allocate an array of 10 integers, call it a 13

Your Turn cont. Write a loop that initializes the 10 elements of the array to the integers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 14

Your Turn cont. Write a loop that outputs the elements of the array, one per line 15

Your Turn cont. Write a loop that outputs the elements of the array, one per line, in reverse order 16

Your Turn cont. Write a piece of code that checks if the elements in an array of char form a palindrome 17

For Loops A different syntax for a familiar concept They work well with arrays for (init; test; update) { for_block } 18

For Loops A different syntax for a familiar concept They work well with arrays for (init; test; update) { for_block } init while (test) { for_block update } 19

For Loops A different syntax for a familiar concept They work well with arrays for (init; test; update) { for_block } boolean expression statements statement sequence init while (test) { for_block update } 20

Example Rewrite the loop that initializes the 10 elements of an array to the integers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 int i = 0; while (i < 10) { a[i] = i + 1; i++; } 21

Your Turn (Our Old Friend) Write a for loop that given a String variable str and a character variable ch, counts and outputs the number of occurrences of character ch in String str. 22

Count Character 23

Your Turn, Again Write a program segment using for loops that given two integer variables, width and height, outputs a rectangle of + s of the given width and height. 24

Output A Rectangle Of + s 25

Arrays And Methods Arrays can be passed as parameters to methods and can be returned by methods (functions), e.g., private static void printarray(double[] a) private static void cleararray(int[] a) private static int[] copyarray(int[] a) private static int indexofsmallest( int startindex, double[] t) private static void sort(double[] temps) 26

Print Array Given an array of double, output each element on a separate line (in the order in which they occur in the array) 27

Clear Array Given an array of int, set each element of the array to 0 28

Copy Array Given an array of int, return a new array which is a copy of the given one (same length, same elements) 29