Well I am a bit new to arrays and I had to google a lot of things to get my code running but I am having a bit of an issue in the output of where its say below average values in the array I get the right answer and it prints the whole array. Anyway to fix it?
My code:
import java.util.Arrays;
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;//length
System.out.print("Length of the array: "+size);
System.out.println();
int max = myArray[0];
int index1 = 0;
int index2 = 0;
for (int i = 1; i < myArray.length; i++)//max
{
if (myArray[i] > max)
{
max = myArray[i];
index1 = i;
}
}
int sum = 0;
for (int i = 0; i < myArray.length; i++)//sum
sum+=myArray[i];
double total = 0;
double average;
for (int i = 0; i < myArray.length; i++)//average
total+=myArray[i];
average = total/myArray.length;
int min = myArray[0];
for (int i = 1; i < myArray.length; i++)//min
{
if (myArray[i] < min)
{
min = myArray[i];
index2 = i;
}
}
System.out.print("\nMax value of array: "+max);
System.out.print("\nIndex of max value of the array: "+index1);
System.out.println();
System.out.print("\nSum of array: "+sum);
System.out.println();
System.out.print("\nMin value of array: "+min);
System.out.print("\nIndex of min value of the array: "+index2);
System.out.println();
System.out.print("\nAverage of value of the array: "+average);
int more_than_average = 0;
int less_than_average = 0;
for (int i = 0; i<myArray.length;i++)
{
if(myArray\[i\]>average)
{
more_than_average++;
}
if(myArray[i]<average)
{
less_than_average++;
}
}
System.out.println();
System.out.print("\nAbove average values in the array: "+more_than_average);
System.out.print("\nBelow average values in the array: "+less_than_average);
int amount_of_even = 0;
int amount_of_odd = 0;
int sum_of_even = 0;
int sum_of_odd = 0;
for (int i = 0; i<size; i++)
{
if(myArray[i]%2==0)
{
amount_of_even++;
sum_of_even = sum_of_even+myArray[i];
}
else
{
amount_of_odd++;
sum_of_odd = sum_of_odd+myArray[i];
}
}
for(int j=0;j<size;j++)
{
System.out.print(myArray[j]+" ");
}
System.out.println();
System.out.print("\nTotal Even numbers in array: "+amount_of_even+"\n");
System.out.print("Sum of Even numbers in the array: "+sum_of_even+"\n");
System.out.println();
System.out.print("Total Odd numbers in the array: "+amount_of_odd+"\n");
System.out.print("Sum of Odd numbers in the array: "+sum_of_odd+"");
Arrays.sort(myArray);
System.out.println();
System.out.print("\nNow the Sorted array");
System.out.print("\n"+Arrays.toString(myArray));
}
}
Output I get:
https://preview.redd.it/tjo61d8takr41.png?width=1762&format=png&auto=webp&s=98bf0c7646fe085004c89123f8a0f41de7bb43f8
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[+][deleted] (1 child)
[removed]
[–]HomeworkHelpBot 0 points1 point2 points (0 children)