SlideShare a Scribd company logo
ArrayTest.java
import java.util.Arrays;
import java.util.Scanner;
public class ArrayTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int table[] = new int[10];
int count = 0;
Scanner scan = new Scanner(System.in);
for(int i=0; i<10; i++){
System.out.println("Enter Array Element:");
int n = scan.nextInt();
if(n < 0){
System.out.println("Invalid Number.");
i--;
if(count >= 5)
break;
}else{
table[i] = n;
count++;
}
}
int sum = 0;
System.out.println("Array Elements are: "+Arrays.toString(table));
for(int i=0; i avg){
high++;
}
else{
equal++;
}
}
System.out.println("Number of elements less than average "+less);
System.out.println("Number of elements greater than average "+high);
System.out.println("Number of elements equal to average "+equal);
for(;;){
System.out.println("Enter Search Element (-1 to quit): ");
int searchNum = scan.nextInt();
if(searchNum == -1){
break;
}
int index = find(searchNum, table, count);
if(index == -1){
System.out.println("Element not found");
}
else{
System.out.println("Element found at "+index);
}
}
smallest(table, count);
}
public static int find (int valueToSearchFor, int intArray[ ], int numberOfValidEnties){
for(int i=0; i a[i]){
min = a[i];
index = i;
}
}
System.out.println("The smallest element is "+min+" index is "+index);
int temp = a[0];
a[0] = a[index];
a[index] = temp;
System.out.println("After swap array elements are :"+Arrays.toString(a));
}
}
Output:
Enter Array Element:
3
Enter Array Element:
4
Enter Array Element:
5
Enter Array Element:
2
Enter Array Element:
1
Enter Array Element:
6
Enter Array Element:
7
Enter Array Element:
8
Enter Array Element:
-1
Invalid Number.
Array Elements are: [3, 4, 5, 2, 1, 6, 7, 8, 0, 0]
The Sum is 36
The Average is : 4.5
Number of elements less than average 4
Number of elements greater than average 4
Number of elements equal to average 0
Enter Search Element (-1 to quit):
2
Element found at 3
Enter Search Element (-1 to quit):
3
Element found at 0
Enter Search Element (-1 to quit):
1
Element found at 4
Enter Search Element (-1 to quit):
6
Element found at 5
Enter Search Element (-1 to quit):
-1
The smallest element is 1 index is 4
After swap array elements are :[1, 4, 5, 2, 3, 6, 7, 8, 0, 0]
Solution
ArrayTest.java
import java.util.Arrays;
import java.util.Scanner;
public class ArrayTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int table[] = new int[10];
int count = 0;
Scanner scan = new Scanner(System.in);
for(int i=0; i<10; i++){
System.out.println("Enter Array Element:");
int n = scan.nextInt();
if(n < 0){
System.out.println("Invalid Number.");
i--;
if(count >= 5)
break;
}else{
table[i] = n;
count++;
}
}
int sum = 0;
System.out.println("Array Elements are: "+Arrays.toString(table));
for(int i=0; i avg){
high++;
}
else{
equal++;
}
}
System.out.println("Number of elements less than average "+less);
System.out.println("Number of elements greater than average "+high);
System.out.println("Number of elements equal to average "+equal);
for(;;){
System.out.println("Enter Search Element (-1 to quit): ");
int searchNum = scan.nextInt();
if(searchNum == -1){
break;
}
int index = find(searchNum, table, count);
if(index == -1){
System.out.println("Element not found");
}
else{
System.out.println("Element found at "+index);
}
}
smallest(table, count);
}
public static int find (int valueToSearchFor, int intArray[ ], int numberOfValidEnties){
for(int i=0; i a[i]){
min = a[i];
index = i;
}
}
System.out.println("The smallest element is "+min+" index is "+index);
int temp = a[0];
a[0] = a[index];
a[index] = temp;
System.out.println("After swap array elements are :"+Arrays.toString(a));
}
}
Output:
Enter Array Element:
3
Enter Array Element:
4
Enter Array Element:
5
Enter Array Element:
2
Enter Array Element:
1
Enter Array Element:
6
Enter Array Element:
7
Enter Array Element:
8
Enter Array Element:
-1
Invalid Number.
Array Elements are: [3, 4, 5, 2, 1, 6, 7, 8, 0, 0]
The Sum is 36
The Average is : 4.5
Number of elements less than average 4
Number of elements greater than average 4
Number of elements equal to average 0
Enter Search Element (-1 to quit):
2
Element found at 3
Enter Search Element (-1 to quit):
3
Element found at 0
Enter Search Element (-1 to quit):
1
Element found at 4
Enter Search Element (-1 to quit):
6
Element found at 5
Enter Search Element (-1 to quit):
-1
The smallest element is 1 index is 4
After swap array elements are :[1, 4, 5, 2, 3, 6, 7, 8, 0, 0]

More Related Content

Similar to ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf

import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
apexelectronices01
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
aravlitraders2012
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
optokunal1
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
stopgolook
 
Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdf
gopalk44
 
import java-util-ArrayList- import java-util-Collections- import java-.pdf
import java-util-ArrayList- import java-util-Collections- import java-.pdfimport java-util-ArrayList- import java-util-Collections- import java-.pdf
import java-util-ArrayList- import java-util-Collections- import java-.pdf
adhityalapcare
 
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
arpaqindia
 
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdfWrite the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
arihantmum
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
fashioncollection2
 

Similar to ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf (15)

import java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdfimport java.util.Scanner;public class Digits { public static v.pdf
import java.util.Scanner;public class Digits { public static v.pdf
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
import java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdfimport java.util.Scanner;public class Main {private static i.pdf
import java.util.Scanner;public class Main {private static i.pdf
 
Java Assignment.Implement a binary search algorithm on an array..pdf
Java Assignment.Implement a binary search algorithm on an array..pdfJava Assignment.Implement a binary search algorithm on an array..pdf
Java Assignment.Implement a binary search algorithm on an array..pdf
 
Oot practical
Oot practicalOot practical
Oot practical
 
Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdf
 
import java-util-ArrayList- import java-util-Collections- import java-.pdf
import java-util-ArrayList- import java-util-Collections- import java-.pdfimport java-util-ArrayList- import java-util-Collections- import java-.pdf
import java-util-ArrayList- import java-util-Collections- import java-.pdf
 
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
 
Code javascript
Code javascriptCode javascript
Code javascript
 
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdfWrite the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
Write the code above and the ones below in netbeans IDE 8.13. (Eli.pdf
 
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdfWrite a method called uniqueNumbers that takes an int array as param.pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
 

More from deepua8

Balancing ANY chemical equation is done exactly t.pdf
                     Balancing ANY chemical equation is done exactly t.pdf                     Balancing ANY chemical equation is done exactly t.pdf
Balancing ANY chemical equation is done exactly t.pdf
deepua8
 
A) ionic compounds generally formed between the c.pdf
                     A) ionic compounds generally formed between the c.pdf                     A) ionic compounds generally formed between the c.pdf
A) ionic compounds generally formed between the c.pdf
deepua8
 
The fact that a molecule vibrates does not in its.pdf
                     The fact that a molecule vibrates does not in its.pdf                     The fact that a molecule vibrates does not in its.pdf
The fact that a molecule vibrates does not in its.pdf
deepua8
 

More from deepua8 (20)

Balancing ANY chemical equation is done exactly t.pdf
                     Balancing ANY chemical equation is done exactly t.pdf                     Balancing ANY chemical equation is done exactly t.pdf
Balancing ANY chemical equation is done exactly t.pdf
 
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
 
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
 
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
 
1. 252.125Solution1. 252.125.pdf
1. 252.125Solution1. 252.125.pdf1. 252.125Solution1. 252.125.pdf
1. 252.125Solution1. 252.125.pdf
 
(B) 0.815Solution(B) 0.815.pdf
(B) 0.815Solution(B) 0.815.pdf(B) 0.815Solution(B) 0.815.pdf
(B) 0.815Solution(B) 0.815.pdf
 
Suppose AFnSolution Suppose AFn.pdf
 Suppose  AFnSolution Suppose  AFn.pdf Suppose  AFnSolution Suppose  AFn.pdf
Suppose AFnSolution Suppose AFn.pdf
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
Definition of Log-Normal DistributionA statistical distr.pdf
  Definition of Log-Normal DistributionA statistical distr.pdf  Definition of Log-Normal DistributionA statistical distr.pdf
Definition of Log-Normal DistributionA statistical distr.pdf
 
Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq) Ste.pdf
                     Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq)  Ste.pdf                     Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq)  Ste.pdf
Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq) Ste.pdf
 
The compounds with low oxidation states (O.S.) be.pdf
                     The compounds with low oxidation states (O.S.) be.pdf                     The compounds with low oxidation states (O.S.) be.pdf
The compounds with low oxidation states (O.S.) be.pdf
 
A) ionic compounds generally formed between the c.pdf
                     A) ionic compounds generally formed between the c.pdf                     A) ionic compounds generally formed between the c.pdf
A) ionic compounds generally formed between the c.pdf
 
A ethers ethers contain R-O-R linkage not carbony.pdf
                     A ethers ethers contain R-O-R linkage not carbony.pdf                     A ethers ethers contain R-O-R linkage not carbony.pdf
A ethers ethers contain R-O-R linkage not carbony.pdf
 
The fact that a molecule vibrates does not in its.pdf
                     The fact that a molecule vibrates does not in its.pdf                     The fact that a molecule vibrates does not in its.pdf
The fact that a molecule vibrates does not in its.pdf
 
The compounds of interest are Na2S and H2SO4. Th.pdf
                     The compounds of interest are Na2S and H2SO4.  Th.pdf                     The compounds of interest are Na2S and H2SO4.  Th.pdf
The compounds of interest are Na2S and H2SO4. Th.pdf
 
sucrose has a formula of C12H22O11while ammonia h.pdf
                     sucrose has a formula of C12H22O11while ammonia h.pdf                     sucrose has a formula of C12H22O11while ammonia h.pdf
sucrose has a formula of C12H22O11while ammonia h.pdf
 
reduction strength Fe Pb As .pdf
                     reduction strength Fe  Pb  As                 .pdf                     reduction strength Fe  Pb  As                 .pdf
reduction strength Fe Pb As .pdf
 
sp3d since it has 1s 3p and 1d orbitals available.pdf
                     sp3d since it has 1s 3p and 1d orbitals available.pdf                     sp3d since it has 1s 3p and 1d orbitals available.pdf
sp3d since it has 1s 3p and 1d orbitals available.pdf
 
No standard potential data given. .pdf
                     No standard potential data given.                .pdf                     No standard potential data given.                .pdf
No standard potential data given. .pdf
 
x2 + 4xSolutionx2 + 4x.pdf
x2 + 4xSolutionx2 + 4x.pdfx2 + 4xSolutionx2 + 4x.pdf
x2 + 4xSolutionx2 + 4x.pdf
 

Recently uploaded

Recently uploaded (20)

Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 

ArrayTest.java import java.util.Arrays; import java.util.Scann.pdf

  • 1. ArrayTest.java import java.util.Arrays; import java.util.Scanner; public class ArrayTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int table[] = new int[10]; int count = 0; Scanner scan = new Scanner(System.in); for(int i=0; i<10; i++){ System.out.println("Enter Array Element:"); int n = scan.nextInt(); if(n < 0){ System.out.println("Invalid Number."); i--; if(count >= 5) break; }else{ table[i] = n; count++; } } int sum = 0; System.out.println("Array Elements are: "+Arrays.toString(table)); for(int i=0; i avg){ high++; } else{ equal++; } }
  • 2. System.out.println("Number of elements less than average "+less); System.out.println("Number of elements greater than average "+high); System.out.println("Number of elements equal to average "+equal); for(;;){ System.out.println("Enter Search Element (-1 to quit): "); int searchNum = scan.nextInt(); if(searchNum == -1){ break; } int index = find(searchNum, table, count); if(index == -1){ System.out.println("Element not found"); } else{ System.out.println("Element found at "+index); } } smallest(table, count); } public static int find (int valueToSearchFor, int intArray[ ], int numberOfValidEnties){ for(int i=0; i a[i]){ min = a[i]; index = i; } } System.out.println("The smallest element is "+min+" index is "+index); int temp = a[0]; a[0] = a[index]; a[index] = temp; System.out.println("After swap array elements are :"+Arrays.toString(a)); } } Output:
  • 3. Enter Array Element: 3 Enter Array Element: 4 Enter Array Element: 5 Enter Array Element: 2 Enter Array Element: 1 Enter Array Element: 6 Enter Array Element: 7 Enter Array Element: 8 Enter Array Element: -1 Invalid Number. Array Elements are: [3, 4, 5, 2, 1, 6, 7, 8, 0, 0] The Sum is 36 The Average is : 4.5 Number of elements less than average 4 Number of elements greater than average 4 Number of elements equal to average 0 Enter Search Element (-1 to quit): 2 Element found at 3 Enter Search Element (-1 to quit): 3 Element found at 0 Enter Search Element (-1 to quit): 1 Element found at 4 Enter Search Element (-1 to quit): 6
  • 4. Element found at 5 Enter Search Element (-1 to quit): -1 The smallest element is 1 index is 4 After swap array elements are :[1, 4, 5, 2, 3, 6, 7, 8, 0, 0] Solution ArrayTest.java import java.util.Arrays; import java.util.Scanner; public class ArrayTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int table[] = new int[10]; int count = 0; Scanner scan = new Scanner(System.in); for(int i=0; i<10; i++){ System.out.println("Enter Array Element:"); int n = scan.nextInt(); if(n < 0){ System.out.println("Invalid Number."); i--; if(count >= 5) break; }else{ table[i] = n; count++; } } int sum = 0; System.out.println("Array Elements are: "+Arrays.toString(table));
  • 5. for(int i=0; i avg){ high++; } else{ equal++; } } System.out.println("Number of elements less than average "+less); System.out.println("Number of elements greater than average "+high); System.out.println("Number of elements equal to average "+equal); for(;;){ System.out.println("Enter Search Element (-1 to quit): "); int searchNum = scan.nextInt(); if(searchNum == -1){ break; } int index = find(searchNum, table, count); if(index == -1){ System.out.println("Element not found"); } else{ System.out.println("Element found at "+index); } } smallest(table, count); } public static int find (int valueToSearchFor, int intArray[ ], int numberOfValidEnties){ for(int i=0; i a[i]){ min = a[i]; index = i; } } System.out.println("The smallest element is "+min+" index is "+index);
  • 6. int temp = a[0]; a[0] = a[index]; a[index] = temp; System.out.println("After swap array elements are :"+Arrays.toString(a)); } } Output: Enter Array Element: 3 Enter Array Element: 4 Enter Array Element: 5 Enter Array Element: 2 Enter Array Element: 1 Enter Array Element: 6 Enter Array Element: 7 Enter Array Element: 8 Enter Array Element: -1 Invalid Number. Array Elements are: [3, 4, 5, 2, 1, 6, 7, 8, 0, 0] The Sum is 36 The Average is : 4.5 Number of elements less than average 4 Number of elements greater than average 4 Number of elements equal to average 0 Enter Search Element (-1 to quit): 2 Element found at 3 Enter Search Element (-1 to quit):
  • 7. 3 Element found at 0 Enter Search Element (-1 to quit): 1 Element found at 4 Enter Search Element (-1 to quit): 6 Element found at 5 Enter Search Element (-1 to quit): -1 The smallest element is 1 index is 4 After swap array elements are :[1, 4, 5, 2, 3, 6, 7, 8, 0, 0]