# need help with java how to return the value to an array



## hannguoi (Mar 25, 2009)

I'm sorry to be a noob but i can seem to figure this out can someone please be kind to help me: here are a piece of code that i need to do for my project but cant seem to figure out how to make it right.

int counts[] = new int [maxHeads + 1]; 
// initialize all of the values in the array to 0
for(int index = MIN_HEADS; index <= maxHeads; index++)
counts[index] = 0;
// flip the coin for n sets with nFlips flips per set
// count the number of heads in each set (heads = 1, tails = 0)

for(int i = 0; i < nSets; i++)
{
int sum = 0;
for( int j = 0; j < nFlips; j++)
{
if(myCoin.flip() == true)
sum = sum + 1;
else
sum = sum + 0;
}
here is where i wanted to return the sum to the array[index], but can seem to figure it out how to code it can some1 please help pointing out what i should do?
}


----------



## JimmySeal (Sep 25, 2007)

Please use [code] tags when you post code here.

It looks like you need an array with length _nSets_, but I don't see one. I also don't understand the purpose of creating an array with length _maxHeads + 1_ or why you are setting certain parts of the array (but not all of it) to 0.


----------



## MRdNk (Apr 7, 2007)

Here, check out the w3schools, Javascript: Arrays.

http://www.w3schools.com/JS/js_obj_array.asp


----------



## hannguoi (Mar 25, 2009)

here is the problem i got please read through my php code because i add comment where i need help here are the programs please help as my project is aproaching deadline soon.
This program will generate the head or tail of the coin

```
public class Coin
{
   private boolean faceValue;  // current value showing on the coin

   //-----------------------------------------------------------------
   //  Constructor: Sets the initial face value.
   //-----------------------------------------------------------------
   public Coin()
   {
      faceValue = false;  // arbitrarily tails by default
   }

   //-----------------------------------------------------------------
   //  Rolls the die and returns the result.
   //-----------------------------------------------------------------
   public boolean flip()
   {
     faceValue = Math.random() >= 0.5? true : false;
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Face value mutator.
   //-----------------------------------------------------------------
   public void setFaceValue (boolean value)
   {
      faceValue = value;
   }

   //-----------------------------------------------------------------
   //  Face value accessor.
   //-----------------------------------------------------------------
   public boolean getFaceValue()
   {
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Returns a string representation of this die.
   //-----------------------------------------------------------------
   public String toString()
   {
     return faceValue? "Heads" : "Tails";
   }
}
```
And this is the program i'm working on. Step three is the most important thing because i need it to finish my project.
What i want to do with this is let say: i got an arrays of counts[maxHeads +1] because if i get 6 coins to flip there are 7 out come that can come out with that flip. that is Zero head or all tails, 1 head, 2 heads, 3 heads, 4 heads, 5 heads, 6 heads or all heads. that make it counts[]=counts[maxHeads+1] = 7 values
Now!! 
here is where i get a problem.
let say if i wanted to flip 6 [email protected],please read my problem 3 javascript below.
i will use two for loop for it
and at the completion of the inner loop i will get a sum number whose is = to the number of heads in that try with 6 coin. let said i'll get a 3 for the sum. that is 3 heads in 6 tried. What i want to do is to code that if(sum == to the what?) so thati can increment the value of counts[index]

```
import java.util.Scanner;

public class Histogram {

  private static final int MIN_HEADS = 0;   // minimum number of heads per set

  /**
   * main method:
   * ask the user how many sets of coin flips and how many flips per set
   * count occurances of heads in user specified number of sets of coin flips
   * draw a horizontal and vertical histogram of the number of heads
   */
  public static void main(String[] args) {

    // set up to get user input
    Scanner scan = new Scanner(System.in);

    // ask user for the number of sets of coin flips
    System.out.println("How many sets of coin flips do you want?");
    int nSets = scan.nextInt();

    // ask user for the number of coin flips per set 
    System.out.println("How many coin flips per set do you want?");
    int nFlips = scan.nextInt();

    // maximum number of heads that can occur is number of flips
    int maxHeads = nFlips;

    // calculate maximum length of histogram bars based on number of flips per set
    // based on finding the number of combinations for half heads / half tails
    //                n!
    // formula is -----------  with r = n/2
    //             r! (n-r)!

    int maxLength = factorial (nFlips) / 
                    (factorial (nFlips / 2) * factorial (nFlips - nFlips / 2));

    // instantiate a coin object named myCoin
    Coin myCoin = new Coin();

    // step 1:
    // declare an int array named "counts" to count coin flip occurences
    // make its size one larger than maxHeads
    int []counts = new int [maxHeads + 1]; 
    // step 2:
    // initialize all of the values in the array to 0
    for(int index = MIN_HEADS; index < counts.length; index++)
    // step 3:
    // flip the coin for n sets with nFlips flips per set
    // count the number of heads in each set (heads = 1, tails = 0)
/* what i
      for(int i = 0; i < nSets; i++)
    {
      int sum = 0;
      {
      for(int j = 0; j < nFlips; j++)
      {
//here i will get a sum of heads with the amount of coins flip
        sum += (myCoin.flip()? 1:0);
//what i want to do with this sum is to find a apporiate spot in the counts[index] and add 1 to it
this is what i think of and have tried it som many time but can produce a right result.      
      if(sum == index);
      counts[index]++
      }
    }
    }

    // step 4:
    // print out the estimated probabilities of all heads and all tails
    System.out.println();
    System.out.println("Estimated Probabilities for:");
   System.out.println ("All Tails: " + (float) counts[MIN_HEADS] / nSets);
   //System.out.println ((float) counts[maxHeads] / nSets);

    //System.out.println("All Heads: " + countsAllHeads);
    //System.out.println("All Tails: " + (double)probTails/nSets);

    // step 5: 
    // find largest number in counts array for scaling length of bars

    // step 6:
    // scale data in counts array to maximum length of bars
    // add 0.5 to round off instead of truncate in conversion to int

    // step 7:
    // draw horizontal bar graph (one line per heads value)

    // step8:
    // draw vertical bar graph (one column per heads value)

  }
  // function to calculate factorial of n
  public static int factorial(int n)
  {

    int factorial = 1;

    // write the code for a loop to calculate factorial of n here

  for(int i = 1; i <= n; i++)
  factorial = factorial * i;
  System.out.println( n + "! = "  + factorial);
   return factorial;
  }
  }
```


----------



## JimmySeal (Sep 25, 2007)

It looks like you need to put

counts[sum]++;​immediately after the inner loop.

And as another side note, you have the start of a comment

/* what i​
turning half of the code in your post orange.


----------

