Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that will read in a list of positive integers (including zero) and display some statistics regarding the integers. The user is assumed

Write a program that will read in a list of positive integers (including zero) and display some statistics regarding the integers.

The user is assumed to enter the list in sorted order, starting with the smallest number and ending with the largest number.

Your program must store the all of the integers in an array. The maximum number of integers that you can expect is 100, however, there may not necessarily be that many. Your program should allow the user to continue entering numbers until they enter a negative number or until the array is filled - whichever occurs first. If the user enters 100 numbers, you should output an message stating that the maximum size for the list has been reached, then proceed with the calculations.

After you have placed all of the integers into an array, your program should perform the following tasks,in this order:

Display the count of how many numbers were read

Display the smallest number in the array

Display the largest number in the array

Display the median (the median is the integer that is stored in the middle position of the array)

Display the average of all the numbers

Allow the user to search the array for a specified value.

First, ask the user to enter an integer to search for.

Next, search the array to determine if the given integer is in the array.

If the integer is not found, display a message stating that the integer is not in the list.

If the integer is found, then display the position number of where you found the integer. If the integer happens to be in the array more than once, then you only need to tell the first position number where you found it.

After performing the search, ask the user if he/she wants to search for more integers. Continue searching for numbers until the user answers "N".

The program runs good, I just need it to compile and not involve any BREAK statements, so if someone could edit the code so it complies but without any break statements, that would be greatly appreciated:

My Code:

import java.util.*;

public class ListStats { public static Scanner kbd; public static final int MAXSIZE = 100; public static void main(String[] args) {

kbd = new Scanner(System.in); int[]list=new int[MAXSIZE]; int numberOfElements = getNums(list); int searchElement; System.out.println(" The count of items entered is: " + numberOfElements); System.out.println("The smallest item in the list is: " + findMin(list, numberOfElements)); System.out.println("The largest item in the list is: " + findMax(list, numberOfElements)); System.out.println("The median of the list is: " + median(list, numberOfElements)); System.out.println("The average of the list is: " + average(list, numberOfElements)); while (true) { System.out.print(" Enter an integer to search for: "); searchElement = kbd.nextInt(); int pos = search(list, numberOfElements, searchElement); if (pos != -1) { System.out.println("Item was found at position " + pos); } else { System.out.println("Item was not found."); } //Getting the character from the user 'Y' or 'y' or 'N' or 'n' System.out.print(" Do you want to search for more numbers (Y or N)? "); char ch = kbd.next(".").charAt(0); if (ch == 'Y' || ch == 'y') continue; else { //System.out.println("Goodbye"); break; } } kbd.close(); } private static int findMin(int[] list, int numberOfElements) { int min = list[0]; for (int i = 0; i < numberOfElements; i++) { if (min > list[i]) min = list[i];

} return min; } private static int findMax(int[] list, int numberOfElements) { int max = list[0]; for (int i = 0; i < numberOfElements; i++) { if (max < list[i]) max = list[i]; } return max; } public static int getNums(int[] list) { int counter = 0; int value = 0; System.out.println("Enter a list of up to " + MAXSIZE + " positive integers. Please enter the values in ascending order."); System.out.println("Enter a negative value to stop."); value = kbd.nextInt(); while (value != -1) { list[counter] = value; counter++; value = kbd.nextInt(); } if (counter == MAXSIZE) { System.out.println("The maximum size of the list has been reached.");

} return counter; } public static double median(int[] list, int size) { double medianValue = 0.0; if (size % 2 != 0) medianValue = list[size / 2]; else medianValue = (double)(list[(size - 1) / 2] + list[size / 2]) / 2.0; return medianValue; } public static double average(int[] list, int size) { double sum = 0; for (int i = 0; i < size; i++) { sum += list[i]; } return sum / size; } public static int search(int[] list, int size, int key) { int i;

for (i = 0; i < size; i++) { if (list[i] == key) return i; } return -1; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define capital structure.

Answered: 1 week ago

Question

List out some inventory management techniques.

Answered: 1 week ago