Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed
----jGRASP exec: java Array17B Length of the array: 15 Max value of array: 91 Index of max value of the array: 14 Sum of array: 831 Min value of array: 20 Index of min value of the array: 14 Average of value of the array: 55.4 ----jGRASP: operation complete. L

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions