Ad

Array program(pending now a time)

Task 1

Make an array of n elements and initialize it, data and size of the array shall be given by user input. Print all the numbers, find minimum number and its index in array, find maximum number and its index in the array, find the average of all numbers.
import java.util.*;

public class Maximum
{
public static void main(String arg[])
{
int sum=0;
float avg;
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of array:");
int size = input.nextInt();
int arr[] = new int[size];
//Enter array elements
for(int i=0;i<arr.length;i++)
{
System.out.printf("Enter the array element %d:",i);
arr[i] = input.nextInt();
}
System.out.println("Array elements:");
for(int i=0;i<arr.length;i++)
{
System.out.println("\n" +arr[i]);
sum = arr[i]+sum;
}
//minimum no find
int min = arr[0];
for(int i=0;i<arr.length;i++)
{
if(min>arr[i])
min = arr[i];
}
//find maximum no
int max = arr[0];
for(int i=0;i<arr.length;i++)
{
if(max<arr[i])
max = arr[i];
}
System.out.println("The maximum no of:" +max);
System.out.println("The minimum no of:" +min);
avg = sum/size;
System.out.println("The avg of :" +avg);
}
}

output:


Task 2:
Hhow to make irrgular array:

public class Tester {
   public static void main(String[] args){
      int[][] twoDimenArray = new int[2][];

      //first row has 3 columns
      twoDimenArray[0] = new int[3];
      
      //second row has 4 columns
      twoDimenArray[1] = new int[4];

      int counter = 0;
      //initializing array
      for(int r=0; r< twoDimenArray.length; r++){
   
         for(int col=0; col < twoDimenArray[row].length; col++){
               twoDimenArray[row][col] = counter++;
         }    
      }

      //printing array
      for(int r=0; r< twoDimenArray.length; r++){
         System.out.println();
         for(int col=0; col < twoDimenArray[r].length; col++){
            System.out.print(twoDimenArray[r][col] + " ");
         }  
      }
   }   
}

output:



#How to sorting array:

import java.util.Arrays;

public class Arrays
Sorting
{
public static void main(String args[])
{
int arr1[] = {2019,450,45,4,3,4,34,234,234};
String arr2[] = {"Hashmi","Usman","Hashmi"};

System.out.println("Original Array:" +Arrays.toString(arr1));
Arrays.sort(arr1);
System.out.println("Integer Sorted Array:" +Arrays.toString(arr1));
System.out.println("\n");
System.out.println("Original Array:" +Arrays.toString(arr2));
Arrays.sort(arr2);
System.out.println("String sorted Array:" +Arrays.toString(arr2));
}

}






Post a Comment

1 Comments