objective: To show our understanding of arrays, specifically how to declare, ini
ID: 3826199 • Letter: O
Question
objective: To show our understanding of arrays, specifically how to declare, initialize and manipulate the elements of an array. To further our understanding of functions, loops, conditional statements, data types and variables. Assignment: Write a program that uses an array to store a series of integers. The program should prompt the user to enter elements into the array. After the array has been filled, it should display the initial contents of the array. It should then perform operations to manipulate the elements of the array through specific functions. Note that if the annay is modified in any way, the program should re-display the contents of the array. Procedure: 1. Declare an array of integers of some pre-defined maximum size. You are free to decide the size of the array, but all the elements of the array should be initialized to zero. 2. Implement the following operations on the array through the specified functions: o input() This function prompts the user to populate the array with data entered from the keyboard. As this is an array of integers, the data should all be numeric integers. o display() This function outputs the contents of the array one element at a time. The array should be output using the following format: Postion Value 10 Note: position, represents the logical index of the array and value represents the numeric integer stored in the corresponding physical location. In other words position 1 corresponds to the value at arraylOExplanation / Answer
Please find the program below:
import java.util.Scanner;
class ArrayWorld{
static int N;
static int sum(int[] arr){
int sum=0;
for(int i=0;i<N;i++)
sum+=arr[i]
return sum;
}
static int max(int[] arr){
int max=arr[0];
for(int i=0;i<N;i++){
if(max<arr[i])
max=arr[i];
}
return max;
}
static int min(int[] arr){
int min=arr[0];
for(int i=0;i<N;i++){
if(min>arr[i])
min=arr[i];
}
return min;
}
static int count(int c, int[] arr){
int count=0;
for(int i=0;i<N;i++)
if(arr[i]==c) count++;
return count;
}
static int findPos(int pos, int[] arr){
int pos=0;;
for(int i=0;i<N;i++)
if(arr[i]==c) pos=i
return pos;
}
static int replace(toReplace, withReplace,int[] arr){
int count=0;
for(int i=0;i<N;i++)
if(arr[i]==toReplace) {arr[i]=withReplace; count++;}
return count;
}
public static void main (String[] args){
Scanner in =new scanner(system.in);
int [] arr=new int[100];
int i=0;
System.out.println("Welcome to the world of arrays ");
//asking size
System.out.println("How many elements you wanna enter");
N= in.nextInt();
for(i=0li<N;i++)
{
System.out.println("Enter element "+ (i+1) );
arr[i]=in.nextInt();
}
System.out.println("You have entered: Position Value");
for (i=0;i<N;i++)
{
System.out.println((i+1) + arr[i]);
}
System.out.println("The sum of all the numbers entered is "+ sum(arr));
System.out.println("The avg of all the numbers entered is "+ sum(arr)/N);
System.out.println("The largest number is "+ max(arr));
System.out.println("The smallest number is "+ min(arr));
System.out.println("What number would you like to count ");
System.out.println(count(in.nextInt());
System.out.println("What number would you like to count");
int toCount=in.nextInt();
System.out.println("The number "+toCount+" appears "+ count(toCount, arr)+" times");
System.out.println("What number would you like to search for ");
int toSearch=in.nextInt();
System.out.println("The number "+toSearch +" is at position "+ findPos(tosearch,arr));
System.out.println("What number would you like to replace");
int toReplace=in.nextInt();
System.out.println("Enter new value");
int withReplace=in.nextInt();
System.out.println("The element "+toReplace+" was replced"+ replace(toReplace, withReplace,arr)+" times");
}
}
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.