Question
Im having a bit of trouble with array in finding the index of min and max values and as well as getting the above and
Im having a bit of trouble with array in finding the index of min and max values and as well as getting the above and below values in the array.
- the index output I am getting are wrong because the expected output is max index value is: 11 and the min index value is: 8 but I am getting 14 for both.
my code so far:
public class Array17B
{
public static void main(String[] args)
{
int[]myArray = {45, 38, 27, 46, 81, 72, 56, 61, 20, 48, 76, 91, 57, 35, 78};
int size = myArray.length;
System.out.print("Length of the array: "+size);
System.out.println();
int max = myArray[0];
int index = 0;
for (int i = 1; i
{
if (myArray[i] > max)
max = myArray[i];
index = i;
}
int sum = 0;
for (int i = 0; i
sum+=myArray[i];
double total = 0;
double average;
for (int i = 0; i
total+=myArray[i];
average = total/myArray.length;
int min = myArray[0];
for (int i = 1; i
{
if (myArray[i]
min = myArray[i];
index = i;
}
System.out.print(" Max value of array: "+max);
System.out.print(" Index of max value of the array: "+index);
System.out.println();
System.out.print(" Sum of array: "+sum);
System.out.println();
System.out.print(" Min value of array: "+min);
System.out.print(" Index of min value of the array: "+index);
System.out.println();
System.out.print(" Average of value of the array: "+average);
}
}
the output I get:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started