Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10)
In Java
Screenshot of the code:
Sample output:
Code to copy:
//Define the class.
public class arrays
{
//Define the main function.
public static void main(String[] args)
{
//Initialize the value as per your wish.
int arr[] = {1,2,3,4,5,6,7,8,9,10};
//Call the function getMinValue and
//store the return value in the
//variable min_value.
int min_value = getMinValue(arr);
//Call the function getMaxValue and
//store the return value in the
//variable max_value.
int max_value = getMaxValue(arr);
//Call the function additional_method and
//store the return array in the
//variable final_array.
int new_array[] = additional_method(arr);
//Display the min, max , and the array.
System.out.println("The minimum value is : "+ min_value);
System.out.println("The maximum value is : "+ max_value);
System.out.println("Final values in the array "
+ "after adding 10 is : ");
//Display the new array.
for (int i = 0; i < new_array.length; i++)
{
System.out.print(new_array[i] + " ");
}
}
//Define the function to extract
//the maximum value for the array.
public static int getMaxValue(int[] array)
{
//Store the first element of the
//array in the variable x.
int x = array[0];
//Begin the for loop.
for (int i = 1; i < array.length; i++)
{
//Compare the elements of
//the array with x.
if (array[i] > x)
{
//Store the minimum value
//of the array in x.
x = array[i];
}
}
//Return the value of x.
return x;
}
//Define the function to extract
//the minimum value for the array.
public static int getMinValue(int[] array)
{
//Store the first element of the
//array in the variable x.
int x = array[0];
//Begin the for loop.
for (int i = 1; i < array.length; i++)
{
//Compare the elements of
//the array with x.
if (array[i] < x)
{
//Store the minimum value
//of the array in x.
x = array[i];
}
}
//Return the value of x.
return x;
}
//Define the function to compute the new array.
public static int[] additional_method(int num[])
{
//Begin the for loop.
for(int i=0;i<num.length;i++)
{
//Add 10 to each value of the initial
//array , and store the elements
//in the new array num.
num[i] = num[i] + 10;
}
//Return the new_array to
//the main function.
return num;
}
}
Get Answers For Free
Most questions answered within 1 hours.