GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Oct-2004, 23:29
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road

more array help (simulation project..)


I am currently in the process of making a program that will simulate an oil spill. The project is going to allow the user to input the number of oil barrels spilled, wind speed. The program also accounts for some evaporation of oil.
I am supposed to use two arrays to show the how the oil moves on the screen (from east to west, which would be for left to right on the screen). The barrels are going to always be spilled in the middle left part of the screen. I am going to allow the user to input the barrels spilled, windspeed, and the number of time steps to process the array. But for now I am just going to input some numbers myself to test it.
The formulas I have to use are:
E % of the oil in a square evaporates;

Of the remaining oil, 10 / (10 + V) of the oil remains in the same square, where V is the wind velocity in miles per hour;

Of the oil which does not evaporate and does not remain in the same square, 40% moves to the square immediately to the east, 20% moves into each of the squares to the north-east and the south-east; and 10% moves into each of the squares to the north and south.

One array holds the old values and one to hold the new values.
Any oil moving beyond the bounds of the array will be lost to the simulation. Also the program should only display numbers to the left of the decimal.
Here is what I have so far.. I am having trouble figuring out how to use the formulas to move the "oil" around in the array.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
using namespace std;

main()
{
	int oilSpill [20] [23], oilSpill2 [20] [23];
	double barrels = 12;
	unsigned int wind, time = 3;
	float evap_rate;

	for (int i = 0; i < 20; i++)
		for (int j = 0; j < 23; j++)
		{	
			oilSpill [i][j] = 0;
			oilSpill2 [i][j] = 0;
		}
	//sets the number of barrels to the left-most center
	oilSpill[10][0] = barrels;

	//calculates where to move oil based on wind/evaporation
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 23; j++)
		{
			oilSpill[i][j] -= 10/(10 + 30); 
		}
	}

	//displays the array
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 23; j++)
		{	
			cout << setw(2)<< oilSpill[i][j] << " ";
		}
		cout << "\n";
	}


return 0;

}
  #2  
Old 16-Oct-2004, 05:58
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello dilmv.

This sounds like a pretty interesting program.

I think that you are definitely on the right track. I did this quick so there could be some typos, but it should get you started.

CPP / C++ / C Code:
main()

for (int i = 0; i < 20; i++)
  {
    for (int j = 0; j < 23; j++)
    {
      if(oilSpill[i][j]>0){  //First of all check to see if there is any oil
        oilSpill[i][j] -= oilspill[i][j]*evap_rate; //Calculate evaporated oil.  This oil is gone forever...
        moved = oilSpill[i][j] - ( 10/(10+wind) * oilSpill[i][j]) );  //Save how much oil is moving for later calculations
        oilSpill[i][j] *= 10/(10 + wind);   //S
        //Move 40% of moved to East
        if(j<22)  //Check for edge of board...
           oilSpill[i][j+1] += moved * .4;
        //Move 20% of the oil to the Northeast
        //Move 20% of the oil to the Southeast
        //Move 10% of the oil to the North
        //Move 10% of the oil to the South
      }
    }
  }

That should get you started. Just remember to check your limits for each direction before you calculate the oil in the square.
  #3  
Old 16-Oct-2004, 10:43
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
Hello dilmv.

This sounds like a pretty interesting program.

I think that you are definitely on the right track. I did this quick so there could be some typos, but it should get you started.

...

That should get you started. Just remember to check your limits for each direction before you calculate the oil in the square.

You also may wish to consider using 2 arrays. Using one you will have different results by processing your array west to east vs east to west. Each time you do a calculation east-west the square with oil will add oil to the western 'square', then when you process that square, you will move both the old oil and the new oil. Switching to two arrays, you build the second array based only on the oil in each square during that timeslice (loop). Then move the new array into the old array and do it again.

For example, spill[4][4] has 100 gallons, spill [4][5] to the west has 5 gallons
Processing spill[4][4] moves 40 gallons into 5,5 making 45. Then in this loop when you process 5,5, you are processing the 45 instead of the actual amount of 5. The next loop is when you want to process the oil from the previous square.
__________________

Age is unimportant -- except in cheese
  #4  
Old 16-Oct-2004, 13:09
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road
Thanks for the help! I made some of the changes that were suggested. I understand what WaltP was taking about, using two arrays so my data is accurate. But I'm not quite sure how to go about using them in the program.
Right now my program moves around the "oil" slightly. However, the oil in the starting "square" becomes zero after the calculations are made to take evaporation away and to move the oil in the other directions.
Also I need the program to not only move the oil to the direct North and South, it has to move North east and South east.
Any more suggestions would be greatly appreciated. Thanks for the continued help :-)



CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
using namespace std;

main()
{
	int oilSpill [20] [23], oilSpill2 [20] [23], moved = 0;
	double barrels = 12;
	unsigned int wind = 15, time = 3;
	float evap_rate = 0.05;

	for (int i = 0; i < 20; i++)
		for (int j = 0; j < 23; j++)
		{	
			oilSpill [i][j] = 0;
			oilSpill2 [i][j] = 0;
		}
	//sets the number of barrels to the left-most center
	oilSpill[10][0] = barrels;

	//calculates where to move oil based on wind/evaporation

	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 23; j++)
		{
		if(oilSpill[i][j]>0)
		{  
			//First of all check to see if there is any oil
			oilSpill[i][j] -= oilSpill[i][j]*evap_rate; //Calculate evaporated oil.  This oil is gone forever...
			oilSpill[i][j] += moved;
			moved = oilSpill[i][j] - ( 10/(10+wind) * oilSpill[i][j] );  //Save how much oil is moving for later calculations
			oilSpill[i][j] *= 10/(10 + wind);  
			//Move 40% of moved to East
			if(j<23)  //Check for edge of board
			oilSpill[i][j-22] += moved * .4;
			//Move 20% to the North East
			if(i<20)
			oilSpill[i-2][j] += moved *.2;
		}
		}
	}

	//displays the array
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 23; j++)
		{	
			cout << setw(2)<< oilSpill[i][j] << " ";
		}
		cout << "\n";
	}


return 0;

}
  #5  
Old 16-Oct-2004, 14:50
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi dilmv. It appears that you have already defined two arrays in order to do as WaltP suggested. You should probably work on implementing that. Just go through your first array (oilspill) and put the "new" oil amounts into your second array (oilspill2). Then after that is complete, just run through and add all of the oil from oilspill2 to oilspill and clear oilspill2 to start again.

As for the other things, you changed something that I am not quite sure of:

CPP / C++ / C Code:
oilSpill[i][j] += moved;
This confuses me. moved is not the carry over. It should not be added in to the next element like this...

CPP / C++ / C Code:
//Move 40% of moved to East
if(j<23)  //Check for edge of board
  oilSpill[i][j-22] += moved * .4;
Why j-22? isn't it just going one position to the east? That would be just j+1 (assuming that j is your east-west direction...)

CPP / C++ / C Code:
//Move 20% to the North East
if(i<20)
	oilSpill[i-2][j] += moved *.2;

Again northeast should be i-1 and j+1. So you should have something like:
CPP / C++ / C Code:
if( (i>0) && (j<23) )
  oilspill2[i-1][j+1] += moved * 0.2;

Notice that I used the oilspill2 array.

One thing that you may want to do is throw in some cout statements if you aren't getting the results that you want. How much is leaving in evaporation? Does that seem correct? How much is left? Does that seem correct? etc.

I'd be interested in what you came up with. This should show a pretty good migration of the oil I believe.
  #6  
Old 16-Oct-2004, 21:16
dilmv dilmv is offline
New Member
 
Join Date: Oct 2004
Posts: 16
dilmv is on a distinguished road
Thanks again for the help. I added a few things. I added a loop that should control the number of timesteps to process the oil. However, I am having trouble getting the oil to continue to move on the screen more than a few times.
Here is the current code:
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
using namespace std;

main()
{
	int oilSpill [20] [23], oilSpill2 [20] [23], moved = 0;
	double barrels = 600;
	unsigned int wind = 15, time = 10;
	float evap_rate = 0.05;

	for (int i = 0; i < 20; i++)
		for (int j = 0; j < 23; j++)
		{	
			oilSpill [i][j] = 0;
			oilSpill2 [i][j] = 0;
		}
	//sets the number of barrels to the left-most center
	oilSpill[10][0] = barrels;

	//calculates where to move oil based on wind/evaporation
for(int timestep = 0; timestep<time; timestep++)
{
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 23; j++)
		{
		if(oilSpill[i][j]>0)
		{  
			//First of all check to see if there is any oil
			oilSpill[i][j] -= oilSpill[i][j]*evap_rate; //Calculate evaporated oil.  This oil is gone forever...
			moved = oilSpill[i][j] - ( 10/(10+wind) * oilSpill[i][j] );  //Save how much oil is moving for later calculations
			oilSpill[i][j] *= 10/(10 + wind);  
			//Move 40% of moved to East
			if(j<23)  //Check for edge of board
			oilSpill2[i][j] += moved * .4;
			//Move 20% to the North East
			if(i<20)
			oilSpill2[i][j] += moved *0.2;
			if( (i>0) && (j<23) )
			oilSpill2[i-1][j+1] += moved * 0.2;
			//move 10% to south
			if ( (i>0) && (j<23) )
			oilSpill2[i+1][j+1] += moved * 0.1;
		}
		}
	}
}
	//displays the array
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 23; j++)
		{	
			cout << setw(2)<< oilSpill2[i][j] << " ";
		}
		cout << "\n";
	}


return 0;

}

Sorry for asking all these questions but I know I will eventually get the hang of arrays and processing them. Thanks again for the help
  #7  
Old 17-Oct-2004, 07:51
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi dilmv. There are still some problems with your program and is still seems that you are struggling with NORTH, SOUTH & EAST. The #define statement can do wonders for readability in my opinion. I added:
CPP / C++ / C Code:
#define NORTH	i-1
#define SOUTH	i+1
#define EAST	j+1

To the top of the program to help me know which way I needed to move things.

Also, your oil arrays should be doubles (as your barrels is). I am surprised that your compiler didn't give you warnings, mine gave me tons . Warnings are signifigant in C.

Also, some of the problem is around C's treatment of integer division. This statement actually always results in 0 because C treats the first statement as integer division:
CPP / C++ / C Code:
moved = oilSpill[i][j] - ( 10/(10+wind) * oilSpill[i][j] );

The easy fix is to specify your constants as decimals.
CPP / C++ / C Code:
moved = oilSpill[i][j] - ( 10.0/(10.0+wind) * oilSpill[i][j] );

I used some printfs at the end of the program that you should probably change. I can not stand using the ghastly iostream library and didn't want to look up what I wanted to do.

Here is what I came up with. You should change it to be more dynamic. I also added a summation to make sure that I am not creating oil or loosing too much oil.
CPP / C++ / C Code:
 
#include <iostream>
#include <iomanip>
using namespace std;

#define NORTH	i-1
#define SOUTH	i+1
#define EAST	j+1

main()
{
  double oilSpill [20] [23], oilSpill2 [20] [23], moved = 0;
  double barrels = 600;
  double	check;
  unsigned int wind = 15, time = 10;
  float evap_rate = 0.05;

  for (int i = 0; i < 20; i++)
    for (int j = 0; j < 23; j++)
    {  
      oilSpill [i][j] = 0;
      oilSpill2 [i][j] = 0;
    }
  //sets the number of barrels to the left-most center
  oilSpill[10][0] = barrels;

  //calculates where to move oil based on wind/evaporation
	for(int timestep = 0; timestep<time; timestep++){
		for (int i = 0; i < 20; i++){
			for (int j = 0; j < 23; j++){
				if(oilSpill[i][j]>0){		//First of all check to see if there is any oil
					oilSpill[i][j] -= oilSpill[i][j]*evap_rate; //Calculate evaporated oil.  This oil is gone forever...
					moved = oilSpill[i][j] - ( 10.0/(10.0+wind) * oilSpill[i][j] );  //Save how much oil is moving for later calculations
	  
					//Handle oil at current position
					oilSpill[i][j] *= 10.0/(10.0 + wind);  
					
					//Handle East Moving oil
					if(j<22)  
						oilSpill2[i][EAST] += moved * 0.4;
		
					//Handle South moving oil
					if(i<19){
						oilSpill2[SOUTH][j] += moved * 0.1;
						if(j<22)
							oilSpill2[SOUTH][EAST] += moved * 0.2;
					}
		
					//Handle North Moving oil
					if(i>0){
						oilSpill2[NORTH][j] += moved * 0.1;
						if(j<22)
							oilSpill2[NORTH][EAST] += moved * 0.2;
					}
      
				}
			}
		}
		
		//Update 1st array & Clear proces array
		for (int i = 0; i < 20; i++)
			for (int j = 0; j < 23; j++){  
				oilSpill [i][j] += oilSpill2[i][j];
				oilSpill2 [i][j] = 0;
			}
			
		//displays the array
		check = 0;
		for (int i = 0; i < 20; i++){
			for (int j = 0; j < 23; j++){
				printf("%4.1f  ", oilSpill[i][j]);
				check += oilSpill[i][j];
				//cout << setw(2)<< oilSpill[i][j] << " ";
			}
			cout << "\n";
		}
		printf("\nThere is currently %4.1f barrels of oil in the ocean.\n",check);
		cout << "\nPress enter for the next time step.\n";
		cin.get();
	}


	return 0;

}

 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
looking for a way to identify a repetitive sequence of digits in an array or string sho C++ Forum 8 14-Jun-2004 23:59
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52
c: array comparison jack C Programming Language 7 26-Jan-2004 11:21
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.