Unable to pass an array to a second method
up vote
2
down vote
favorite
I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;
import java.util.Random;
public class ArrayTest{
public static void main(String args){
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}
public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}
Where am I going wrong, be gentle.
java arrays
add a comment |
up vote
2
down vote
favorite
I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;
import java.util.Random;
public class ArrayTest{
public static void main(String args){
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}
public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}
Where am I going wrong, be gentle.
java arrays
1
You never call theaveragefunction.
– Matt
Nov 11 at 0:39
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;
import java.util.Random;
public class ArrayTest{
public static void main(String args){
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}
public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}
Where am I going wrong, be gentle.
java arrays
I'm having difficulty with my homework assignment and I'm just not very good at writing code correctly, I can understand how it works but creating it myself is a struggle. I'm supposed to write an application that creates and array of 10 random numbers from 1 to 100, then print them out. A second method than calculates the average and a 3rd method displays all of the numbers that are below that average. I've got the first 2 parts done, but it's only displaying the 10 random ints, not calculating the average, my code is this;
import java.util.Random;
public class ArrayTest{
public static void main(String args){
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
}
public static int average(int array){
int sum =0;
for (int i = 0; i < array.length; i++){
sum += array[i];
int finalAvr = array[i] /10;
}
return sum / 10;
}
}
Where am I going wrong, be gentle.
java arrays
java arrays
edited Nov 11 at 3:02
GBlodgett
7,14541329
7,14541329
asked Nov 11 at 0:22
Josh Sanders
133
133
1
You never call theaveragefunction.
– Matt
Nov 11 at 0:39
add a comment |
1
You never call theaveragefunction.
– Matt
Nov 11 at 0:39
1
1
You never call the
average function.– Matt
Nov 11 at 0:39
You never call the
average function.– Matt
Nov 11 at 0:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
The first problem is that you never call the average() method:
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
System.out.println(average(numbers));
For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:
public static void lowerNums(int average, int arr) {
//For each int in the array
for(int num : arr) {
//If the int is less than the average, print it out
if(num < average) {
System.out.println(num);
}
}
}
Or using Streams:
Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);
add a comment |
up vote
-1
down vote
Here's how to make an array a parameter in a method:
public static void doStuff(int numbers){
//super awesome code
}
If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.
Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:
public static int getSmallValues(int average, int numbers){
Arraylist<Integer> smallNumbers = new Arraylist<>();
for(int i: numbers){
if(i < average){
smallNumbers.add(i);
}
}
return ((int) smallNumbers.toArray());
}
I hope this helps.. Merry coding!
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
The first problem is that you never call the average() method:
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
System.out.println(average(numbers));
For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:
public static void lowerNums(int average, int arr) {
//For each int in the array
for(int num : arr) {
//If the int is less than the average, print it out
if(num < average) {
System.out.println(num);
}
}
}
Or using Streams:
Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);
add a comment |
up vote
0
down vote
accepted
The first problem is that you never call the average() method:
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
System.out.println(average(numbers));
For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:
public static void lowerNums(int average, int arr) {
//For each int in the array
for(int num : arr) {
//If the int is less than the average, print it out
if(num < average) {
System.out.println(num);
}
}
}
Or using Streams:
Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
The first problem is that you never call the average() method:
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
System.out.println(average(numbers));
For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:
public static void lowerNums(int average, int arr) {
//For each int in the array
for(int num : arr) {
//If the int is less than the average, print it out
if(num < average) {
System.out.println(num);
}
}
}
Or using Streams:
Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);
The first problem is that you never call the average() method:
Random random= new Random();
int numbers= new int[10];
for (int i = 0; i < 10; i++)
{
int number= random.nextInt(100);
System.out.println(number);
numbers[i]=number;
}
System.out.println(average(numbers));
For the third method you will need to loop through the numbers array and display the numbers that are less than the average. So something like:
public static void lowerNums(int average, int arr) {
//For each int in the array
for(int num : arr) {
//If the int is less than the average, print it out
if(num < average) {
System.out.println(num);
}
}
}
Or using Streams:
Arrays.stream(arr).filter(e -> e > average).forEach(System.out::println);
answered Nov 11 at 3:01
GBlodgett
7,14541329
7,14541329
add a comment |
add a comment |
up vote
-1
down vote
Here's how to make an array a parameter in a method:
public static void doStuff(int numbers){
//super awesome code
}
If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.
Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:
public static int getSmallValues(int average, int numbers){
Arraylist<Integer> smallNumbers = new Arraylist<>();
for(int i: numbers){
if(i < average){
smallNumbers.add(i);
}
}
return ((int) smallNumbers.toArray());
}
I hope this helps.. Merry coding!
add a comment |
up vote
-1
down vote
Here's how to make an array a parameter in a method:
public static void doStuff(int numbers){
//super awesome code
}
If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.
Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:
public static int getSmallValues(int average, int numbers){
Arraylist<Integer> smallNumbers = new Arraylist<>();
for(int i: numbers){
if(i < average){
smallNumbers.add(i);
}
}
return ((int) smallNumbers.toArray());
}
I hope this helps.. Merry coding!
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Here's how to make an array a parameter in a method:
public static void doStuff(int numbers){
//super awesome code
}
If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.
Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:
public static int getSmallValues(int average, int numbers){
Arraylist<Integer> smallNumbers = new Arraylist<>();
for(int i: numbers){
if(i < average){
smallNumbers.add(i);
}
}
return ((int) smallNumbers.toArray());
}
I hope this helps.. Merry coding!
Here's how to make an array a parameter in a method:
public static void doStuff(int numbers){
//super awesome code
}
If you have an array (called digits) in another method, you can pass it directly to doStuff() by typing doStuff(digits). It's that simple.
Now, for your question.. In your third method, I think you should add two parameters: the array and the average you got earlier. Also, it's difficult to tell how many numbers are smaller than the average, I'd advise you use an arraylist instead of an array. You can convert to an array later. Look at this:
public static int getSmallValues(int average, int numbers){
Arraylist<Integer> smallNumbers = new Arraylist<>();
for(int i: numbers){
if(i < average){
smallNumbers.add(i);
}
}
return ((int) smallNumbers.toArray());
}
I hope this helps.. Merry coding!
answered Nov 11 at 0:49
Taslim
1,11741124
1,11741124
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244724%2funable-to-pass-an-array-to-a-second-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You never call the
averagefunction.– Matt
Nov 11 at 0:39