0
votes

process involves adding toppings to individual pudding that have varying sizes that roll out on a conveyor belt.

  1. At least 1g of toppings needs to be added to each pudding.
  2. If two adjacent puddings are of different sizes, the larger piece needs to have at least 1g more than the smaller one.
  3. If two adjacent pieces are of the same size, then the amount of toppings relative to each other does not matter.

XXXXXXX

public class Program
{

    static void Main(string[] args)
    {
        //topping t1 = new topping();
        //t1.CalTopping();

        topping t1 = new topping();
        t1.CalTopping();
    }

    public class topping
    {
     

        public void CalTopping()
        {
            int[] cupcakes = { 1, 2, 2, 6, 2, 1 };
            int tot = 0;
            int topping = 0;
            //int i=0;



            for (int i = 0; i < cupcakes.Length; i++)
            {
                
                if (i + 1 < cupcakes.Length)
                {
                    topping = 1;
                    if (cupcakes[i] == cupcakes[0])
                    {
                        topping = 1;
                    }

                    else if (cupcakes[i] > cupcakes[i - 1])
                    {

                        topping = topping + 1;
                    }
                    else
                        topping = 1;
                }

                tot = tot + topping;
                Console.WriteLine(cupcakes[i] + " = " + topping + "g");
            }
            Console.WriteLine("total toppings Amount:-" + tot + "g");

        }

    }
}

}

what you have tried so far?Nitin S
Please edit your question to show what you have tried so far.Turksarama
What have you tried? Think about keeping track of the previous item and using that and the current item in a loop.Ian Mercer
You are using a foreach loop, i is not an index.gunr2171
I don't see how rule (2) can work if the puddings come out on a conveyor belt. If ten puddings come out in order from large to small, the first pudding will need +9 on its toppings... but you won't know that until you reach the end of the conveyor belt. Are you allowed to run it multiple times?John Wu