Question

In: Computer Science

Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:

IN JAVA

Array Operations

Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:

  • getTotal: This method should accept a one-dimensional array as its argument and return the total of the values in the array.
  • getAverage: This method should accept a one-dimensional array as its argument and return the average of the values in the array.
  • getHighest: This method should accept a one-dimensional array as its argument and return the highest value in the array.
  • getLowest: This method should accept a one-dimensional array as its argument and return the lowest value in the array.

Demonstrate each of the methods in the program.

Solutions

Expert Solution

Program in Java -

public class arrayOperations{
  
static int getTotal(int[] a){
int sum=0;
for(int i=0;i<a.length;i++)
sum = sum+a[i];
return sum;
}
  
static float getAverage(int[] a){
int sum=0;
for(int i=0;i<a.length;i++)
sum = sum+a[i];
float average=sum/a.length;
return average;
}
  
static int getHighest(int[] a){
int max=a[0];
for(int i=0;i<a.length;i++)
if(a[i]>max)
max=a[i];
return max;
}
  
static int getLowest(int[] a){
int min=a[0];
for(int i=0;i<a.length;i++)
if(a[i]<min)
min=a[i];
return min;
}
  

public static void main(String []args)
{
int a[]=new int[]{55,66,11,33,99,2,4,6,88,77};
int Sum = getTotal(a);   
float Average = getAverage(a);
int Highest=getHighest(a);
int Lowest=getLowest(a);
System.out.println("Sum of array values="+Sum);
System.out.println("Average of array values="+Average);
System.out.println("Highest value in array="+Highest);
System.out.println("Lowest value in array="+Lowest);
  
}
}

The output of the Program -

 


Related Solutions

Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: -getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. -getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. -getRowTotal. This method should accept a two-dimensional array as...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: fillRandom. Accepts a reference to a two-dimensional array and fills it with random integers from 0 to 99 formatPrint. This method should accept a two-dimensional array and print it out row by row getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in...
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array. ▪ getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array. ▪ getRowTotal ....
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Write a program to produce an array of integer random numbers. Your program should find out...
Write a program to produce an array of integer random numbers. Your program should find out from the user how many numbers to store. It should then generate and store that many random integers (the random numbers must be between 1 and 999 inclusive). The program should then determine the smallest number, the largest number, and the average of all the numbers stored in the array. Finally, it should print out all the numbers on the screen, five numbers to...
Write the simplest program possible in your language of choice containing your 'main' and any other...
Write the simplest program possible in your language of choice containing your 'main' and any other functions you may need that will: A. Read a sequence of words up to a maximum of 64 words from std input. Before reading the input, prompt the user to enter the words. B. Check to see if there any duplicate words in the input read. C. If there are no duplicates, print out to std output the message 'No Duplicates'. D. If there...
Write a program using multiple functions. Make use of an array to store data Make use...
Write a program using multiple functions. Make use of an array to store data Make use of searching techniques Read data from a file Write data to a file Instructions: In this lab, you will be examining a set of stock collected over a twenty four day period. Be sure to make use of an array to store these stocks. You will be required to read in the data points from a file. Write a function to read in the...
Get data on government debt of any country of your choice and explain its fluctuations. Also,...
Get data on government debt of any country of your choice and explain its fluctuations. Also, explain who bears the burden of government debt and why. Under what circumstances is there no burden to be borne of government debt? Country: United States
Get data on government debt of any country of your choice and explain its fluctuations. Also,...
Get data on government debt of any country of your choice and explain its fluctuations. Also, explain who bears the burden of government debt and why. Under what circumstances is there no burden to be borne of government debt? Country: United States
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT