```
/*
Author: Jaap Karan Singh for COMP1911
Date created: 24/04/2010
Real Estate
*/
#include <stdio.h>
void print_format(int option); // 1 = header, 0 = footer
void print_stage(int stage); //prints the stage number
int main(int argc, char *argv[]) {
//skip the header line
while (getchar()!='\n') {
}
int len = 0; //total number of records inputted
int home[][5]={{0},{0},{0},{0},{0}}; //introduce the array, [0] = postcode, [1] = month, [2] = year, [3] = bedrooms, [4] = price
int input = scanf("%d%d%d%d%d", &home[len][0], &home[len][1], &home[len][2], &home[len][3], &home[len][4]);
while (input != EOF) {
len++;
input = scanf("%d%d%d%d%d", &home[len][0], &home[len][1], &home[len][2], &home[len][3], &home[len][4]);
}
//printing inputted information into table form
print_format(1);
printf("%d", len);
printf("| %4d %02d %4d %2d %8d |\n", home[7][0], home[7][1], home[7][2], home[7][3], home[7][4]);
//simple for loop to print out all elements
//int index = 0;
//printf("| %4d %02d %4d %2d %8d |\n", home[index][0], home[index][1], home[index][2], home[index][3], home[index][4]);
/*for (index=0; index < len; index++) {
printf("| %4d %02d %4d %2d %8d |\n", home[index][0], home[index][1], home[index][2], home[index][3], home[index][4]);
}*/
print_format(0);
/*//printing sales that only happened in 2009
print_stage(2);
printf("Listing of all sales recorded for the year 2009:\n");
print_format(1);
index = 0;
while (index < len) {
if (home[index][2] == 2009) {
printf("| %4d %2d %4d %2d %8d |\n", home[index][0], home[index][1], home[index][2], home[index][3], home[index][4]);
}
index++;
}
print_format(0); */
/* //print number of sales for each postcode (minimum 1) with average price
printf("---------------------------------------------------------------------------\n");
printf("STAGE 3\n");
printf("---------------------------------------------------------------------------\n");
//introduce 2d array that record the number of sales and their average price in each postcode
int pcode_sale[][3]={}; // [0] = postcode, [1] = no of sales, [2] = total sales
int pcode_len = 1, pcode_index;
pcode_sale[0][0] = home[0][0]; //add first record manually so for loop initialises successfully
for (index=0; index < len; index++) { //it goes through the home array recording each element
for (pcode_index=0; pcode_index < pcode_len; pcode_index++) { //goes through pcode_sale array to check whether postcode already exists
if (pcode_sale[pcode_index][0] == home[index][0]) {
pcode_sale[pcode_index][1]++; //if it does exist increment number of sales
pcode_sale[pcode_index][2] = pcode_sale[pcode_index][2] + home[index][4]; //and add to average price
}
else { //else add new record
pcode_len++;
pcode_sale[pcode_len][0] = home[index][0];
pcode_sale[pcode_len][1] = 1;
pcode_sale[pcode_len][2] = home[index][4];
}
}
}
for (pcode_index=0; pcode_index < pcode_len; pcode_index++) {
printf("Postcode %4d: %d sale%cs%c took place with an average price of %8d\n", pcode_sale[pcode_index][0], pcode_sale[pcode_index][1], 40, 41, pcode_sale[pcode_index][2] / pcode_sale[pcode_index][1]);
}*/
/*
//sort all sales in ascending order according to price, year, month, postcode, bedrooms
printf("---------------------------------------------------------------------------\n");
printf("STAGE 4\n");
printf("---------------------------------------------------------------------------\n");
int ordered = 1; //0 means it is ordered
while (ordered == 1) {
ordered = 0;
for (index = 0; index < len - 1; index++) {
if (postcode[index] < postcode[index+1]) {
int help = postcode[index];
postcode[index] = postcode[index+1];
postcode[index+1] = help;
help = month[index];
month[index] = month[index+1];
month[index+1] = help;
help = year[index];
year[index] = year[index+1];
year[index+1] = help;
help = bedrooms[index];
bedrooms[index] = bedrooms[index+1];
bedrooms[index+1] = help;
help = price[index];
price[index] = price[index+1];
price[index+1] = help;
ordered = 1;
//below else statement should be commented
} else if ((postcode[index] == postcode[index+1]) && (year[index] > year[index+1])) {
int b = numArray[j][1];
numArray[j][1] = numArray[j+1][1];
numArray[j+1][1] = b;
ordered = 1;
}
}
}
*/
/* //determine median price depending on year
printf("---------------------------------------------------------------------------\n");
printf("STAGE 5");
printf("---------------------------------------------------------------------------\n");
int median[][2] = {0}, median_price_array[]={0}; //2d array that stores year, median price and no of sales
int median_len, median_index, median_price_len;
for (index = 0; index < len - 1; index++) { //it goes through the year array recording each element
for (median_index=0; median_index < median_len - 1; median_index++) { //goes through median array to check whether postcode already exists
if (median[median_index][0] == year[index]) {
median[median_index][1]++; //if it does increment number of sales
}
else { //else do the same except add new record
median[median_len][0] = year[index];
median[median_len][1] = 1;
}
}
median_price_array[median_price_len] = price[index];
median_price_len++;
}
//the above can be imporved by sorting the list before hand and then just using the normal index rather than median_price_index
//sort price array in correspondance with year
//fix the code and use the median_price array to store median price since right now it gets discarded. we can still print using below code in worst case but better to store the values in an array
median_index = 0;
int median_price_index=0, median_price, sales = median[median_index][1];
while (median_index < median_len - 1) { //calculate median price and print it
if ( (median[median_index][1] % 2) == 0) { //if even number of sales, average of the two medians
median_price = ( median_price_array[ (sales / 2) ] + median_price_array[ (sales / 2)+1 ] ) / 2; //the logic is written below
} else {
median_price = median_price_array[ (sales / 2) ]; //since array, it should be minus one but using integer type means another plus one. eg: if 5 sales, 3rd sale is median which is stored in array[2] for the first year. However, when divind by 2 answer gives 2.5 where .5 is truncated hence making the answer 2 which is the memory location.
median_price_index = sales - 1; //increment the median_price_array by the no of sales
}
}
printf("Median sales price for %4d is %8d based on %d sales", 1,2,3);
*/
//end of program
return 0;
}
void print_format(int option) {
if (option == 1) {
printf("+=======================================+\n");
printf("| Postcode Month Year Bedrooms Price |\n");
printf("+---------------------------------------+\n");
} else {
printf("+=======================================+\n");
}
}
void print_stage(int stage) {
printf("---------------------------------------------------------------------------\n");
printf("STAGE %d\n", stage);
printf("---------------------------------------------------------------------------\n");
}
```