So im having trouble creating this function that has to find the integer average of a char array.
This is my array char[]letters = {‘A’, ‘B’ , ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’};
Im trying to type cast to find the integer average like A= 32 j= 74. adding the integer value and turning it back in a character but am stuck at the moment.
/********************************************************************************
This function will calculate the integer average of characters in the array
********************************************************************************/
public static void average( char [] letters )
{
int total = 0;
char mean;
total = total + letters[letters.length];
mean = (char)total / letters.length;
System.out.println("The value is " + average( letters) );
}
This is incorrect:
This operation adds the value past the end of the array to
total, triggering an exception.You need a loop here:
You can also use the
for–inloop, like this:You are also casting
totalinstead of casting the result of the division:should be replaced with