Lab 3 Part 2: Flooding in the Mountain Range

Overview

In Part 1 of this lab, we "flooded" an unfortunate city. For the part 2, we will bring the "disaster" to a mountain range. The major difference between flooding in a city vs flooding in the mountain is that the later has elevation (height) 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 the mountain range.

And to make the lab more interesting (or more lethal, depends on your viewpoint ☺), we will start with a totally flat ground and "grow" / "make" mountain(s) 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 "grow" a mountain, we indicate two pieces of information:
    1. A coordinate, which indicates the center (peak) of the mountain, and
    2. The elevation of the mountain (the height of the peak) in meters.

The mountain will have the greatest elevation at the peak and slope towards the top, bottom, left and right direction. As we travel away from the peak, the height decrease 10 meters per square to simulate the slope.

Lets see an example:

Grow Mountain at (4,4) of 40 meters

 

0

1

2

3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 0 0 0 10 0 0 0
2 0 0 0 10 20 10 0 0
3 0 0 10 20 30 20 10 0
4 0 10 20 30 40 30 20 10
5 0 0 10 20 30 20 10 0
6 0 0 0 10 20 10 0 0
7 0 0 0 0 10 0 0 0

The peak at (4,4) is bolded for your reference.

When we "grow" multiple mountains on the same map, it is possible to have squares that belong to multiple mountains. To resolve the elevation problem for these squares, we enforce a simple rule in our model: the elevation of a square can only be increased.

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

Grow Mountain at (4,0) of 30 meters

 

0

1

2

3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 0 0 0 10 0 0 0
2 10 0 0 10 20 10 0 0
3 20 10 10 20 30 20 10 0
4 30 20 20 30 40 30 20 10
5 20 10 10 20 30 20 10 0
6 10 0 0 10 20 10 0 0
7 0 0 0 0 10 0 0 0

For square (4,1), the elevation changes from 10 meters to 20 meters (increasing). So, the change is ok.
For square (4,2), the elevation should change from 20 meters to 10 meters (decreasing). So, the change is ignored.

 

After the user is satisfied with the map, we can 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 -1.

For example (using the previous map):

Flood at (5,1)

 

0

1

2

3 4 5 6 7
0 0 0 0 0 0 0 0 0
1 0 0 0 0 10 0 0 0
2 10 0 0 10 20 10 0 0
3 20 10 10 20 30 20 10 0
4 30 20 20 30 40 30 20 10
5 20 -1 -1 20 30 20 10 0
6 10 -1 -1 10 20 10 0 0
7 -1 -1 -1 -1 10 0 0 0

Continue from the previous map:

Flood at (4,6)

 

0

1

2

3 4 5 6 7
0 -1 -1 -1 -1 -1 -1 -1 -1
1 -1 -1 -1 -1 10 -1 -1 -1
2 10 -1 -1 10 20 10 -1 -1
3 20 10 10 20 30 20 -1 -1
4 30 20 20 30 40 30 -1 -1
5 20 -1 -1 20 30 20 -1 -1
6 10 -1 -1 10 20 10 -1 -1
7 -1 -1 -1 -1 10 -1 -1 -1

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 MoutainAt(int Map[][SIZE], int row, int column, int height);
    Purpose:    A recursive function to "Grow" a mountain on map.
    Input:      Map (the 2d array that represents the Map)
                Row (the row number)
                Column (the column number) 
                Height (the elevation of the square)
    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 mountains, nMountain to be added to the map.
        4. For each mountain:
            a. Ask for coordinate (repeat for invalid coordinate)
            b. Ask for elevation (no need to check)
            c. Grow the mountain.
        5. Print 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.
        8. 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);
void MountainAt(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)
{
}

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

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 Mountain(s) to Make: 2
Row = 8
Column = 8
Invalid Coordinate. Try Again

Row = 4
Column = 4
Height = 40
Mountain At (4,4) of 40 meters done

Row = 4
Column = 0
Height = 30
Mountain At (4,0) of 30 meters done

Map after Mountain Phase
===========================

     0   1   2   3   4   5   6   7
 0   0   0   0   0   0   0   0   0
 1   0   0   0   0  10   0   0   0
 2  10   0   0  10  20  10   0   0
 3  20  10  10  20  30  20  10   0
 4  30  20  20  30  40  30  20  10
 5  20  10  10  20  30  20  10   0
 6  10   0   0  10  20  10   0   0
 7   0   0   0   0  10   0   0   0

Number of Flood(s): 2
Row = -1
Column = 4
Invalid Coordinate. Try Again

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

Row = 4
Column = 6
Flood At (4,6) done

Map after Flood Phase
===========================

     0   1   2   3   4   5   6   7
 0  -1  -1  -1  -1  -1  -1  -1  -1
 1  -1  -1  -1  -1  10  -1  -1  -1
 2  10  -1  -1  10  20  10  -1  -1
 3  20  10  10  20  30  20  -1  -1
 4  30  20  20  30  40  30  -1  -1
 5  20  -1  -1  20  30  20  -1  -1
 6  10  -1  -1  10  20  10  -1  -1
 7  -1  -1  -1  -1  10  -1  -1  -1

Program Terminated. Bye Bye!