Solving Array Problems in Java


class ArrayDemo {
public static void main(String[] args) {

int[] anArray; // declares an array of integers
anArray = new int[10]; // allocates memory for 10 integers
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);
                }
}
Identify and correct the syntax error in each of the following array declaration.
(a)    float arr[] = new float {1.0,2.0,3.0};
float[] arr= {1.0,2.0,3.0};

class ArrayDemo1 {
public static void main(String[] args) {
double myList[] = {5.6, 4.5, 3.3, 13.2, 4, 34.33, 34, 45.45, 99.993, 11.3};
for (int i = 0; i < myList.length; i++) {
System.out.println("Element at index" + i + ": " + myList[i]);
}
}}
Use Enchance Loop
class ArrayDemo2 {
public static void main(String[] args) {
double myList[] = {5.6, 4.5, 3.3, 13.2, 4, 34.33, 34, 45.45, 99.993, 11.3};
for (double i: myList) {
System.out.println(i);
}
}
}



Write a program that receives 6 numbers from the user, finds the largest number and counts the occurrence of the largest number entered.

Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its occurrence count is 4.



import java.util.Scanner;
class TestingArray{
public static void main(String args[]){
int[] testArr = new int[6];
Scanner input= new Scanner(System.in);
System.out.println("Enter" + testArr.length + "numbers.");
int biggest=testArr[0];
int visited = -1;
int [] fr = new int [testArr.length];
for(int i=0;i<testArr.length;i++){
System.out.print("Enter number:" + (i+1) +":" );
testArr[i] = input.nextInt();
}
for(int j=0;j<testArr.length;j++){
System.out.println("Elements:"+ testArr[j]);
}
for(int k=0;k<testArr.length;k++){
if (testArr[k]>biggest)
biggest= testArr[k];
}
System.out.print("The biggest number:"+ biggest);
for(int l = 0; l < testArr.length; l++){ 
int count = 1; 
for(int m = l+1; m < testArr.length; m++){ 
if(testArr[l] == testArr[m]){ 
count++; 
                    //To avoid counting same element again 
fr[m] = visited; 
                } 
            } 
if(fr[l] != visited) 
fr[l] = count; 
        } 
System.out.println("---------------------"); 
System.out.println(" Element | Frequency"); 
System.out.println("---------------------"); 
for(int n = 0; n < fr.length; n++){ 
if(fr[n] != visited) 
System.out.println("    " + testArr[n] + "    |    " + fr[n]); 
        } 
System.out.println("---------------------"); 

}
}

Post a Comment

0 Comments