Program to find amount of water in a given glass

There are some glasses with equal capacity as 1 litre. The glasses are kept as follows:

1
2 3
4 5 6
7 8 9 10

You can put water to only top glass. If you put more than 1 litre water to 1st glass, water overflows and fills equally in both 2nd and 3rd glasses. Glass 5 will get water from both 2nd glass and 3rd glass and so on.
If you have X litre of water and you put that water in top glass, how much water will be contained by jth glass in ith row?

Example. If you will put 2 litre on top.
1st – 1 litre
2nd – 1/2 litre
3rd – 1/2 litre

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        public class Glass
        {
            public double volume;
        }
        public static void Main(string[] args)
        {
            double volumeOfWater = 100;
            int rowNum = 7, colNum = 7;
            double glassCapacity = 1;
            Glass[] glass = new Glass[(rowNum + 1) * (rowNum + 1 + 1)/2];//add an extra row
            //Initialize the glass array and store it in a list
            List<List<Glass>> glassPattern = new List<List<Glass>>();
            for (int glassCounter = 0, row = 0; row < rowNum + 1; row++)
            {
                List<Glass> subList = new List<Glass>();
                for (int col = 0; col <= row; col++)
                {
                    glass[glassCounter] = new Glass();
                    glass[glassCounter].volume = 0;
                    subList.Add(glass[glassCounter++]);
                }
                glassPattern.Add(subList);
            }
            //Fill the top glass
            glassPattern[0][0].volume = volumeOfWater;
            //Mock the waterflow
            int flag = 0;
            for (int i = 0; i < glassPattern.Count - 1 && flag == 0; i++)//for each row but the last
            {
                for (int j = 0; j < glassPattern[i].Count; j++)//each column
                {
                   if(glassPattern[i][j].volume > glassCapacity) //nothing to do if volume <= glassCapacity
                    {
                        double temp = glassPattern[i][j].volume;
                        glassPattern[i][j].volume = glassCapacity;
                        glassPattern[i + 1][j].volume = glassPattern[i + 1][j].volume + (temp - glassCapacity) / 2;
                        glassPattern[i + 1][j + 1].volume = glassPattern[i + 1][j + 1].volume + (temp - glassCapacity) / 2;
                    }
                   if(i == rowNum - 1 && j == colNum - 1)
                    {
                        Console.WriteLine("Volume of water in glass position (" + rowNum + "," + colNum + ") = " + glassPattern[i][j].volume);
                        flag = 1;
                        break;
                    }
                }
            }
        }
    }
}

Output
=======
Volume of water in glass position (7,7) = 0.578125
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s