Click to See Complete Forum and Search --> : some help please


keko4201
01-27-2005, 08:42 PM
hey guys hows it goin.ok im workingo n my first project, now im not looking for u guys to do my code, cause i did it already, theres just a couple things im not understanding on how to get.

ok i have to make a program that rolls a die 1000 times, and stores the result in an array. this is wha he wants the out put to look like.
/////////////////////////////////////////////////////////////////////


Most frequent value: 4 (186 rolls)
Least frequent value: 2 (143 rolls)
Frequencies:
1: 184 rolls, 18.4%
2: 143 rolls, 14.3%
3: 154 rolls, 15.4%
4: 186 rolls, 18.6%
5: 181 rolls, 18.1%
6: 152 rolls, 15.2%

/////////////////////////////////////////////////////////////////////

ok, so thats how he wnats it, but i cant get it like that cause i cant figure out how to get the hiest frequenci numbers. i can get the amount, but not the specific number. heres wha i mean, this is my output.

/////////////////////////////////////////////////////////////////////

Most Frequent Value 177(see, i can get 177, but not the numer it was)
least Frequent Value 155 (same thing for this)
1 = 166
2 = 155
3 = 161
4 = 177
5 = 167
6 = 174

HOW DO I GET THE NUMBER DIE, THAT WAS ROLLED THE MOST? HELP
////////////////////////////////////////////////////////////////////


ok, now here is my code, i dont know if its any good, so maybe u guys can look at it and make suggestions, its not a lot, just a couple of loops.

/////////////////////////////////////////////////////////////////////

/*
* project.java
*
* Created on January 27, 2005, 7:19 PM
*/

/**
*
* @author Me
*/
public class project {

/** Creates a new instance of project */
public project() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int[] nums = new int[1000];
int[] sides = {1,2,3,4,5,6};
int[] count = new int[6];

for(int j = 0; j<nums.length; j++){

int rolls = (int)(6 * Math.random() + 1);

nums[j] = rolls;



}
int i = 0;
for(int j = 0; j<nums.length; j++){

if(nums[j]==sides[i]){

count[0]++;

}
if(nums[j]==sides[1]){

count[1]++;
}

if(nums[j]==sides[2]){

count[2]++;
}

if(nums[j]==sides[3]){

count[3]++;
}

if(nums[j]==sides[4]){

count[4]++;
}

if(nums[j]==sides[5]){

count[5]++;
}


}

int large = count[0];
int small = count[0];

for(int j = 1; j<count.length; j++){

if(count[j]<small){

small = count[j];
}

if(count[j]>large){

large = count[j];
}

}

System.out.println("Most Frequent Value " + (large));
System.out.println("least Frequent Value " + (small));

int c = 1;

for(int j = 0; j < count.length; j++){

System.out.println(c + " = " + count[j]);
c++;
}









}

}

ray326
01-27-2005, 11:11 PM
Instead of an array to store the results, consider using a HashMap keyed with the number of the face of the die. Then it looks like:

Loop 1000 times
Roll the die
Add 1 to the current roll-keyed value in the hash
End Loop
Find the key with the largest value in the hash

By the way, it should be Project, not project.

keko4201
01-28-2005, 02:34 AM
i dont understand wha u mean, can u elaberate?

-thnks for the reply too-

ray326
01-28-2005, 11:07 PM
Let's just leave it like you've got. I ran this in a scrapbook page in Eclipse.

int[] count = new int[7];
int[] nums = new int[1000];

// roll the die
for (int i = 0; i < nums.length; i++)
nums[i] = (int)(Math.random()*6 + 1);
// clear the buckets
for (int i = 0; i < count.length; i++)
count[i]=0;
// tally into the buckets
for (int j = 0; j < nums.length; j++)
count[nums[j]]++;
// show the buckets
for (int i = 1; i < count.length; i++)
System.out.println(""+ i +" = "+count[i]);

keko4201
01-29-2005, 09:29 PM
ok,i kinda understand wha u did. i have a question about it though. first, you dont need brackets around the different loops? also
i dont understand how this loop works

int[] count = new int[7];
int[] nums = new int[1000];

for (int j = 0; j < nums.length; j++)
count[nums[j]]++;

how does this work?i dont understand how its storing its matching all the like numbers abd storin g it in the count.

also, i dont mean to be a pain, how do i get the most frequent rolls, i need the highest frequent roll, and the lowest to display. i can get the highest and lowest with this method

int large = count[0];
int small = count[0];

for(int j = 1; j<count.length; j++){

if(count[j]<small){

small = count[j];
}

if(count[j]>large){

large = count[j];
}


but i cant get the number which it represents. for example, if it rolls the number 2, 160 times, i can display the 160, but not the 2. im not sure how to display the 2, is it with a for loop, and an if statement?

also, thnks for your help, i appreciate it. sorry for being a pain.

ray326
01-29-2005, 11:44 PM
how does this work?i dont understand how its storing its matching all the like numbers abd storin g it in the count. Each member of nums holds the "face" (1-6) that was rolled. That "face" is used to index the count[]. In effect the "face" becomes a "key" to the count[]. Change the j to face to make it more clear maybe.

int largeCount = 0;
int largeFace = 0;
int smallCount = nums.length;
int smallFace = 0;

for (int face = 1; face<count.length; face++)
{
if (count[face] < smallCount)
{
smallCount = count[face];
smallFace = face;
}

if (count[face] > largeCount)
{
largeCount = count[face];
largeFace = face;
}
}

keko4201
01-30-2005, 02:03 AM
Well i have to say, i understand it now, its much more clear when its explained. i couldnt see how it was being done before. but now i see how it works, and its very efficient. thanks a lot i really appreciate it.im not gonna use what you gave me though ray, its not right.

-keko-

ray326
01-30-2005, 01:12 PM
thanks a lot i really appreciate it.im not gonna use what you gave me though ray, its not right.
You're welcome. I appreciate your ethics, too.