Calculating Area of Annulus - arrays problems!!!

Printable View

• 05-15-2011, 05:28 AM
hadenkl
Calculating Area of Annulus - arrays problems!!!
This has been solved now - thank you!
• 05-15-2011, 05:56 AM
pbrockway2
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.

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.
• 05-15-2011, 06:28 AM
hadenkl
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.
• 05-15-2011, 06:36 AM
pbrockway2
Quote:

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.
• 05-15-2011, 06:47 AM
hadenkl
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);
• 05-15-2011, 09:30 AM
JosAH
Quote:

Originally Posted by hadenkl
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
• 05-15-2011, 11:27 AM
Fubarable
Quote:

Originally Posted by JosAH
@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:

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 );   } }```
• 05-15-2011, 11:40 AM
JosAH
Quote:

Originally Posted by Fubarable
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