Array Basics Programming Question: Reverse Order Write a program that reads ten
ID: 3696983 • Letter: A
Question
Array Basics
Programming Question: Reverse Order
Write a program that reads ten integers into an array; define another array to save those ten numbers in the reverse order. Display the original array and new array. Then define two separate methods to compute the maximum number and minimum number of the array.
Sample Run:
Please enter 10 numbers: 1 3 5 8 2 -7 6 100 34 20 The new array is: 20 34 100 6 -7 2 8 5 3 1
The maximum is: 100
The minimum is: -7
Bonus:
Determine how many numbers are above or equal to the average and how many numbers are below the average.
Note: Submit your java code (*.java file) on blackboard only. Please refer to the sample program on our textbook to write program comments on top of your program and pay attention to your program format. Provide necessary comments in your program.
Sample comments
//********************************** COSC 1173 Programming Lab
Name: John
Data: 8/24/2015
Purpose of your program: //**********************************
-----------------------------------------------------------------------------------------------------------------------------------------------
Commonly made mistakes:
1: array index starts from 0 but not 1
2: using .length variable as often as possible instead of constant numbers refer to the size of array:
int[] list = new int[10];
e.g. for(int i = 0; i< list.length; i++) {
}
3: pay attention to outOfBoundary exceptions:
array index is out of boundary (0<= index<=list.length-1)
----------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
Hello there ,
Please find below code as per your reuirement and it's O/P .
Let me know if you have any queries.
import java.util.Scanner;
//********************************** COSC 1173 Programming Lab
/**
* Name: John
* Date: 8/24/2015
* This is an array tutorial consists of reversing an
* arrya and finiding max,min from the array.Also it provides method for finding
* elements which are above and below average.
*/
public class ArrayTutorial {
static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
int[] array = new int[10];
System.out.println("Please enter 10 numbers:");
for (int i = 0; i < 10; i++) {
array[i] = scanner.nextInt();
}
int[] newArray = reverseArray(array);
System.out.println("The new array is: ");
for (int i = 0; i < newArray.length; i++) {
System.out.println(newArray[i]);
}
System.out.println("The maximum is: " + findMaximum(newArray));
System.out.println("The minimum is: " + findMinimum(newArray));
findElementsAboveBelowAVG(array);
}
public static int[] reverseArray(int[] array) {
for (int i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
return array;
}
/**
* This method finds min element from the given array
*
* @param array
* @return
*/
public static int findMinimum(int[] array) {
if (array == null || array.length < 1)
System.out.println("The array is null of empty");
int min = 0;
// if only one element
if (array.length == 1) {
min = array[0];
}
if (array[0] > array[1]) {
min = array[1];
} else {
min = array[0];
}
for (int i = 2; i <= array.length - 1; i++) {
if (min > array[i]) {
min = array[i];
}
}
return min;
}
/**
* This method finds max element from the given array.
*
* @param array
* @return
*/
public static int findMaximum(int[] array) {
if (array == null || array.length < 1)
System.out.println("The array is null of empty");
int max = 0;
// if only one element
if (array.length == 1) {
max = array[0];
}
if (array[0] > array[1]) {
max = array[0];
} else {
max = array[1];
}
for (int i = 2; i <= array.length - 1; i++) {
if (max < array[i]) {
max = array[i];
}
}
return max;
}
/**
* This method finds elements above and below average.
*
* @param a
* @return
*/
public static void findElementsAboveBelowAVG(int[] array) {
int[] aboveAVG = new int[array.length];
int[] belowAVG = new int[array.length];
int index1 = 0, index2 = 0;
if (array == null || array.length < 1)
System.out.println("The array is null of empty");
double avg = 0;
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum = sum + array[i];
}
avg = sum / array.length;
System.out.println("Average of the array is :" + avg);
for (int i = 0; i < array.length; i++) {
if (array[i] < avg) {
belowAVG[index1] = array[i];
index1++;
} else {
aboveAVG[index2] = array[i];
index2++;
}
}
System.out.println(index1 + " elements below average are: ");
for (int i = 0; i < index1; i++) {
System.out.println(belowAVG[i]);
}
System.out.println(index2 + " elements above average are: ");
for (int i = 0; i < index2; i++) {
System.out.println(aboveAVG[i]);
}
}
}
====O/P===
Please enter 10 numbers:
1
3
5
8
2
-7
6
100
34
20
The new array is:
20
34
100
6
-7
2
8
5
3
1
The maximum is: 100
The minimum is: -7
Average of the array is :17.0
7 elements below average are:
6
-7
2
8
5
3
1
3 elements above average are:
20
34
100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.