/**************************************** * Section : 15 * Machine No. : N * Roll No. : 19CS100XY * Name : Aritra Hazra * Assignment No : 4B * Description : Trapping Rain Water ****************************************/ #include #define MAX 1000000 int main( ) { int n, i, elv[MAX], left, right, leftMax, rightMax, trap, max, j; /* taking an integer array of inputs */ printf("Enter Total Width of the Cityscape: "); scanf("%d", &n); printf("Enter %d Positive Height for Elevation Map: ", n); for (i=0; i max) max = elv[i]; } /* printing the elevation map */ printf("\nThe Elevation Map:\n"); for (i=0; i0; j--) { for (i=0; i elv[i]) /* printing space for shorter buildings */ printf(" "); else /* printing the building floors */ printf("|H|"); } printf("\n"); } for (i=0; i= leftMax) /* new maximum for left */ leftMax = elv[left]; else /* update trapped water with added units from left side */ trap = trap + (leftMax - elv[left]); left ++; } else { if (elv[right] >= rightMax) /* new maximum for right */ rightMax = elv[right]; else /* update trapped water with added units from right side */ trap = trap + (rightMax - elv[right]); right --; } } printf("\nAmount of Trapped Rain Water = %d units\n\n", trap); return 0; }