Java Programming---Arrays

How can i calculate the average of the given number of arrays, i'v

Page 1 of 1

10 Replies - 1276 Views - Last Post: 09 November 2009 - 06:51 PM Rate Topic: -----

#1 BabyKodou  Icon User is offline

  • New D.I.C Head

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

Java Programming---Arrays

Post icon  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  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 BabyKodou  Icon User is offline

  • New D.I.C Head

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

Re: Java Programming---Arrays

Posted 09 November 2009 - 05:47 PM

View Postjimdandy75, 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.
Was This Post Helpful? 0
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

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

Re: Java Programming---Arrays

Posted 09 November 2009 - 05:50 PM

Please use the code tags around your code so we may better help you....TY
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

Was This Post Helpful? 1
  • +
  • -

#5 WaHooCrazy7  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 WaHooCrazy7  Icon User is offline

  • New D.I.C Head

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

Re: Java Programming---Arrays

Posted 09 November 2009 - 05:58 PM

View PostBabyKodou, 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.
Was This Post Helpful? 0
  • +
  • -

#7 theautokustomizer  Icon User is offline

  • D.I.C Regular

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

Re: Java Programming---Arrays

Posted 09 November 2009 - 06:12 PM

View PostWaHooCrazy7, on 9 Nov, 2009 - 04:58 PM, said:

View PostBabyKodou, 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!!!! :D :^: :D
Was This Post Helpful? 1
  • +
  • -

#8 WaHooCrazy7  Icon User is offline

  • New D.I.C Head

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

Re: Java Programming---Arrays

Posted 09 November 2009 - 06:27 PM

View Posttheautokustomizer, on 9 Nov, 2009 - 05:12 PM, said:

View PostWaHooCrazy7, on 9 Nov, 2009 - 04:58 PM, said:

View PostBabyKodou, 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!!!! :D :^: :D



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


Was This Post Helpful? 0
  • +
  • -

#9 BabyKodou  Icon User is offline

  • New D.I.C Head

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

Re: Java Programming---Arrays

Posted 09 November 2009 - 06:36 PM

View PostWaHooCrazy7, on 9 Nov, 2009 - 05:27 PM, said:

View Posttheautokustomizer, on 9 Nov, 2009 - 05:12 PM, said:

View PostWaHooCrazy7, on 9 Nov, 2009 - 04:58 PM, said:

View PostBabyKodou, 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!!!! :D :^: :D



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
Was This Post Helpful? 0
  • +
  • -

#10 WaHooCrazy7  Icon User is offline

  • New D.I.C Head

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

Re: Java Programming---Arrays

Posted 09 November 2009 - 06:46 PM

View PostBabyKodou, 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.
Was This Post Helpful? 0
  • +
  • -

#11 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1