Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.

Slides:



Advertisements
Similar presentations
Introduction to arrays Array One dimenstional array.
Advertisements

Chapter 8: Arrays.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Working with arrays (we will use an array of double as example)
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
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)
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.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
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.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Simple algorithms on an array - compute sum and min.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Introduction to programming in java Lecture 11 Boolean Expressions and Assignment no. 2.
Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Introduction to programming in java Lecture 05 Review of 1 st four lectures and Practice Questions.
Array in C# Array in C# RIHS Arshad Khan
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Building Java Programs
CSC 142 Computer Science II
OPERATORS (1) CSC 111.
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Yong Choi School of Business CSU, Bakersfield
CSC 113 Tutorial QUIZ I.
Java Tutotrial for [NLP-AI] 2
AP Java Warm-up Boolean Array.
Introduction To Programming Information Technology , 1’st Semester
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Arrays in Java.
CS149D Elements of Computer Science
Building Java Programs
More on iterations using
Lecture 20 – Practice Exercises 4
Presentation transcript:

Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3

Initializer Lists Declare Array – Syntax: type[] arrayName = new type[length] – Example: int[] studentID=new int[10]; Initializer List – You can declare, construct, and initialize the array all in one statement: – Example: int[] studentID={123,234,345,456,567,678,789,890,901}

Fill in the blanks so that the values in valA are copied into the corresponding cells of valB. class ArrayExp { public static void main(String[] args) { int[] valA = {12,23,45,56}; int valB = new int[4]; valB[0] = valA[0]; valB[1] = valA[1]; valB[2] = valA[2]; valB[3] = valA[3]; }}

Complete the assignment statement so that it computes the sum of all the numbers in the array. class Exercise1 { public static void main ( String[] args ) { int[] val = {0, 1, 2, 3}; sum = _________________________________ System.out.println( "Sum of all numbers = " + sum ); } val[0] + val[1] + val[2] + val[3] ;

The length of an Array What is the length of the following array? – int[] egArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }; We can use length instance variable. Examples System.out.println(“Length of egArray is “ + egArray.length); for ( int index= 0 ; index < egArray.length; index++ )

Practice Question # 1 Set up an array to hold the following values, and in this order: – 3, 1, 5, 7, 4, 12, -3, 8, -2. Write a program which identifies the largest number in the array.

Solution Practice more by yourself: find smallest number

Practice Question # 2 Set up a string array that contains name of the FCIT departments: – IT, CS, IS Write a program which takes department name from the user then program check whether the user provided department belongs to FCIT college or not.

Homework Assignment # 3 (Total marks: 5) Submission deadline: 19 th Nov 2013 NOTES: 1.Only HAND WRITTEN assignments are acceptable on A4 size white paper. 2.If any student copy assignment from other then -5 marks will be deducted from the overall grade. 3.Late submission is not allowed.

Question 1 Ask the user to enter in a number. Print out that many *’ s on the screen without spaces or newlines. Example: Please enter a number: 7 *******

Question 2 Write a program which prompts user to enter any sentence. Program will then print the whole sentence in reverse order. Example: Enter sentence: I like java programming..gnimmargorp avaj ekil I

Question 3 Ask the user to enter in a number. Write a program to find out if a number is prime or not? Example Enter number is a prime number.

Question 4 Examine the following program: Complete the program with four assignment statements so that each cell of sum contains the sum of the corresponding cells in valA and valB. I.e., add cell zero of valA to cell zero of valB and put the result in cell zero of sum, and so on.

Question 5 Define an integer array that contains the following values: int[] data = {3, 2, 5, 7, 9, 12, 97, 24, 54}; Write a program that computes – the sum of all the elements of the array, – the sum of the even elements, and – the sum of the odd elements.