Tutorial about Arrays

Similar documents
Arrays. Eng. Mohammed Abdualal

Object Oriented Programming. Java-Lecture 6 - Arrays

Midterm Examination (MTA)

Computer programming Code exercises [1D Array]

Loops. CSE 114, Computer Science 1 Stony Brook University

CS141 Programming Assignment #6

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Programming with Java

Instructor: Eng.Omar Al-Nahal

COMP102: Test July, 2006

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

CSE Fall 2015 Section 002 Exam 2, Time: 80 mins

Example: Monte Carlo Simulation 1

Arrays Data structures Related data items of same type Remain same size once created Fixed-length entries

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

Tutorial 8 (Array I)

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

CS 101 Spring 2007 Midterm 2 Name: ID:

1. Find the output of following java program. class MainClass { public static void main (String arg[])

LAB 13: ARRAYS (ONE DIMINSION)

CS141 Programming Assignment #5

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Java Unit 1. Using BlueJ to Write Programs

Arrays (Deitel chapter 7)

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Last Class. More on loops break continue A bit on arrays

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

AP Computer Science Unit 1. Writing Programs Using BlueJ

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Last Class. While loops Infinite loops Loop counters Iterations

Tasks for fmri-setting (Tasks of first and second pilot study at the end)

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

Passing Array to Methods

Java Coding 3. Over & over again!

Objectives of the lesson

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

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

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

13 th Windsor Regional Secondary School Computer Programming Competition

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

JAVA- PROGRAMMING CH2-EX1

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

Nested Loops. A loop can be nested inside another loop.

Question 1 [20 points]

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

CS170 (005): Introduction to Computer Science Exam 2

Example: Computing prime numbers

CIS November 14, 2017

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017

International Journal of Advanced Research in Computer Science and Software Engineering

CS141 Programming Assignment #10

Controls Structure for Repetition

Topic 12 more if/else, cumulative algorithms, printf

COMP 202 Java in one week

Chapter 8 Multi-Dimensional Arrays

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Object Oriented Programming. Java-Lecture 1

1 Short Answer (10 Points Each)

Building Java Programs

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Combined Assignment Operators. Flow of Control. Increment Decrement Operators. Operators Precedence (Highest to Lowest) Slide Set 05: Java Loops

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

COMP 202. Java in one week

CS170 Introduction to Computer Science Midterm 2

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

AP CS Unit 3: Control Structures Notes

Array. Array Declaration:

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

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

Loops and Expression Types

Types in Java. 8 Primitive Types. What if we want to store lots of items e.g. midsem marks?

Computer Science is...

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Loops. Repeat after me

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Lecture 2. Two-Dimensional Arrays

AP Computer Science Unit 1. Programs

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

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

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Section 2.2 Your First Program in Java: Printing a Line of Text

Lecture 14. 'for' loops and Arrays

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

St. Edmund Preparatory High School Brooklyn, NY

Top-Down Program Development

ITERATION WEEK 4: EXMAPLES IN CLASS

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

Declaring Array Variable

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

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

CS111: PROGRAMMING LANGUAGE II

Transcription:

Q1: Write Java statements that do the following: Reviewed Tutorial about Arrays a. Declare an array alpha of 15 components of the type int int alpha[] = new int[15]; b. Print the value of the tenth component in the array alpha System.out.print(alpha[9]); c. Set the value of the fifth component in the array alpha to 35 alpha[4]=35; d. Set the value of the ninth component of the array alpha to the sum of the sixth and thirteen component of the array alpha alpha[8]=alpha[5] +alpha[12]; e. Set the value of the fourth component of the array alpha to three time the value of the eight component of the array alpha alpha[3]=3*alpha[7] f. Print the alpha so that five components per line are printed int count =0; for(int i=0;i<alpha.length;i++) System.out.print(alpha[i]+" "); count++; if(count==5) System.out.println(); count=0; Q2: Write Java statements that do the following: a. Initialize each of the five elements of array g to 8. int[] g=new int[5]; for(int i=0;i<g.length;i++) g[i]=8;

b. Total the 100 elements of floa ng-point array c. float sum=0.0f; for(int i=0; i<c.length;i++) or sum=sum+c[i]; float sum =0.0f; for( float x : c) sum=sum+x; c. Copy 11-element array a into the first portion of array b, which contains 34 elements. for(int i=0; i<a.lenght.i++) b[i]=a[i]; Q3: what is the output of the following java code: a) int i; int [] array1 = 1,2,3; int [] array2 = 1,2,3; for(i = 0; i < array1.length ; i ++) if (array1[i] == array2[i]) System.out.println("element number " + i + " are equal "); else System.out.println("element number " + i + " are not equal "); if (array1 == array2) System.out.println(" the two array are equal "); else System.out.println("the two array are not equal "); Answer: element number 0 are equal element number 1 are equal element number 2 are equal the two array are not equal

b) int i; int [] array = 1,2,3,4,5; for (i =0 ; i<= array.length -1; i+=2) System.out.println(array[i]); 1 3 5 BUILD SUCCESSFUL (total me: 0 seconds) c) int i; int [] list =new int [5]; for (i =0 ; i<5; i++) list[i] = 2 * i +5; if (i + 2 == 0) list[i] = list[i] - 3; for (i =0 ; i<5; i++) System.out.println(list[i]); Answer: 5 7 9 11 13

d) int [] list =new int [6]; list[0] =5; for (int i =0 ; i<6; i++) list[i] = i * i +5; if (i > 2) list[i] = 2 *list[i] - list[i-1]; for (int i =0 ; i<6; i++) System.out.println(list[i]); Answer: 5 6 9 19 23 37 e) int[][] b = new int[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) b[i][j] = 2 * (i + j) + 4; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) System.out.print(b[i][j] + "\t"); System.out.println();

Answers: 4 6 8 6 8 10 8 10 12 BUILD SUCCESSFUL (total me: 0 seconds) f) public static void main(string[] args) int[] array = 87, 68, 94, 100, 83; int total = 0; for (int number : array) total += number; System.out.printf("Total of array elements: %d%n", total); g)- public class JavaApplica on17 public static void main(string[] args) int[] array = 1, 2, 3, 4, 5; modify1(array); for (int value : array) System.out.printf(" %d", value); modify2(array[3]); System.out.prin ("array[3] a er modifyelement: %d%n", array[3]); public sta c void modify1(int x[]) for (int counter = 0; counter < x.length; counter++) x[counter] *= 5; public sta c void modify2(int e)

e *= 2; System.out.printf( "\nvalue of element in modifyelement: %d%n", e); // end class h) public static void main(string[] args) int[][] array1 = 1, 2, 3, 4, 5, 6; System.out.println("Values in array1 by row are"); outputarray(array1); public static void outputarray(int[][] array) for (int row = 0; row < array.length; row++) for (int column = 0; column < array[row].length; column++) System.out.printf("%d ", array[row][column]); System.out.println();

Q4 Write a method createarithmeticsseq that prompts the user to input two numbers, first and diff. The method then creates a one-dimensional array of 16 elements ordered in an arithme c sequence. It also output the arithmetic sequence. For example if first = 21 and diff = 5, the arithmetic sequence is 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 public static void createarithmeticsseq () int first; int differ; Scanner in=new Scanner(System.in); System.out.print("Enter first:"); first =in.nextint(); System.out.print("Enter differe:"); differ=in.nextint(); int[] arr=new int[16]; for(int i=0;i<arr.length;i++) arr[i]=first; first+=differ; for(int i=0;i<arr.length;i++) System.out.print(arr[i]+ " "); Q5: Write a method that receives array and integer x, the method should search for integer in array and return the index of the integer in array. If not found the integer in array the method should return -1 public static void main(string[] args) int y=3; int[] myarray=4,3,2,1; System.out.print(Mymothod(myarray, y)); // end of main public static int Mymothod(int []arr, int x) for(int i=0;i<arr.length;i++) if (x== arr[i]) return i; return -1;