Lab 3 Part 2: Flooding in the Meteor Garden

Overview

In Part 1 of this lab, we "flooded" an unfortunate city. For the part 2, we will crank up the "disaster" scale by bringing in meteor strike. At the spot of meteor strike, an irregular deep crater (big hole in the ground) will form. The unfortunate ground will then be flooded (again!). The major difference between flooding in a city vs flooding the crater filled ground is that the later has elevation (depth) to be considered. Unlike city, which is basically flat (the water flood in the street only), the elevation of the ground affects the flow of the water in this scenario.

And to make the lab more interesting (or more lethal, depends on your viewpoint ☺), we will start with a totally flat ground and starts bringing down meteor on user chosen locations. When the user is satisfied with his/her map, then the flooding starts.......

Getting Started

As in part 1, the bird's-eye-view of the ground is simulated. To better represent the elevation of the ground, an integer array is used where each number indicates the height of the ground. So, below is an example of a 8 x 8 flat ground (the elevation is zero for every square).

 

0

1

2

3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0

To have a meteor strike, we need two pieces of information:
    1. A coordinate, which indicates the center (peak) of the strike, and
    2. The power of the impact (each point of "power" is equivalent to adding another 10 meters to the depth of the crater), e.g. a power 3 meteor will create a crater of 30 meters deep at its center.

The crater will be deepest at the center and slope towards the top, bottom, left and right direction. Since meteor strike creates pretty random geography, all we know is this: As we travel away from the center, the depth of the crater increase by a fix value. However, the increase rate is different for each meteor. The known possible increasing rate is in the range [5,15] inclusive. In another words, if the depth at the center is -X meters (negative because its lower than flat ground) and the increasing rate for this meteor is +13 (chosen by random), then the squares in the 4 directions mentioned will have depth (-X+13). Note that the process stops when the depth reaches ground level (i.e. stop when elevation is level with flat ground).

Lets see an example:

Meteor Strike at (2,2) of Power 6 (i.e. 60 meters deep)

 

 

0

1

2

3 4 5 6 7
0 -8 -21 -34 -21 -8 0 0 0
1 -21 -34 -47 -34 -21 -8 0 0
2 -34 -47 -60 -47 -34 -21 -8 0
3 -21 -34 -47 -34 -21 -8 0 0
4 -8 -21 -34 -21 -8 0 0 0
5 0 -8 -21 -8 0 0 0 0
6 0 0 -8 0 0 0 0 0
7 0 0 0 0 0 0 0 0

The center at (2,2) is bolded for your reference. Also, you should be able to see that the increasing rate for this meteor is +13 (Compare (2,2) with (1,2) for example).

When multiple meteors strike on the same map, it is possible to have squares that are modified more than once. To resolve the elevation problem for these squares, we enforce a simple rule in our model: the depth of a square can only be decreased.

For example (using the map of the previous example):

Meteor Strike at (4,7) of Power 4

 

 

0

1

2

3 4 5 6 7
0 -8 -21 -34 -21 -8 0 0 0
1 -21 -34 -47 -34 -21 -8 0 -10
2 -34 -47 -60 -47 -34 -21 -10 -20
3 -21 -34 -47 -34 -21 -10 -20 -30
4 -8 -21 -34 -21 -10 -20 -30 -40
5 0 -8 -21 -8 0 -10 -20 -30
6 0 0 -8 0 0 0 -10 -20
7 0 0 0 0 0 0 0 -10
Bolded values (except at (4,7)) indicates depth modified by the second meteor strike. Also, take note that the increasing rate for this meteor is +10.

After we have enough meteor strikes, we can then start flooding the map. The idea is similar to part 1 with a new twist:
        1. Water flows towards the top, bottom, left and right direction.
        2. Water only flows to squares with LOWER or EQUAL elevation.
        3. The water is represented by the value 22 (looks like a wave when you look at it sideways).

For example (using the previous map):

Flood at (2,1)

 

0

1

2

3 4 5 6 7
0 -8 -21 -34 -21 -8 0 0 0
1 -21 -34 -47 -34 -21 -8 0 -10
2 -34 22 22 -47 -34 -21 -10 -20
3 -21 -34 -47 -34 -21 -10 -20 -30
4 -8 -21 -34 -21 -10 -20 -30 -40
5 0 -8 -21 -8 0 -10 -20 -30
6 0 0 -8 0 0 0 -10 -20
7 0 0 0 0 0 0 0 -10

Only two squares are flooded: {(2,1), (2,2)}

Continue from the previous map:

Flood at (3,5)

 

0

1

2

3 4 5 6 7
0 -8 -21 -34 -21 -8 0 0 0
1 -21 -34 -47 -34 -21 -8 0 -10
2 -34 22 22 22 22 22 -10 -20
3 -21 -34 22 22 22 22 22 22
4 -8 -21 -34 -21 -10 22 22 22
5 0 -8 -21 -8 0 -10 -20 -30
6 0 0 -8 0 0 0 -10 -20
7 0 0 0 0 0 0 0 -10

 

Bits and Pieces

Since this is already the third lab, you will be given lesser help. Don't forget that you are allowed to define other functions to aid your coding. You have to follow the program structure given, including writing recursive functions as specified. A few simple/tedious functions are given to let you concentrate on the main problem.

int IsValid(int row, int column);
    Purpose:    Checks whether row and column are in the range 0 to SIZE-1
    Input:      Row (an integer representing the row number)
                Column (an integer representing the column number)
    Output:     1 (True. The row and column are well defined)
                0 (False. Either the row or column is out of range)
    Remark:     You can choose NOT to implement or use this function.

void MeteorStrikeAt(int Map[][SIZE], int row, int column, int height, int inc);
    Purpose:    A recursive function to form a meteor crater on map.
    Input:      Map (the 2d array that represents the Map)
                Row (the row number)
                Column (the column number) 
                Height (the depth of the square)
                Inc (the increasing rate)
    Output:     None.

void FloodAt(int Map[][SIZE], int row, int column, int height);
    Purpose:    A recursive function to simulate the flooding.
    Input:      Map (the 2d array that represents the Map)
                Row (the row number)
                Column (the column number) 
                Height (the elevation of the square where the water flows from)
    Output:     None.
   

 

Putting it together

With the help of the functions above, you can now design the main function. The basic steps is given as follows:

    Basic Steps:

        1. Initialize the Map.
        2. Print Map.
        3. Ask for number of Meteor Strike, nMeteor.
        4. For each meteor:
            a. Ask for coordinate (repeat for invalid coordinate)
            b. Ask for power (no need to check)
            c. Pick a random number from [5,15] inclusive for increasing rate
            d. Form a meteor crater.
            e. Print the New Map.
        6. Ask for number of floods, nFlood to be added to the map.
        7. For each flood:
            a. Ask for coordinate (repeat for invalid coordinate)
            b. Flood the map at the indicated coordinate.
            c. Print Map.

Refer to the sample output section for output format.....

Skeleton Code

 

#include 

#define SIZE 8

//The two functions below are given.
int Reading(char[]);
void PrintArray(char[],int [][SIZE]);

//These are the functions that you need to code. You may
//define extra functions if necessary.
int IsValid(int,int);
int RandomeRange(int,int);
void MeteorStrikeAt(int[][SIZE],int,int,int);
void FloodAt(int[][SIZE],int,int,int);

int main()
{
    int Map[SIZE][SIZE];

    //Starts your code here.

    return 0;
}

int Reading(char msg[])
{
    int input;

    printf(msg);
    scanf("%d",&input);

    return input;
}


void PrintArray(char str[],int a[][SIZE])
{
    int i,j;


    printf("%s\n",str);
    printf("===========================\n\n");

    printf("   ");
    for (i = 0; i < SIZE; i++){
        printf("%3d ",i);
    }
    printf("\n");

    for (i = 0; i < SIZE; i++){
        printf("%2d ",i);
        for (j = 0; j < SIZE;j++){
            printf("%3d ",a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int IsValid(int X, int Y)
{
}

int RandomRange(int Start, int End)
{
}

void MeteorStrikeAt(int Map[][SIZE], int X, int Y, int Height, int inc)
{
}

void FloodAt(int Map[][SIZE],int X, int Y,int Height)
{
}

Sample Output

Note: User Input is in bold.

Initial Map
===========================

      0    1    2    3    4    5    6    7
 0    0    0    0    0    0    0    0    0
 1    0    0    0    0    0    0    0    0
 2    0    0    0    0    0    0    0    0
 3    0    0    0    0    0    0    0    0
 4    0    0    0    0    0    0    0    0
 5    0    0    0    0    0    0    0    0
 6    0    0    0    0    0    0    0    0
 7    0    0    0    0    0    0    0    0

Number of Meteor Strike(s): 2
Row = 2
Column = 2
Power = 6
Meteor Strike At (2,2) of -60 Meters, Increasing at 13 Meters. Done.

Map after Latest Meteor Strike
===========================

      0    1    2    3    4    5    6    7
 0   -8  -21  -34  -21   -8    0    0    0
 1  -21  -34  -47  -34  -21   -8    0    0
 2  -34  -47  -60  -47  -34  -21   -8    0
 3  -21  -34  -47  -34  -21   -8    0    0
 4   -8  -21  -34  -21   -8    0    0    0
 5    0   -8  -21   -8    0    0    0    0
 6    0    0   -8    0    0    0    0    0
 7    0    0    0    0    0    0    0    0

Row = -1
Column = 0
Invalid Coordinate. Try Again

Row = 4
Column = 7
Power = 4
Meteor Strike At (4,7) of -40 Meters, Increasing at 10 Meters. Done.

Map after Latest Meteor Strike
===========================

      0    1    2    3    4    5    6    7
 0   -8  -21  -34  -21   -8    0    0    0
 1  -21  -34  -47  -34  -21   -8    0  -10
 2  -34  -47  -60  -47  -34  -21  -10  -20
 3  -21  -34  -47  -34  -21  -10  -20  -30
 4   -8  -21  -34  -21  -10  -20  -30  -40
 5    0   -8  -21   -8    0  -10  -20  -30
 6    0    0   -8    0    0    0  -10  -20
 7    0    0    0    0    0    0    0  -10

Number of Flood(s): 2
Row = 2
Column = 1
Flood At (2,1) done

Map after Latest Flood
===========================

      0    1    2    3    4    5    6    7
 0   -8  -21  -34  -21   -8    0    0    0
 1  -21  -34  -47  -34  -21   -8    0  -10
 2  -34   22   22  -47  -34  -21  -10  -20
 3  -21  -34  -47  -34  -21  -10  -20  -30
 4   -8  -21  -34  -21  -10  -20  -30  -40
 5    0   -8  -21   -8    0  -10  -20  -30
 6    0    0   -8    0    0    0  -10  -20
 7    0    0    0    0    0    0    0  -10

Row = 3
Column = 9
Invalid Coordinate. Try Again

Row = 3
Column = 5
Flood At (3,5) done

Map after Latest Flood
===========================

      0    1    2    3    4    5    6    7
 0   -8  -21  -34  -21   -8    0    0    0
 1  -21  -34  -47  -34  -21   -8    0  -10
 2  -34   22   22   22   22   22  -10  -20
 3  -21  -34   22   22   22   22   22   22
 4   -8  -21  -34  -21  -10   22   22   22
 5    0   -8  -21   -8    0  -10  -20  -30
 6    0    0   -8    0    0    0  -10  -20
 7    0    0    0    0    0    0    0  -10

Program Terminated. Bye Bye!