Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 25th, 2007, 11:30 AM   #1
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
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).
Fall Back Son is offline   Reply With Quote
Old Mar 25th, 2007, 11:48 AM   #2
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
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" .
Fall Back Son is offline   Reply With Quote
Old Mar 25th, 2007, 11:55 AM   #3
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
/* 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];
      }
   }
}
*bummed*
Fall Back Son is offline   Reply With Quote
Old Mar 25th, 2007, 2:21 PM   #4
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
please... help me

Fall Back Son is offline   Reply With Quote
Old Mar 25th, 2007, 5:51 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
         if (i != SIZE)
In the array, you are allowed to access coordinates from 0 to SIZE-1, your test allows your code to access coordinates of SIZE (i.e. when i is SIZE-1). This means you are accessing values outside of your oceans boundaries.
Try changing the test (and all the other SIZE tests) to
         if (i != SIZE - 1)
The Dark is offline   Reply With Quote
Old Mar 25th, 2007, 6:03 PM   #6
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
thanks, tried it already though. that's also why I thought my answers were incorrect.
Fall Back Son is offline   Reply With Quote
Old Mar 25th, 2007, 6:04 PM   #7
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
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];
      }
   }
Fall Back Son is offline   Reply With Quote
Old Mar 25th, 2007, 6:13 PM   #8
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Mar 25th, 2007, 6:28 PM   #9
Duck
Programmer
 
Join Date: Jun 2006
Location: England London
Posts: 72
Rep Power: 3 Duck is on a distinguished road
(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]
Duck is offline   Reply With Quote
Old Mar 25th, 2007, 6:28 PM   #10
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC