Hi everybody, I really tried to do my assignment but I am lost . Any helping is very helpful , Thanks
Write a program that will generate a set of 50 random integers in the range -20 to + 20. The program
should display the following:
a) The number of positive and negative numbers (treat 0 as positive).
b) The average value of the positive numbers
c) The average value of the negative numbers
d) The largest and smallest number.
e) The average of all the numbers.
You must use a for loop structure for this question along with the Math.random() method to generate
the random numbers.
Hint: Generate numbers in the range of 0-40 and subtract 20
My try is :
public class Assign4A {
public static void main(String[] args){
// a
for (int i = 0; i < 50; i++) {
int random = (int)(Math.random() * (40) + (-20) );
System.out.print(random + " " );
}
//b
for ( int i = 0; i < 50 ; i++) {
int random = (int)(Math.random() * (49) + 1 );
System.out.print( random + " " + "\n" );
}
int n = 50;
int sum = runSimulation(n);
System.out.print(sum );
}
public static int runSimulation (int n){
int sum = 0;
for (int i = 1; i <= n; i++){
int sim = (int)(Math.random() * (49) + 1 );
sum += sim;
}
return sum;
}
}