Lab 2 Part 2: Happy Household
Overview
Getting Started
A city is simulated using a two-dimensional array of 8 x 8 squares (or points). Each square represents a building block with randomly assigned desirability value ranging from 0 to 9 (you can take 0 for least desirable e.g. recycling plant, heavy industry etc, 9 for most desirable e.g. shopping complex, golf-course etc).
For example, below is a city of 5 x 5 squares.
0 |
1 |
2 |
3 | 4 | |
0 | 8 | 7 | 0 | 5 | 5 |
1 | 4 | 8 | 3 | 1 | 7 |
2 | 0 | 3 | 4 | 7 | 8 |
3 | 2 | 5 | 6 | 2 | 2 |
4 | 7 | 3 | 4 | 6 | 9 |
The happiness of a square is influenced by its 8 adjacent neighbors. For example, the neighbors for square (2,3) are: { (1,2), (2,2), (3,2), (1,3), (3,3), (1,4), (2,4), (3,4) }. Obviously, a square is more "happy" if the adjacent squares have close desirability, but loses "happiness point" if there is a great discrepancy. To simulate this influence, we define the following "Happiness Rules":
1. All square starts with a base happiness point of zero.
2. Compare desirability value of the square with all neighbors (the 8 adjacent squares):
a. If the desirability value is the same, the happiness point increases by 2.
b. If the desirability value differs by one, the happiness point increases by 1.
c. If the desirability value differs by two, the happiness point is not affected.
d. If the desirability value differs by three, the happiness point decreases by 1.
e. If the desirability value differs by more than three, the happiness point decreases by 2.
Observe that squares on the edges and corners of the map will have less than 8 neighbors, for example, the square (0, 0) will only have 3 neighbors { (0, 1), (1, 0), (1, 1) }. For these squares, you only need to consider the valid neighbors i.e. only consider the 3 squares mentioned for square (0, 0).
Calculation of Happiness, an example:
Referring to the sample city above, the square (2, 3) has a happiness value of -5. The detailed calculation is as follows:
Happiness value for square (2,3) is
0 (base value)
-2 (Square (1,2))
-2 (Square (2,2))
+2 (Square (3,2))
-1 (Square (1,3))
+1 (Square (3,3))
+1 (Square (1,4))
-2 (Square (2,4))
-2 (Square (3,4))
= -5
The square (0, 0) has a happiness value of 1. Details as follows:
Happiness value for square (0,0) is
0 (base value)
+1 (Square (0,1))
-2 (Square (1,0))
+2 (Square (1,1))
= 1
After the happiness point for all squares are calculated, identify the happiest household(s) i.e. squares with highest happiness point, and print out the surrounding "environment". Refer to the Sample Output section for display format.
Bits and Pieces
Implements the following functions which would help you in defining the final program. The purpose, input and output of the functions are given to help you. It is up to you to use these functions if you wish. Note that a few of these functions have a close counterparts in Lab 2 Part 1 where you can just copy and modify a little. You are also allowed to define additional functions as you see fit. Remember to code incrementally, one function at a time to minimize the time for debugging.
We assume that following constant declaration (the size of the city) in the
following description:
#define SIZE 8
Remember that you should use the constant "SIZE"
whenever necessary so that the solution is general enough to solve the same
problem for city of any size.
void InitDesirability(int City[][SIZE])
Purpose:
Give all squares in the city a random desirability value ranging from 0 to 9
Input: City(the 2d
array that represents the city)
Output: None. See remark.
Remark: Since the city
is directly manipulated/changed by this
function, no return type is required.
void CalculateHappiness(int City[][SIZE], int Happy [][SIZE]);
Purpose:
Calculate the happiness point for all squares in the city
Input: City (the 2d
array that contains the desirability value for all squares)
Happy (the 2d array that contains the happiness point for all squares)
Output: None. See remark.
Remark: Since the
array Happy is directly manipulated/changed by this
function, no return type is required.
int FindHighestHappiness(int Happy[][SIZE]);
Purpose:
Find the Maximum Happiness Point in the array Happy
Input: Happy (the 2d
array that contains the happiness point for all squares)
Output: The highest happiness point
found.
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 PrintAllWithHappiness(int City[][SIZE],int Happy[][SIZE], int MaxHappy);
Purpose:
Print the desirability value of the neighbors for ALL squares that have
the highest happiness value as indicated by MaxHappy. The output format
is
as follows:
House Hold with Happiness of 2
*******************************
Point (2,4):
(1,3)7 (1,4)4 (1,5)0
(2,3)6 (2,4)6 (2,5)8
(3,3)5 (3,4)2 (3,5)6
Point (7,5):
(6,4)4 (6,5)3 (6,6)2
(7,4)9 (7,5)3 (7,6)1
(X,X)X (X,X)X (X,X)X
Each point has the format (R,C)D,
where R = Row Number
C = Column Number
D = Desirability of this square
Input: City (the 2d
array that contains the desirability value for all squares)
Happy (the 2d array that contains the happiness point for all squares)
MaxHappy (the highest happiness value found).
Output: None. Directly display on
screen.
Remark: Always print
out nine squares with the household with MaxHappy in the center.
For squares with less than 8 neighbors, print invalid neighbors as (X,X)X.
To reduce the difficulty of this lab, the following function is already defined for you. You can use it to aid your coding.
void PrintArray(char Title[],int Array[][SIZE]);
Purpose: Print the
2D Array using the
following format
Print the Title here..
*****************
0 1 2 ..... 7
0
1
2
...
7
Each column is separated by a tab.
Input:
Array (an 2d array, for example can be the city or the happiness array)
Title (A character message)
Output: None. Directly display on
screen.
Remark: This function is
already coded in the skeleton code.
There is no need to implement it.
Sample usage:
PrintArray("Desirability",Desirability);
The above statement assume that the "Desirability" is the 2d array that
contains the desirability values for the whole city.
Putting it together
With the help of the functions above, you can now design the happy household main function. The basic steps is given as follows:
Basic Steps:
1. Initialize the Desirability Array.
2. Calculate the corresponding
Happiness Array.
3. Print out Both of the
arrays.
4. Find the maximum happiness value,
MaxHappy.
5. Use MaxHappy to
print out the environment of the happiest household(s).
Extra: Random Number Generator and Your Lucky Number
The C language has its built in random number generator (RNG) via two built in functions in <stdlib.h>:
rand( ) gives a random positive number.
srand( ) set up the RNG by taking in a "seed" number. By passing in a different seed number, the rand( ) function would give a different sequence of random numbers.
To give your program a little more predictability (very helpful during debugging process), the skeleton program given includes code to ask user for a lucky number, which will be used as the "seed" number. By using the same "lucky number", you will be able to get exactly the same city layout every time. Also, by using the same "lucky number" as indicated in the sample output, you will be able to compare your program output with ours.
Because of this reason, remember NOT to invoke the srand( ) function again in your code.
Skeleton Program
#include <stdio.h>
#include <stdlib.h>
#define SIZE 8
void InitDesirability(int [][SIZE]);
void CalculateHappiness(int [][SIZE],int[][SIZE]);
int FindHighestHappiness(int[][SIZE]);
int IsValid(int,int);
void PrintArray(char[],int [][SIZE]);
void PrintAllWithHappiness(int[][SIZE],int[][SIZE],int);
int main()
{
int Desirability[SIZE][SIZE], Happiness[SIZE][SIZE];
int lucky;
// Do NOT modify the following:
printf("What is your lucky number?");
scanf("%d",&lucky);
srand(lucky);
//Start Your Code Here
return 0;
}
void InitDesirability(int city [][SIZE])
{
}
void CalculateHappiness(int city[][SIZE] ,int Happy[][SIZE])
{
}
int FindHighestHappiness(int Happy[][SIZE])
{
}
int IsValid(int row, int column)
{
}
void PrintArray(char Title[], int Array[][SIZE])
{
int i,j; printf("%s\n",Title); printf("********************\n\n"); //Print the label for the columns for (i = 0; i < SIZE; i++){ printf("\t%d",i); } printf("\n"); //Print the rows. First print out the Row number followed by the //Row's content. for (i = 0; i < SIZE; i++){ printf("%d\t",i); for (j = 0; j < SIZE;j++){ printf("%d\t",Array[i][j]); } printf("\n"); } printf("\n");
}
void PrintAllWithHappiness(int City[][SIZE], int Happy[][SIZE], int MaxHappy)
{
}
Sample Output 1
Note: User Input is in bold.
What is your lucky number? 1 Desirability =========================== 0 1 2 3 4 5 6 7 0 8 8 3 5 1 7 0 9 1 2 6 9 7 4 0 5 3 2 9 3 7 6 6 8 5 1 3 7 4 1 5 2 6 4 5 4 4 2 1 7 4 3 1 6 5 8 8 5 3 7 6 5 3 6 2 4 3 0 4 3 2 8 7 2 7 7 5 9 3 1 4 Happiness =========================== 0 1 2 3 4 5 6 7 0 0 -1 -7 -3 -6 -7 -5 -6 1 -7 -5 -7 -1 -6 -9 -7 -3 2 -7 -7 -1 0 2 -10 1 -7 3 -6 -3 -6 -2 -10 -2 -1 0 4 -3 -5 -6 -7 1 -1 -11 -1 5 -6 -11 -5 -1 -7 -4 -2 -2 6 -4 -4 -4 -14 -1 -1 -1 -9 7 0 -5 -3 -3 -10 2 -2 -3 House Hold with Happiness of 2 ******************************* Point (2,4): (1,3)7 (1,4)4 (1,5)0 (2,3)6 (2,4)6 (2,5)8 (3,3)5 (3,4)2 (3,5)6 Point (7,5): (6,4)4 (6,5)3 (6,6)2 (7,4)9 (7,5)3 (7,6)1 (X,X)X (X,X)X (X,X)X
Sample Output 2
What is your lucky number? 653 Desirability =========================== 0 1 2 3 4 5 6 7 0 4 5 8 1 9 1 0 1 1 3 7 0 2 0 5 2 9 2 4 6 1 4 2 2 6 5 3 7 2 8 6 7 5 8 5 4 1 2 2 0 3 8 3 1 5 3 8 8 6 0 5 3 5 6 7 1 3 3 3 5 8 0 7 3 5 7 4 8 3 4 9 Happiness =========================== 0 1 2 3 4 5 6 7 0 1 -2 -6 -1 -10 -1 -2 0 1 -1 -6 -8 0 -4 -10 -2 -9 2 -1 -5 -6 -8 -2 -4 -1 -1 3 -6 0 -12 -8 -7 -3 -7 0 4 -2 -2 -6 -9 -8 -7 -2 -6 5 -3 -9 -10 -10 -9 -2 -3 -5 6 -5 -10 -4 -3 1 -1 -10 -9 7 -2 -2 -7 0 -9 -1 -4 -3 House Hold with Happiness of 1 ******************************* Point (0,0): (X,X)X (X,X)X (X,X)X (X,X)X (0,0)4 (0,1)5 (X,X)X (1,0)3 (1,1)7 Point (6,4): (5,3)6 (5,4)0 (5,5)5 (6,3)3 (6,4)3 (6,5)5 (7,3)4 (7,4)8 (7,5)3