Page 1 of 2 12 Last
1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
43
Rep Power
5

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);

}}
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
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)
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
43
Rep Power
5
not sure how to add that into the current code
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
Originally Posted by Life_Student
not sure how to add that into the current code
What's your array you want to sort ?
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
43
Rep Power
5
its in the code its listed as dblArray
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
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....)
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
43
Rep Power
5
No idea what your talking thanks though. Can anyone else explain?
8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
Originally Posted by Life_Student
No idea what your talking thanks though. Can anyone else explain?
Sorry, Couldn't be of Any Help.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
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. !
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
43
Rep Power
5
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);
}}
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
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 Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated 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.length-1;i++){ if(d[i+1]>=d[i])max=d[i+1]; if(d[i+1]<d[i])max=d[i]; } // TODO Auto-generated 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 !
12. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
43
Rep Power
5
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);

}}
13. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
Code:
public class Max {

private static double Max;

public Max() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated 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.length-1;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 Auto-generated method stub
return max;

}

}

This is Yout Remedy ( Full Solution ), so No Worries !

)

• Aurum84 disagrees : Have you even run this code? Very wrong Solution, it dienst do what you claim. Stop providing these "full answers".
14. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
47
Rep Power
0
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.
15. 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).
Page 1 of 2 12 Last