import java.util.*;
class ArrayOperations
{
static void displayMenu()
{
System.out.println("1. Modify the
elements of the array.");
System.out.println("2. Print the
items in the array.");
System.out.println("3. Compute and
print the total of the array items.");
System.out.println("4. Compute and
print the average value in the array.");
System.out.println("5. Compute and
print the smallest value in the array.");
System.out.println("6. Count and
display the number of items in the array that are
negative.");
System.out.println("7. Sort the
array.");
System.out.println("8.
Quit");
}
static int selectChoice()
{
Scanner sc = new
Scanner(System.in);
System.out.print("Enter your
choice: ");
int choice = sc.nextInt();
while(choice < 1 || choice >
8)
{
System.out.println("Choice should be in the range 1 - 8.");
System.out.print("Enter your choice: ");
choice =
sc.nextInt();
}
return choice;
}
static void randomNumber(int[] array)
{
Random rand = new Random();
for(int i = 0; i < array.length;
i++)
array[i] =
rand.nextInt();
}
static void printValues(int[] array)
{
for(int i = 0; i < array.length;
i++)
System.out.print(array[i] + " ");
System.out.println();
}
static int totalOfElements(int[] array)
{
int sum = 0;
for(int i = 0; i < array.length;
i++)
sum +=
array[i];
return sum;
}
static double averageOfElements(int[]
array)
{
return
(double)totalOfElements(array) / array.length;
}
static int smallestElement(int[] array)
{
int small = array[0];
for(int i = 0; i < array.length;
i++)
if(array[i] <
small)
small = array[i];
return small;
}
static int countNegatives(int[] array)
{
int count = 0;
for(int i = 0; i < array.length;
i++)
if(array[i] <
0)
count++;
return count;
}
static void bubbleSort(int[] array)
{
for(int i = 0; i < array.length
- 1; i++)
for(int j = 0; j
< array.length - i - 1; j++)
if(array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
public static void main(String[] args)
{
int choice;
Scanner sc = new
Scanner(System.in);
System.out.print("Enter the number
of elements to generate in the array: ");
int numOfElements =
sc.nextInt();
int[] array = new
int[numOfElements];
randomNumber(array);
while(true)
{
displayMenu();
choice =
selectChoice();
switch(choice)
{
case
1: randomNumber(array); break;
case
2: printValues(array); break;
case
3: System.out.println("Total: " +
totalOfElements(array)); break;
case 4:
System.out.println("Average: " + averageOfElements(array));
break;
case 5:
System.out.println("Smallest: " + smallestElement(array));
break;
case
6: System.out.println("Negatives count: " +
countNegatives(array)); break;
case
7: bubbleSort(array);
case
8: return;
default:
System.out.println("Invalid selection.");
}
}
}
}
Using Java In this assignment we are working with arrays. You have to ask the user...
in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed. Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed. Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Netflix into the array. Call a function called getAverage to calculate and display the average of the hours entered. Call a function called...
JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 8 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements...
Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...
Using Matlab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Provide the user the following menu from which he can select: 1. Sum of all elements in the array. Print the result. 2. Sum of the element in a row that he’ll specify. Print the result. 3. Sum of the...
this assignment need to be done in java only Requirements: Ask the user to enter the size of an array (int value) Allocate a 2D array of int that size (if the user enters in 5, then allocate a 5x5 array) Using a Random, initialize each element of the array to be either 0 or 1 Output the array in a table format (see below for an example) Output the fraction of your array that is ones and the fraction...
Q1. Write a program in Java a. Create 2 separate arrays, of user defined values, of same size b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....
"you will write a program in C++ which will read a list of up to 100 integers from the user, and print them back to the screen in sorted order. The user can indicate that they are done entering numbers by entering any negative value. Your program will store the entered numbers into an array. It will then sort the array, using the selection sort algorithm. After each iteration of the sorting algorithm, it will print the current state of...
Playing With Arrays The following problems are to give you practice with programming with arrays. Write a program to: 1.Ask the user for numbers and place them in an array sequencially. 2.Next, print out the values in the array in the order they are stored. 3.Search the array for the postion containing the smallest value. 4.Print out the position where the smallest value was found. 5.Swap the smallest value with the value in the first postition of the array. 6.Print...
please help fill out the class menu create a prototype that will display information about housing data1. In this prototype you will use dynamic array data structures to store the data and compute metrics. For the requirements of the prototype, your client has given you a list of metrics and operations. The user interface will be a menu that displays this list and prompts the user to choose which option to execute.You will create two classes. First you will create...