March 5th, 2013, 08:21 PM

Array assignment problem
heres my current problem:
Create an array (double type) with 10 elements, and initialize it using your way. Write
code to implement an algorithm for
a. Computing the average value;
b. Finding the maximum value in this array, and also show the corresponding index
this is what i have so far not sure how to add the max value and the index to this program
public class myArray {
public static void main(String[] args) {
double[] dblArray = new double[] { 3, 7 ,9 , 11, 14, 17, 19, 23, 27, 33};
int sum = 0;
for( int i = 0; i < dblArray.length; i++){
sum = (int) (sum + dblArray[i]);
}
double average = sum / dblArray.length;
System.out.println("average: " + average);
}}
March 5th, 2013, 08:29 PM

Originally Posted by Life_Student
heres my current problem:
Create an array (double type) with 10 elements, and initialize it using your way. Write
code to implement an algorithm for
a. Computing the average value;
b. Finding the maximum value in this array, and also show the corresponding index
this is what i have so far not sure how to add the max value and the index to this program
public class myArray {
public static void main(String[] args) {
double[] dblArray = new double[] { 3, 7 ,9 , 11, 14, 17, 19, 23, 27, 33};
int sum = 0;
for( int i = 0; i < dblArray.length; i++){
sum = (int) (sum + dblArray[i]);
}
double average = sum / dblArray.length;
System.out.println("average: " + average);
}}
You Can Use
Arrays.sort(the_1D_array)
March 5th, 2013, 09:02 PM

not sure how to add that into the current code
March 5th, 2013, 09:03 PM

Originally Posted by Life_Student
not sure how to add that into the current code
What's your array you want to sort ?
March 5th, 2013, 09:05 PM

its in the code its listed as dblArray
March 5th, 2013, 09:10 PM

Originally Posted by Life_Student
its in the code its listed as dblArray
Yes, I know.
Apply Arrays.sort to that array to get the doubles in increasing order.
But here's a trick : You will need each doubles' original indexes.
What do you suggest ( Hint : Parallel smt....)
March 5th, 2013, 09:13 PM

No idea what your talking thanks though. Can anyone else explain?
March 5th, 2013, 09:15 PM

Originally Posted by Life_Student
No idea what your talking thanks though. Can anyone else explain?
Sorry, Couldn't be of Any Help.
March 5th, 2013, 09:21 PM

But Please be aware that there are many sorting ( bubble, insertion sort...) Algorithms being used by the Java Community.
I humbly advise you to examine " bubble sort " and then apply it to your code.
Also parallel arrays ( I know you have no idea what it is but you can search google for that ) are not desirable over creation of Objects !
Take it Easy, Mr. !
March 5th, 2013, 09:42 PM

ok I tried to figure out bubble sort dont understand how to apply it to this program I have an example in front of me but i dont understand it so this is what i did instead it seems to work, but how to i show the corresponding index for the max value?
public class myArray {
public static <index> void main(String[] args) {
double[] dblArray = new double[] { 3, 7 ,9 , 11, 14, 17, 19, 23, 27, 33};
double max;
int sum = 0;
for( int i = 0; i < dblArray.length; i++){
sum = (int) (sum + dblArray[i]);
}
double average = sum / dblArray.length;
max= dblArray[0];
for(int i=1; i <10; i++){
if(dblArray[i]>max) max = dblArray[i];
}
System.out.println("average: " + average);
System.out.println("Maximum: " + max);
}}
March 5th, 2013, 09:53 PM

Code:
Originally Posted by Life_Student
ok I tried to figure out bubble sort dont understand how to apply it to this program I have an example in front of me but i dont understand it so this is what i did instead it seems to work, but how to i show the corresponding index for the max value?
public class Max {
public Max() {
// TODO Autogenerated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Autogenerated method stub
double d[]={1,2,3,5,0,1,2,0.,5566,2};
System.out.print(Max(d));
}
private static double Max(double[] d) {
double max = 0;
for(int i=0;i<d.length1;i++){
if(d[i+1]>=d[i])max=d[i+1];
if(d[i+1]<d[i])max=d[i];
}
// TODO Autogenerated method stub
return max;
}
}
public class myArray {
public static <index> void main(String[] args) {
double[] dblArray = new double[] { 3, 7 ,9 , 11, 14, 17, 19, 23, 27, 33};
double max;
int sum = 0;
for( int i = 0; i < dblArray.length; i++){
sum = (int) (sum + dblArray[i]);
}
double average = sum / dblArray.length;
max= dblArray[0];
for(int i=1; i <10; i++){
if(dblArray[i]>max) max = dblArray[i];
}
System.out.println("average: " + average);
System.out.println("Maximum: " + max);
}}
Here's your lead since you want the Maximum of the array, you needn't bother with sorting.
And an if clause and a loop, will give you the index .
Best of Luck !
March 5th, 2013, 10:09 PM

bubble sort makes no sense i dont get i dont understand how it works i dont get how to put it in my code. You gave an example i dont see how it applies to my program anyways, So ill wait till my teacher goes over it cuz im getting no where with it.
This is what i got
public class myArray {
public static <index> void main(String[] args) {
double[] dblArray = new double[] { 3, 7 ,9 , 11, 14, 17, 19, 23, 27, 33};
double max;
int indexMax = 0;
int sum = 0;
for( int i = 0; i < dblArray.length; i++){
sum = (int) (sum + dblArray[i]);
}
double average = sum / dblArray.length;
max= dblArray[0];
for(int i=1; i <10; i++){
if(dblArray[i]>max) max = dblArray[i];
if( dblArray[i] < max)
max = dblArray[i];
indexMax = i;
}
System.out.println("average: " + average);
System.out.println("Maximum: " + max + " The corresponding index is: " + indexMax);
}}
March 5th, 2013, 10:12 PM

Code:
public class Max {
private static double Max;
public Max() {
// TODO Autogenerated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Autogenerated method stub
double d[]={1,2,3,5,0,1,2,0.,5566,2,9990.87};double min=0;double ave=0;double cnt;
System.out.println("\n"+Max(d));
Max=Max(d);
for(int i=0;i<d.length;i++)ave+=d[i];
System.out.println("average is "+ave/d.length);
for(int i=0;i<d.length;i++)
if(Max==d[i])
System.out.println(" the biggest "+Max +" was indexed at "+i);
}
private static double Max(double[] d) {
double max = 0;double maxx=0;
for(int i=0;i<d.length1;i++){
if(d[i+1]>=d[i])max=d[i+1];
if(d[i+1]<d[i])max=d[i];
maxx=Math.max(d[i], d[i+1]);
}
System.out.print(maxx);
// TODO Autogenerated method stub
return max;
}
}
This is Yout Remedy ( Full Solution ), so No Worries !
)
Comments on this post
March 5th, 2013, 11:20 PM

OK, Mr. Aurum84,
but please let the OP decide if my code was useful or not.
From Now On, no solutions ( full ) from me.
When I try to do Good, I do Bad, You Say.
So OK.
Be it.
Best Regards.
March 6th, 2013, 01:25 AM

Sorting
Google is your friend. If you do not understand what bubble sort is or what is the theory behind it, search for it in the google.
There are a dozen other sorting ways available. Each with its strength in terms of speed and weaknesses in terms of memory usage and so on.
In order to find the maximum value from an array, one should order (that is, sorting) the elements in a way (ascending or descending), such that the maximum value would be the starting/ending element of that array.
Code:
this is what i have so far not sure how to add the max value and the index to this program
public class myArray {
public static void main(String[] args) {
double[] dblArray = new double[] { 3, 7 ,9 , 11, 14, 17, 19, 23, 27, 33};
int sum = 0;
for( int i = 0; i < dblArray.length; i++){
sum = (int) (sum + dblArray[i]);
}
double average = sum / dblArray.length;
System.out.println("average: " + average);
}}
What I would suggest you to do is to read about sorting algorithms by following the link below:
Sorting Algorithm (Wikipedia)
Once you have read about various sorting algorithms, pick one of those and implement the algorithm into your codes, then the maximum value in the array would be the first/last element in your array after sorting, depending on the orientation of the sorting algorithm you applied (ascending or descending).
When the programming world turns decent, the real world will turn upside down.