# Thread: Calculating Area of Annulus - arrays problems!!!

1. Member Join Date
May 2011
Posts
7
Rep Power
0

## 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.  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.  Reply With Quote

3. Member Join Date
May 2011
Posts
7
Rep Power
0

## 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.  Reply With Quote

4. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.  Reply With Quote

5. Member Join Date
May 2011
Posts
7
Rep Power
0

## 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);  Reply With Quote

6. ##  Originally Posted by hadenkl This has been solved now - thank you!

kind regards,

Jos  Reply With Quote

7. ##  Originally Posted by JosAH 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);
double r1 = keyboard.nextDouble();
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.length );
}
}```  Reply With Quote

8. ##  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  Reply With Quote

#### Posting Permissions

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