# Java Programming---Arrays

Page 1 of 1

## 10 Replies - 1873 Views - Last Post: 09 November 2009 - 06:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=137892&amp;s=760103abd190e3fe91e4a8855eec9229&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BabyKodou

Reputation: 0
• Posts: 12
• Joined: 02-November 09

# Java Programming---Arrays

Posted 09 November 2009 - 05:12 PM

How can i calculate the average of the given number of arrays, i've already calculated the sum but don't know how to get the average.

THIS IS THE CODE

import java.util.Random;
import java.io.*;
public class Average{
public static void main(String[]args){
int MyArray[]=new int[20];
int Largest=MyArray[0];
int sum=0;
Random Me=new Random();

for(int x=0;x<20;x++){
MyArray[x]=Me.nextInt(100);
System.out.print(MyArray[x]);
System.out.print(" ");
for(int j=0;j<20;j++){
if(Largest<MyArray[j]){
Largest=MyArray[j];
}
}
for(int m=0;m<20;m++){
sum+=MyArray[m];

}
}
System.out.println(": Largest number is: "+ Largest);
System.out.println("The sum of all the numbers is : " + sum);

}
}

Is This A Good Question/Topic? 0

## Replies To: Java Programming---Arrays

### #2 jimdandy75

• D.I.C Regular

Reputation: 37
• Posts: 311
• Joined: 30-June 08

## Re: Java Programming---Arrays

Posted 09 November 2009 - 05:29 PM

Well, it looks like you have 20 elements in your array. Wouldn't you just take the sum divided by 20 to get the average?

```System.out.println("The average of all the numbers is : " + sum/20);
```

This post has been edited by jimdandy75: 09 November 2009 - 05:32 PM

### #3 BabyKodou

Reputation: 0
• Posts: 12
• Joined: 02-November 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 05:47 PM

jimdandy75, on 9 Nov, 2009 - 04:29 PM, said:

Well, it looks like you have 20 elements in your array. Wouldn't you just take the sum divided by 20 to get the average?

```System.out.println("The average of all the numbers is : " + sum/20);
```

I've done that thank you, it worked!.
So can you help me show me how to search for a number in an array set of numbers. For example if a user wants to search if or if not a number is in the set of arrays.

### #4 theautokustomizer

• D.I.C Regular

Reputation: 16
• Posts: 250
• Joined: 20-September 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 05:50 PM

And as per your other problem, I would just use an
```if (MyArray[x] == userinput)
System.out.println("The number is in this array");

```

Obviously you would have to ask the question first and declare your input, but that is general idea of it....

This post has been edited by theautokustomizer: 09 November 2009 - 05:56 PM

### #5 WaHooCrazy7

Reputation: 3
• Posts: 19
• Joined: 09-November 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 05:51 PM

There is also another option(s) that would work, even if you changed the size of the array. Java has a length method for figuring out what the size of an array is. So you could do something like this:
```String[] foo;
foo = new String[20];
//While loop to populate foo
println "The average of Foo is: " +sum/foo.length + "\n";

```

That method will find the average size of any array that you give it to. One important note though is if you allocate 20 cells, but only fill 10 the length method is still going to return 20. (Should not be a problem in your case)

This post has been edited by WaHooCrazy7: 09 November 2009 - 06:02 PM

### #6 WaHooCrazy7

Reputation: 3
• Posts: 19
• Joined: 09-November 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 05:58 PM

BabyKodou, on 9 Nov, 2009 - 04:47 PM, said:

I've done that thank you, it worked!.
So can you help me show me how to search for a number in an array set of numbers. For example if a user wants to search if or if not a number is in the set of arrays.

That task can be accomplished by a function. If you simply want to know if a number is there or not, it can return a bool. So for example:
```//Assumes an array of Foo is already in existance
bool findNum(int x){
for (int i = 0; i < Foo.length; i++) {
if (Foo[i] == x){
return true; //Will break you out of the for loop
} else {
return false;
}
}
}

```

That will go through Foo and return True if it finds the value that you passed it. This can be modified to return the index of where it is in the array if that's what you need.

### #7 theautokustomizer

• D.I.C Regular

Reputation: 16
• Posts: 250
• Joined: 20-September 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 06:12 PM

WaHooCrazy7, on 9 Nov, 2009 - 04:58 PM, said:

BabyKodou, on 9 Nov, 2009 - 04:47 PM, said:

I've done that thank you, it worked!.
So can you help me show me how to search for a number in an array set of numbers. For example if a user wants to search if or if not a number is in the set of arrays.

That task can be accomplished by a function. If you simply want to know if a number is there or not, it can return a bool. So for example:
```//Assumes an array of Foo is already in existance
bool findNum(int x){
for (int i = 0; i < Foo.length; i++) {
if (Foo[i] == x){
return true; //Will break you out of the for loop
} else {
return false;
}
}
}

```

That will go through Foo and return True if it finds the value that you passed it. This can be modified to return the index of where it is in the array if that's what you need.

I LIKE IT!!!!

### #8 WaHooCrazy7

Reputation: 3
• Posts: 19
• Joined: 09-November 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 06:27 PM

theautokustomizer, on 9 Nov, 2009 - 05:12 PM, said:

WaHooCrazy7, on 9 Nov, 2009 - 04:58 PM, said:

BabyKodou, on 9 Nov, 2009 - 04:47 PM, said:

I've done that thank you, it worked!.
So can you help me show me how to search for a number in an array set of numbers. For example if a user wants to search if or if not a number is in the set of arrays.

That task can be accomplished by a function. If you simply want to know if a number is there or not, it can return a bool. So for example:
```//Assumes an array of Foo is already in existance
bool findNum(int x){
for (int i = 0; i < Foo.length; i++) {
if (Foo[i] == x){
return true; //Will break you out of the for loop
} else {
return false;
}
}
}

```

That will go through Foo and return True if it finds the value that you passed it. This can be modified to return the index of where it is in the array if that's what you need.

I LIKE IT!!!!

Actually I just realized my code is wrong...man I've been fried lately. Problem with it is that it will only check the first value in the array and if its not equal to x just return false. Basically I should have moved the else outside of the for loop. Here is the correct code:
```//Assumes an array of Foo is already in existance
bool findNum(int x){
for (int i = 0; i < Foo.length; i++) {
if (Foo[i] == x){
return true; //Will break you out of the for loop
}
}
//You wont get here if there is any value that would cause the if(Foo[i] == x) evaluate true
return false;
}

```

### #9 BabyKodou

Reputation: 0
• Posts: 12
• Joined: 02-November 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 06:36 PM

WaHooCrazy7, on 9 Nov, 2009 - 05:27 PM, said:

theautokustomizer, on 9 Nov, 2009 - 05:12 PM, said:

WaHooCrazy7, on 9 Nov, 2009 - 04:58 PM, said:

BabyKodou, on 9 Nov, 2009 - 04:47 PM, said:

I've done that thank you, it worked!.
So can you help me show me how to search for a number in an array set of numbers. For example if a user wants to search if or if not a number is in the set of arrays.

That task can be accomplished by a function. If you simply want to know if a number is there or not, it can return a bool. So for example:
```//Assumes an array of Foo is already in existance
bool findNum(int x){
for (int i = 0; i < Foo.length; i++) {
if (Foo[i] == x){
return true; //Will break you out of the for loop
} else {
return false;
}
}
}

```

That will go through Foo and return True if it finds the value that you passed it. This can be modified to return the index of where it is in the array if that's what you need.

I LIKE IT!!!!

Actually I just realized my code is wrong...man I've been fried lately. Problem with it is that it will only check the first value in the array and if its not equal to x just return false. Basically I should have moved the else outside of the for loop. Here is the correct code:
```//Assumes an array of Foo is already in existance
bool findNum(int x){
for (int i = 0; i < Foo.length; i++) {
if (Foo[i] == x){
return true; //Will break you out of the for loop
}
}
//You wont get here if there is any value that would cause the if(Foo[i] == x) evaluate true
return false;
}

```

What about the BinarySearch method using Java.util.Arrays class

### #10 WaHooCrazy7

Reputation: 3
• Posts: 19
• Joined: 09-November 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 06:46 PM

BabyKodou, on 9 Nov, 2009 - 05:36 PM, said:

What about the BinarySearch method using Java.util.Arrays class

Hmmm that would work, I just looked at the Javadoc's and I was not aware that the method was there. I'm currently taking a data structures class in C++ so I guess I'm just used to my professor making me write out this stuff. The binary search method is easy to use, just pass it the name of your array (e.g. Foo) and whatever value your looking for (e.g. 5). so something like....
```int location = binarySearch(Foo, 5);

```

Then if you want true / false just say if location is > 0 then the value exists in the array, or if the value is negative then the value is not in the array.

### #11 theautokustomizer

• D.I.C Regular

Reputation: 16
• Posts: 250
• Joined: 20-September 09

## Re: Java Programming---Arrays

Posted 09 November 2009 - 06:51 PM

We just got done with arrays, you are correct with the binarysearch, and I feel like an idiot for not thinking of it, lol.

This post has been edited by theautokustomizer: 09 November 2009 - 06:51 PM