Wipro Training Assignment
Java Fundamentals (Arrays) DAY 3
Name = Harshit More
Enrollment = 0103CS193D05
Q1. Write a program to initialize an integer array and print the sum and
average of the array
package arrays;
import java.util.*;
public class simple_array
{
public static void main(String[] args)
{
int [] array1 = new int[] {1,2,3,4,5};
int sum = 0;
float avg = 0;
for(int i = 0;i<array1.length;i++)
{
sum = array1[i]+ sum;
}
avg = sum/array1.length;
System.out.println("Sum of array is "+sum);
System.out.println("Average of the array is "+avg);
}
}
Q2. Write a program to initialize an integer array and find the maximum
and minimum value of an array
package arrays;
import java.util.*;
public class MAX_MIN_ARRAY
{
public static void main(String[] args)
{
int [] array1= new int[] {20,30,40,50,60};
int max = 0;
int min= array1[0];
for(int i = 0; i <array1.length;i++)
{
if(array1[i]>=max)
{
max = array1[i];
}
else if(array1[i]<=min)
{
min = array1[i];
}
}
System.out.println("Max element in array = "+max);
System.out.println("Min element in array = "+min);
Q3. Write a program to initialize an integer array with values and check if
a given number is present in the array or not. If the number is not found, it
will print -1 else it will print the index value of the given number in the
array
Ex1) Array elements are {1,4,34,56,7} and the search element is 90
O/P: -1
Ex2)Array elements are {1,4,34,56,7} and the search element is 56
O/P: 4
package arrays;
import java.util.*;
public class ARRAY_SEARCH
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int [] array1 = new int[] {1,4,34,6,7};
System.out.println("Enter the number to be searched : ");
int key = sc.nextInt();
int index=0;
int flag=0;
for(int i = 0; i<array1.length;i++)
{
if(array1[i]==key)
{
index= i;
flag = 1;
else
{
continue;
}
}
if(flag!=0)
{
System.out.println("Index of the searched element is
"+index);
}
else
{
System.out.println("-1");
}
}
}
Q4. Initialize an integer array with ascii values and print the corresponding
character values in a single row.
package arrays;
import java.util.*;
public class ASSIC_ARRAY
{
public static void main(String[] args)
{
int [] array1= new int [] {65,66,67,68,69,70};
for(int i = 0; i<array1.length;i++)
{
System.out.println((char)array1[i]);
}
Q5. Write a program to find the largest 2 numbers and the smallest 2
numbers in the given array
package arrays;
import java.util.*;
public class LARGEST_SMALLEST
{
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int largest[] = new int[2];
int smallest[] = new int[2];
boolean taken[] = new boolean[arr.length];
for (int j = 0; j < 2; j++) {
int max = 0, id = 0;
for (int i = 0; i < arr.length; i++) {
if (max < arr[i] && !taken[i]) {
max = arr[i];
id = i;
}
}
taken[id] = true;
largest[j] = max;
}
for (int i = 0; i < taken.length; i++)
taken[i] = false;
for (int j = 0; j < 2; j++) {
int min = Integer.MAX_VALUE, id = 0;
for (int i = 0; i < arr.length; i++) {
if (min > arr[i] && !taken[i]) {
min = arr[i];
id = i;
}
}
taken[id] = true;
smallest[j] = min;
}
System.out.println("Largest: " + largest[0] + ", " + largest[1]);
System.out.println("Smallest: " + smallest[0] + ", " + smallest[1]);
Q6. Write a program to initialize an array and print them in a sorted
fashion
package arrays;
import java.util.*;
public class ARRAY_SORT
{
public static void main(String[] args)
{
int arr[] = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
for(int i = 0;i<arr.length;i++)
{
for(int j =i+1;j<arr.length;j++)
{
if(arr[j]<arr[i])
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
Q7. Write a program to remove the duplicate elements in an
array and print
Eg) Array Elements--12,34,12,45,67,89
O/P: 12,34,45,67,89
package arrays;
import java.util.*;
public class REPEAT_ARRAY
{
public static void main(String[] args)
{
int arr[] = { 1, 2, 4, 5, 6, 7, 4, 5, 4 };
int l = arr.length;
for (int i = 0; i < arr.length; i++) {
boolean flag = false;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
flag = true;
}
}
if (flag) {
for (int j = i; j < arr.length-1; j++) {
arr[i] = arr[i+1];
}
l--;
}
}
//printing array
for (int i = 0; i < l; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Q8. Write a program to print the element of an array that has occurred the highest
number of times
Eg) Array -> 10,20,10,30,40,100,99
O/P:10
package arrays;
import java.util.*;
public class REPEAT_ARRAY
{
public static void main(String[] args)
{
int arr[] = { 10, 20, 10, 30, 40, 100, 99 };
int max = 0, id = 0;
for (int i = 0; i < arr.length; i++) {
int c =0;
for (int j = 0; j < arr.length; j++) {
if(arr[i] == arr[j])
c++;
}
if(max < c) {
max = c;
id = i;
}
}
System.out.println(arr[id]);
}
}
Q9. Write a program to print the sum of the elements of the array with the given below
condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers
between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10
package arrays;
import java.util.*;
public class CLASS
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter size of array: ");
int size = in.nextInt();
int arr[] = new int[size];
System.out.print("Enter array elements: ");
boolean six = false, seven = false;
int id_six = -1, id_seven = arr.length;
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
if (arr[i] == 6) {
six = true;
id_six = i;
}
if (six && arr[i] == 7) {
seven = true;
id_seven = i;
}
}
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if (six && seven) {
if (i < id_six || i > id_seven)
sum += arr[i];
} else
sum += arr[i];
}
System.out.println(sum);
Q10. Write a program to reverse the elements of a given 2*2 array. Four integer
numbers needs to be passed as Command Line arguments.
Example1:
C:\>java Sample 1 2 3
O/P Expected : Please enter 4 integer numbers
Example2:
C:\>java Sample 1 2 3 4
O/P Expected :
The given array is :
12
34
The reverse of the array is :
43
21
1 2 3 4
5 6 7 8
9 10 11 12
package arrays;
import java.util.*;
public class REVERSE_CLASS
{
public static void main(String[] args)
{
if (args.length != 4)
System.out.println("Please enter 4 integer numbers");
else {
int arr[][] = new int[2][2];
for (int i = 0, k = 0; i < 2; i++) {
for (int j = 0; j < 2; j++, k++) {
arr[i][j] = Integer.parseInt(args[k]);
}
}
System.out.println("The given array is :");
//printing array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
// reversing rows;
for (int i = 0; i < 2; i++) {
int tmp = arr[0][i];
arr[0][i] = arr[1][i];
arr[1][i] = tmp;
}
//reversing columns
for (int i = 0; i < 2; i++) {
int tmp = arr[i][0];
arr[i][0] = arr[i][1];
arr[i][1] = tmp;
}
//printing reverse array
System.out.println("The reverse of the array is :");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
}
Q11. Write a program to find greatest number in a 3*3 array. The program is supposed
to receive 9 integer numbers as command line arguments.
Example1:
C:\>java Sample 1 2 3
O/P Expected : Please enter 9 integer numbers
Example2:
C:\>java Sample 1 23 45 55 121 222 56 77 89
O/P Expected :
The given array is :
1 23 45
55 121 222
56 77 89
The biggest number in the given array is 222
class Solution {
public static void main(String[] args) {
if(args.length != 9)
System.out.println("Please enter 9 integer numbers");
else {
int arr[][] = new int[3][3];
int max = 0;
for(int i=0, k = 0; i<3; i++) {
for(int j=0; j<3; j++, k++) {
arr[i][j] = Integer.parseInt(args[k]);
}
}
System.out.println("The given array is :");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
System.out.printf("%3d ", arr[i][j]);
}
System.out.println();
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(max < arr[i][j])
max = arr[i][j];
}
}
System.out.println("The biggest number in the given array is " + max);
}
}
}