Java Programming---ArraysHow can i calculate the average of the given number of arrays, i'v
Page 1 of 1
10 Replies - 983 Views - Last Post: 09 November 2009 - 06:51 PM
#1
Java Programming---Arrays
Posted 09 November 2009 - 05:12 PM
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);
}
}
Replies To: Java Programming---Arrays
#2
Re: Java Programming---Arrays
Posted 09 November 2009 - 05:29 PM
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
Re: Java Programming---Arrays
Posted 09 November 2009 - 05:47 PM
jimdandy75, on 9 Nov, 2009 - 04:29 PM, said:
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
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
Re: Java Programming---Arrays
Posted 09 November 2009 - 05:51 PM
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
Re: Java Programming---Arrays
Posted 09 November 2009 - 05:58 PM
BabyKodou, on 9 Nov, 2009 - 04:47 PM, said:
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
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:
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
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:
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
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:
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
Re: Java Programming---Arrays
Posted 09 November 2009 - 06:46 PM
BabyKodou, on 9 Nov, 2009 - 05:36 PM, said:
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
Re: Java Programming---Arrays
Posted 09 November 2009 - 06:51 PM
This post has been edited by theautokustomizer: 09 November 2009 - 06:51 PM
|
|

New Topic/Question
Reply




MultiQuote





|