Results 1 to 8 of 8
  1. #1
    hadenkl is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Exclamation Calculating Area of Annulus - arrays problems!!!

    This has been solved now - thank you!
    Last edited by hadenkl; 05-15-2011 at 09:22 AM. Reason: personal information included.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    int column = 0;
    int row = 0; 
    
    double [][] hits = new double [column][row];

    This doesn't look right: it creates a hits array of size zero and arrays never change size. Figure out how many rows and columns the array should have and make the array that big.


    Java Code:
    hits [column][row] = hits [column][row]+1;
    
    hits [column][row] = hits [column][row]/samples;

    Every time you get a hit you increment the hits array at the appropriate entry and then divide that entry by samples. In other words you divide by samples quite a few times. If you are trying to find the "average" hit rate you should do that after all the trials are over.

    Perhaps it would be a good idea to explain in plain language the algorithm you are using.

  3. #3
    hadenkl is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    this is the original pseudocode I'm using below. You're right re: the column/row integers; I put that in without thinking because my array wasn't compiling; i kept getting an unknown variable error on 'column' and 'row'. I'm not sure I have to actually put that in though, because the gridsize is 20 and that should account for the columns/rows. I think there's supposed to be three nested loops in there.
    Last edited by hadenkl; 05-15-2011 at 09:22 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm not sure I have to actually put that in though, because the gridsize is 20 and that should account for the columns/rows.

    That's right. You an declare hits based on gridSize.

    -------------------

    I think possibly there should be some indenting in that pseudocode to show the scope of the for loops.

    But the point I made before stands: you should really have a clear idea yourself about how the area is calculated from random "dart throws" or however you are thinking about them. This might clear up the question about how many for loops are needed and their nesting.

  5. #5
    hadenkl is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    sorry; the indenting didn't come through properly!!! This is what was in my original program, and then the random 'dart throws' was the next stage; which is the bit I kind of don't understand. I understand how they do it etc, but I know I'm supposed to add an inner nested loop for the random samples and I just can't figure out how to make the three loops work!! Instructions were to move the calculation of x to the inner loop, and that's where it all started to go downhill for me. I don't even know WHERE the inner loop is supposed to sit!!

    for (int column = 0; column< gridSize-1;column++) {

    double x = minx +(column + 0.5)*((maxx-minx)/gridSize);
    //set row

    for (int row = 0; row< gridSize-1; row++){
    //set test
    double y = miny +(row + 0.5)*((maxy-miny)/gridSize);

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by hadenkl View Post
    This has been solved now - thank you!
    @OP: this thread doesn't make sense anymore now; please don't edit your original post when your problem is solved.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by JosAH View Post
    @OP: this thread doesn't make sense anymore now; please don't edit your original post when your problem is solved.

    kind regards,

    Jos
    I agree and have sent him a "first warning" message. I think that his code looked something like this though:

    Java Code:
    package yr2011.m05.c;
    
    import java.util.Scanner;
    
    public class AreaOfAnnulus {
       public static void main(String args[]) {
          // set grid size
          int gridSize = 20;
          // set samples
          final int samples = 100;
          // create Scanner object
          Scanner keyboard = new Scanner(System.in);
          // ask user for outer radius
          System.out.println("Please enter the outer radius of the annulus");
          double r1 = keyboard.nextDouble();
          // Ask user for the inner radius
          System.out.println("Please enter the inner radius of the annulus");
          double r2 = keyboard.nextDouble();
          // set minx = 1r1
          double minx = -r1;
          // set maxx = r1
          double maxx = r1;
          // set miny -= -r1
          double miny = -r1;
          // set maxy = r1
          double maxy = r1;
          // set counter = 0
          double counter = 0;
          int column = 0;
          int row = 0;
          double[][] hits = new double[column][row];
          // set column and row for loops
          for (int i = 0; i < 100; i++) {
             double x = minx + (column + Math.random()) * ((maxx - minx) / gridSize);
             for (int j = 0; j < gridSize - 1; j++) {
                double y = miny + (row + Math.random()) * ((maxy - miny) / gridSize);
                // set test
                double test = x * x + y * y;
                if (test < r1 * r1 && test > r2 * r2) {
                   hits[column][row] = hits[column][row] + 1;
                   hits[column][row] = hits[column][row] / samples;
                   counter = counter + hits[column][row];
                   System.out.printf("[r,c]: [%d, %d]%n", row, column);
                }
                // set area
                double area = (maxx - minx) * (maxy - miny) * counter / (gridSize * gridSize);
                System.out.println("Area: " + area);
             }
          }
          System.out.printf("[c, r]: [%d, %d]%n", hits.length, hits[0].length );
       }
    }

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    I agree and have sent him a "first warning" message. I think that his code looked something like this though: [ snip ... ]
    Thanks for that; pbrockway2 already found what was wrong ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Displaying and calculating 2D arrays
    By JSTBO in forum Java 2D
    Replies: 5
    Last Post: 03-07-2011, 01:07 AM
  2. two problems with 2D arrays
    By doha786 in forum New To Java
    Replies: 0
    Last Post: 04-05-2010, 06:06 AM
  3. Replies: 4
    Last Post: 08-12-2008, 01:41 PM
  4. Problems with arrays
    By Marcus in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •