Professional Documents
Culture Documents
FA23-BSE-004 (Lab 08)
FA23-BSE-004 (Lab 08)
LAB: 08(Arrays)
Activity # 1:
Write a Java program to accept an array of 10 integer values from user
and find the largest and second largest values.
Program:
import java.util.Scanner;
public class A1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] array = new int[10];
System.out.println("Enter array elements: ");
for (int i = 0; i<10;i++) {
array[i] = sc.nextInt();
}
int largest = array[0];
for (int i = 0; i<10;i++){
if (array[i]>largest) largest = array[i];
}
int largest2;
if (array[0] == largest) largest2 = array[1];
else largest2 = array[0];
for (int i = 0; i<10; i++){
if (array[i]!= largest)
if (array[i] > largest2)
largest2 = array[i];
}
System.out.println("Largest is " + largest);
System.out.println("Second Largest is " + largest2);
}
}
OUTPUT:
Activity #02:
Write a Java program to accept 5 integer values from user. Pass this array
to a method to find the sum of the array.
Program:
import java.util.Scanner;
public class A2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[] array = new int[5];
System.out.println("Enter array elements...");
for (int i=0;i<array.length;i++)
{
array[i]=sc.nextInt();
}
System.out.println("Sum of the given array is... "+sum (array));
}
public static int sum (int[] arr) {
int s = 0;
for (int x : arr)
s += x;
return s;
}
}
OUTPUT:
Activity #03:
Write a program which takes 50 characters as input in array and counts the
occurrences of each character.
Program:
import java.util.Scanner;
public class A3 {
public static void main(String[] args) {
char[] characters = new char[10];
int size = 0;
int element = characters[0];
char[] unique = new char[size];
int counter = 0;
Scanner input = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.print("A[" + i + "] = ");
characters[i] = input.next().charAt(0);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (characters[i] == characters[j]) {
counter = counter + 1;
}
}
System.out.println(characters[i] + " occurs " + counter + " times");
counter = 0;
}
}
}
OUTPUT:
Activity# 04:
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 & smallest value in the array. (details of this functionality is given in Step
c)
import java.util.Scanner;
public class A4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Enter a number to choose menu : ");
System.out.println(" 1: For input and display the Array \n 2: For searching
Elemnt in an Array ");
System.out.println(" 3: For largest and Smallest element in the Array ");
System.out.println(" 4: For copy data to another Array");
int a = input.nextInt();
switch (a) {
case 1: {
int[] a1 = new int[1];
int[] array = (Input(a1));
display(array);
break;
}
case 2: {
int[] a1 = new int[1];
int[] array1 = (Input(a1));
System.out.println(" Enter number for search");
int searchkey = input.nextInt();
search(array1, searchkey);
break;
}
case 3: {
int[] a1 = new int[1];
int[] array11 = (Input(a1));
largest(array11);
smallest(array11);
break;
}
case 4: {
int[] a1 = {1};
int[] array1 = (Input(a1));
int[] copied = new int[10];
copydata(array1, copied);
break;
}
default:
System.out.println(" You Enter invalid number");
}
}
public static int[] Input (int[] Array) {
int[] array1 = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.println(" A[" + i + "] = ");
array1[i] = sc.nextInt();
}
return array1;
}
Task# 01:
10 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.
Program:
import java.util.Scanner;
public class T1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int average = 0;
int[] ratings = new int[10];
for (int i = 1; i <= 10 ; i++) {
System.out.print("Enter Rating "+i+" : ");
ratings[i-1] = input.nextInt();
average += ratings[i-1];
}
System.out.println();
for (int i = 1; i <= 10 ; i++) {
System.out.println(i+" Rating : "+countInstances(ratings,i));
}
System.out.println();
if ((average/10) < 5){
if ((average/10)==1)
System.out.println("Overall Ratings : Awful");
else
System.out.println("Overall Ratings : Bad");
} else if ((average/10)>5) {
if ((average/10)==10)
System.out.println("Overall Ratings : Excellent");
else
System.out.println("Overall Ratings : Good");
}
else System.out.println("Overall Ratings : Average");
}
public static int countInstances(int[] ratings,int valueToSearch) {
int count = 0;
for (int i = 0; i <= 9; i++) {
if (ratings[i] == valueToSearch) count += 1;
}
return count;
}
}
OUTPUT:
Taske# 02:
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( )
Program:
public class T2 {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i<array.length; i++){
System.out.println(modify(array)[i]);
}
}
public static int[] modify(int[] array){
int[] newArray = new int[array.length];
for (int i = 0; i<array.length; i++){
newArray[i] = 3*array[i];
}
return newArray;
}
}
OUTPUT:
Task# 03:
Write a program to copy the contents of one array into another in the reverse order.
Program:
public class T3 {
public static void main(String[] args) {
double[] numbers = {1.0,2.0,3,4,5,6,7,8,9,10};
double[] reverseArray = new double[numbers.length];
for (int i = 0; i<numbers.length; i++){
reverseArray[i] = numbers[numbers.length-i-1];
}
for (double v : reverseArray) {
System.out.println(v);
}
}
}
OUTPUT: