![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
Question about two dimensional arrays.
I'm using two dimensional arrays to simulate an ocean which contains an oil spill. The wind pushes the oil, and it moves in the ocean (aka array). So I have to make calculations (where the wind pushes the oil) on the values of my "original" ocean and those become the values in the "new" ocean. However, whenever I try this, I print out the new array every x time units. I am getting huge answers that are incorrect. I'll post my code shortly. . thanks for any help (no code though, please).
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
Ok, no longer getting huge answers that don't make sense.. but smaller answers that don't make sense, and my ocean isn't spreading enough.
Btw, the reason for all the if statements is that the array can't be evaluated at -1 or anything greater than "SIZE" . |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
/* this function should "blow" the oil to the West in this manner:
the start space ends up with half it's original oil. the two spaces directly above and below the start space get 5% of the start space's oil. The space up and to the right of the start space gets 10% . the space down and right gets 10%. The space to the right of the start space gets 20% */
void West (int OldOceanArray[SIZE][SIZE], int TransitionOcean[SIZE][SIZE])
{
int i ;
int j ;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j ++)
{
TransitionOcean[i][j] = (OldOceanArray[i][j]/2) + OldOceanArray[i][j];
if (i != SIZE)
{
TransitionOcean[i+1][j] = (OldOceanArray[i][j] * (.05)) + OldOceanArray[i+1][j];
}
if (i != 0)
{
TransitionOcean[i-1][j] = (OldOceanArray[i][j] * (.05)) + OldOceanArray[i-1][j];
}
if (i != 0 && j != SIZE)
{
TransitionOcean[i-1][j+1] = (OldOceanArray[i][j] * (.1)) + OldOceanArray[i-1][j+1];
}
if (j != SIZE)
{
TransitionOcean[i][j+1] = (OldOceanArray[i][j] * (.2)) + OldOceanArray[i][j+1];
}
if (i != SIZE && j != SIZE)
{
TransitionOcean[i+1][j+1] = (OldOceanArray[i][j] * (.1)) + OldOceanArray[i+1][j+1];
}
}
}
for (i = 0; i < SIZE; i ++)
{
for (j = 0; j < SIZE; j++)
{
OldOceanArray[i][j] = TransitionOcean[i][j];
}
}
} |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
please... help me
![]() |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
if (i != SIZE) Try changing the test (and all the other SIZE tests) to if (i != SIZE - 1) |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
thanks, tried it already though. that's also why I thought my answers were incorrect.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
New code:
/* This code is different because it relies on the fact that each "square" of the array should receive a certain percent of the oil from the squares around it. */ void West (int OldOceanArray[SIZE][SIZE], int TransitionOcean[SIZE][SIZE])
{
int i ;
int j ;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j ++)
{
TransitionOcean[i][j] = (OldOceanArray[i][j]/2) + (OldOceanArray[i+1][j]/20) + (OldOceanArray[i-1][j]/20) + (OldOceanArray[i][j-1]/5) + (OldOceanAr\
ray[i+1][j-1]/10) + (OldOceanArray[i-1][j-1]/10);
}
}
for ( i = 0 ; i < SIZE ; i ++)
{
for (j = 0 ; j < SIZE ; j ++)
{
OldOceanArray[i][j] = TransitionOcean[i][j];
}
} |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
I don't know what I'm doing wrong (obviously) but I continue to get negative answers.
I'm going to put the if statements back in for this. that should eliminate the negative answers, but they will still be wrongl ol. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jun 2006
Location: England London
Posts: 72
Rep Power: 3
![]() |
(In your last code snippet)..You'll also be accessing memory outside the arrays boundaries when i or j equals zero, since:
OldOceanArray[i-1][j-1] |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 204
Rep Power: 2
![]() |
No. My answers are still negative. I don't understand how I can be getting negative answers, considering there is a check in place (with what you said, SIZE - 1) for both i and j. I'm trying to make the program go through the for loop, and for each square of the temporary "ocean" (TransitionOcean) fill it with the appropriate values from the OldOceanArray. Each square should get 50% of what it used to have, 5% from the square above it, 5% from the square below it, 20% from the square behind it, and ten percent from both the square down and left & the square down and right. If I use the for loop to do these calculations for each square, and do it for every square, shouldn't I get the right answers?
Despite the various ways I've tried the math, nothing has gotten correct answers. And this method as well as several other methods I have used make sense from a mathematical standpoint, I believe, so it must be something I am doing in the program which is wrong. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Quick question about arrays and functions | Writlaus | C++ | 10 | Apr 9th, 2006 9:15 AM |
| Question about arrays of objects | crazykid48x | C++ | 10 | Nov 13th, 2005 12:16 PM |
| One Dimensional Arrays | JavaBeginner | Java | 2 | Oct 27th, 2005 7:38 PM |
| Question about multidimensional arrays of strings | aznluvsmc | C | 8 | Oct 15th, 2005 10:20 PM |
| Another random question, DataTable vs Array(s) | Arla | C# | 1 | Apr 26th, 2005 8:10 PM |