Chapter6 (4) (1).pptx plog fix down more

mohammadalali41 5 views 16 slides Apr 27, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

find the square root of each member of the vector


Slide Content

Chapter 6 Single-Dimensional Arrays

Introduction An array is a data structure that can be used to store a collection of data (elements) of the same type . - Suppose, for instance, that you need to read 100 numbers, compute their average, and find out how many numbers are above the average -Instead of declaring individual variables, such as number_0, number_1, . . . , and number_99, you declare one array variable such as numbers and use numbers[0], numbers[1], . . . , and numbers[99] to represent individual variables. - it more useful to think of an array as a collection of variables of the same type. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Introduction The array named number will look like following: number[99] number[98] number[97] ..... number[ i ] ..... number[2] number[1] number[0] 90 72 88 ….. 60 ….. 55 45 70 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Declaration The syntax for declaring an array variable is: Example : - The array can be any data type, and all elements in the array will have the same data type. datatype [ ] arrayName ; // or datatype arrayName [ ]; double [ ] marks; // double marks [ ]; int [ ] number; // int number [ ]; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Creation The syntax for creating the array is: The array_size specifies the number of elements that can be stored in this array. Example : - The size of an array cannot be changed after the array is created arrayName = new datatype [ array_size ]; marks = new double [45]; number= new int [100]; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Declaration and Creation - Declaring and creating an array can be combined in one statement. - The syntax for declaring and creating an array in one statement is: Example: datatype [ ] arrayName = new datatype [ array_size ]; or datatype arrayName [ ] = new datatype [ array_size ]; double [ ] marks = new double [45]; int number [ ] = new int [100]; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Declaration and Creation We have declared and created the array, but we haven’t filled it yet!. - The syntax for assigning (storing) a value at specific index in an array is: - The index represents the position where the value will be stored in. - The value must be in the data type of the array. Example : marks[4] = 72.0; marks[30] = 87.0; arrayName [index] = value; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Declaration and Creation Example: ---------------------------------------------------------------------------------- num 4 3 2 1 23 41 -13 18 4 vowels 4 3 2 1 ‘u’ ‘o’ ‘ i ’ ‘e’ ‘a’ char [] vowels = new char[5]; vowels[0]= ‘a’; vowels[1]= ‘e’; vowels[2]= ‘ i ’; vowels[3]= ‘o’; vowels[4]= ‘u; int [] num=new int [5]; num[0] = 4; num[1] = 18; num[2] = -13; num[3] = 41; num[4] = 23; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Default Values - When an array is created, its elements are assigned the default values as following: default value data type zero numeric data types space char data type false boolean data type null String data type Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Initializers - Java has a shorthand notation, known as the array initializer , which combines the declaration, creation, and initialization of an array in one statement. The syntax is: Example: - Array initializing has to be written in one statement. The following causes syntax error: datatype [ ] arrayName = {value_1 , value_2 , … , value_n }; int [ ] num; num={4 , 18 , -13 , 41 , 23}; int [ ] num= {4 , 18 , -13 , 41 , 23}; char [ ] vowels= { ‘a’ , ‘e’ , ‘ i ’ , ‘o’ , ‘u’}; Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Size The syntax for finding the size of an array is: Example : arrayName.length ; int [ ] num= {4 , 18 , -13 , 41 , 23}; int size= num.length ; // size=5 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Array Index -The array elements are accessed through the index. Array indices are zero based; that is, they range from zero to arrayName.length-1. - Each element in the array is represented using the following syntax: Example : After an array is created, an indexed variable can be used in the same way as a regular variable. Example: arrayName [index]; int [ ] num= {4 , 18 , -13 , 41 , 23}; System.out.println (num[3]); // 41 num[2] = num[0]+num[1]; // num[2] = 22 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Processing Arrays When processing array elements, you will often use a for loop. Example : int [ ] num= {4 , 18 , -13 , 41 , 23}; for( int i =0 ; i < num.length ; i ++){ System.out.println (num[ i ]); } Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Processing Arrays Q: write a program that defines an integer array of size 10, and prompts the user to enter 10 numbers, stores it in an array and prints the numbers, and average of the numbers. Q: write a program to store the following temperatures in an array 25 , 14 , 34 , 8 , 14 , 7 , 32 , 18 , 15 , 22 , 40 , 38 , 14 , 21 , 28 , 40 , 19. then find the following: 1. Print all the temperatures. 2. Find the sum and average of the temperatures. 3. Find the smallest temperature. 4. Find the largest temperature. 5. Find the index of temperature 32. 6. Find the smallest index of largest element. 7. How many times temperature 14 was repeated. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Processing Arrays Q: write a program that prompts the user to enter his name, and puts the name characters in an array. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein

Copying Arrays To copy the contents of one array, you have to create another array of the same size and data type, then by using for loop you can copy the elements of the first array to its corresponding indices in the second array. Q: write a program to copy the following array into another one: 10.8 93 75.3 22 19 60 88 14.5 5 12.4 Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 MODIFIED BY: Abla Hussein