Arrays (1-D & 2-D) and Passing arrays to methods
Activity Outcomes:
This lab teaches you the following topics:
Create Arrays
Accessing indexes/location of variables in arrays
Accessing maximum, minimum numbers in arrays
Different array’s operation
Passing arrays to methods
Introduction
Often you will have to store a large number of values during the execution of a program. Suppose, for instance, that you need to read 100 numbers, compute their average, and find out how many numbers are above the average. Your program first reads the numbers and computes their average, then compares each number with the average to determine whether it is above the average. In order to accomplish this task, the numbers must all be stored invariables. You have to declare 100 variables and repeatedly write almost identical code100 times. Writing a program this way would be impractical. So, how do you solve this problem?
An efficient, organized approach is needed. Java and most other high-level languages provide a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. In the present case, you can store all 100 numbers into an array and access them through a single array variable.
Array is a data structure that represents a collection of the same types of data.
The array elements are accessed through the index. The array indices are 0-based, i.e., it starts from 0 to arrayRefVar.length-1.
• Declaring arrays:
elementType[] arrayRefVar;
The elementType can be any data type, and all elements in the array will have the same data type. For example, the following code declares a variable myList that references an array of double elements.
double[] myList;
• Creating arrays:
arrayRefVar = new elementType[arraySize];
Lab Activities:
Activity 1:
Create a menu driven program, with the following functionalities: (Note: all of the functionalities should be created in a single program with following menu options)
1. Input elements in array. (details of this functionality is given in Step a)
2. Search element and its location. (details of this functionality is given in Step b)
3. Find largest and smallest value in the array. (details of this functionality is given in Step c)
4. Copy data. (details of this functionality is given in Step d)
a) Input 10 elements in the array and display the array. (Note: this should call two methods Input(int Array[ ] ) and display(int A[ ]) )
b) Search element is in the array then print “Element found” along with its location.
(Note: this should call two methods Input(int Array[ ]) and search(intsearchkey, int Array[ ]). You should call the same Input() method that is called in step a )
c) Find the largest and the smallest element in array. Then place largest element on 0th index and smallest element on the last index 9th. (Note: this should call three methods previously used Input(int Array[]) , Largest(int
Array[]) and Smallest (int Array[])
d) Copy the contents of one array into another.(Note: this should call two methods Input(int Array[]) and copydata(int Array[], intcopiedArray[]).
Solution:
You will get the following output.
Activity 2:
Write a program which takes 50 characters as input in array and counts the occurrences of each character.
E.g.
Solution:
You will get the following output.
Activity 3:
Write a program to insert elements in 2D array. Then print the array in matrix form.
Solution:
You will get the following output.
Activity 4:
Write a program that reads two 2-D arrays of 3X3 each and Swap their values and then print both matrices.
Solution:
You will get the following output.
Activity 5:
Program below will show the difference between passing a primitive datatype value and an array reference variable to a method. The program contains two methods for swapping elements in an array.
The first method, named swap, fails to swap two int arguments.
The second method, named swapFirst-TwoInArray, successfully swaps the first two elements in the array argument.
Solution:
You will get the following output.
Home Activities:
1. Forty students were asked to rate the quality of food in the student cafeteria, on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the forty responses in an integer array and summarize the results of the poll.
2. Write a program which performs the following tasks:
• initialize an integer array of 10 elements in main( )
• Pass the entire array to a function modify( )
• In modify( ) multiply each element of array by 3
• return the control to main( ) and print the new array elements in main( )
3. Write a program to copy the contents of one array into another in the reverse order.