Range in Array Java -
i need complete task. not entirely sure how range array. think supposed use loop somehow not work.
this have far:
import java.util.*; public class a2_1 { static scanner x = new scanner(system.in); public static void main(string[] args) { int [] myarray = new int [1000000]; int x; ( x = 0; x <= 100; x++) { myarray [x] = x+1; } system.out.println(myarray); } }
this task:
"create program generates 1,000,000 integer random values in range of [1,..,100] , given x (between 1 , 100) taken user input computes "(๐๐๐ก๐๐ ๐๐ข๐๐๐๐ ๐๐ ๐๐๐๐๐๐๐ก๐ ๐๐๐ ๐ ๐กโ๐๐ ๐๐ ๐๐๐ข๐๐ ๐ก๐ ๐ฅ)/1,000,000". value must comparable cdf of uniform distribution u[1,100] @ point x."
im not going give answer since sounds homework break down manageable chunks.
first, generate 1000000 random numbers between 1 , 100. on right track , combine dawnkeepers hint. int[] randoms = new int[1000000];
int array size of 1000000. iterate on each index , assign random number(see above). have array million random numbers. generating million randoms can lengthy procedure depending on machine, yea.
next, use scanner class users input.(pro tip: dont use same variable name scanner variable in loop lol). if check make sure enter number between 1 , 100. if(val > 0 && val <= 100)
. if passes, move on, else quit or prompt user give new input. using scanner pretty trivial wont go this.
finally, iterate through list of randoms , keep counter of how many numbers less or equal x
.
int counter = 0; for(int = 0; < randoms.length; i++) { //if randoms[i] less or equal x, counter++ }
take counter , math, int final_answer = counter/randoms.length;
that's it. there more efficient ways of doing tried make simple follow. if misread question, sorry.
Comments
Post a Comment