Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
King Fahd University of Petroleum & Minerals Information and Computer Science Department ICS102: Introduction to Computing Lab 9: Arrays I Objectives Designing and implementing Java programs that deal with: 1. Arrays a. Declaring Arrays’ Objects. b. length variable. Introduction An array can be declared in two ways: 1. type name [] = {value 1, value 2,value 3, . . .} Example: int m [][] = {1, 2, 3, 4}; 1 2 3 4 2. type name [] = new type [size] Example: int n [] = new int [4]; n has 4 elements but not initilzed yet. (default value is assigned to each element) 0 0 0 0 To initialize or process an array you need a loop: for(int i = 0; i < array.length; i++) Do something with array[i] Example: The following program declares an array with 7 elements of type int and finds the maximum. /* This program finds the maximum in an integer array*/ public class Ex { public static void main(String [] args) { int [] score = {83, 76, 87, 98, 54, 65, 90}; int max = 0; for(int i = 1; i < score.length; i++){ if(score[i] > score[max]) max = i; } System.out.println("the maximum score is " + score[max]); } } KFUPM, ICS Department ICS102: Introduction to Computing 2/4 Lab9: Arrays I Exercises Exercise #01: (Ex.java) Change the code in the example such that the array score is initialized by the user input (using Scanner class). Then, the program should find the minimum score and subtract it from every element in the array. Print the array after subtraction. Solution:import java.util.Scanner ; public class Ex { public static void main(String [] args) { Scanner keyboard = new Scanner (System.in); int [] score = new int [7]; int min = 0; System.out.println ("Enter 7 integers:-"); for(int i = 0; i < score.length; i++){ score [i] = keyboard.nextInt(); } for (int i=1 ; i < score.length ; i++) if (score[i] < score[min]) min = i ; for (int i=0 ; i < score.length ; i++) score[i] -= score[min]; System.out.println("The elements of the array after the substraction are:-"); for (int i=0 ; i<score.length ; i++) System.out.print (score[i]+" "); } } Exercise #02: (Histogram.java) Given the following array, display its data graphically by plotting each numeric value as a bar of asterisks (*) as shown in the diagram. int[] array = {10, 19, 5, 1, 7, 14, 0, 7, 5}; KFUPM, ICS Department ICS102: Introduction to Computing 3/4 Lab9: Arrays I Solution:public class Histogram { public static void main (String[] args){ int[] array = {10, 19, 5, 1, 7, 14, 0, 7, 5}; System.out.println("Element Value Histogram"); for (int i=0 ; i < array.length ; i++) { System.out.print (i + "\t\t " + array[i] + "\t\t"); for (int j=0;j<array[i];j++) System.out.print("*"); System.out.println(); } } } Exercise #03: (ArrayMult.java) Design and implement a java code that should reads two integer arrays A and B each of size 4 from the user. The program then computes the product of corresponding elements in both arrays and stores the result in an integer array C of the same size. Your program should then print the array C. Test your program with the following data: 2 4 3 3 5 6 2 4 Your output should be: ARRAY A: 2 4 ARRAY B: 5 6 ARRAY C: 10 24 3 2 6 3 4 12 Solution:import java.util.Scanner; public class ArrayMult { public static void main (String[] args) { Scanner keyboard = new Scanner (System.in); int[] A = new int[4]; int[] B = new int[4]; KFUPM, ICS Department ICS102: Introduction to Computing 4/4 int[] C = new int[4]; System.out.println("Enter 4 integers"); for (int i=0 ; i<A.length ; i++) A[i]=keyboard.nextInt(); System.out.println("Enter another 4 integers"); for (int i=0 ; i<B.length ; i++) B[i]=keyboard.nextInt(); System.out.println ("The product of the corresponding integers"); for (int i=0 ; i<C.length ; i++) C[i] = A[i]*B[i]; System.out.print("ARRAY A: "); for (int i=0 ; i<A.length ; i++) System.out.print(A[i]+" "); System.out.println(); System.out.print("ARRAY B: "); for (int i=0 ; i<B.length ; i++) System.out.print(B[i]+" "); System.out.println(); System.out.print("ARRAY C: "); for (int i=0 ; i<C.length ; i++) System.out.print(C[i]+" "); } } Lab9: Arrays I