Hi Guys,
iI am new to programming and started aabout 2 months ago. I am doing some exercises to help me get my head around the programming language. Here is what i am stuck on;

Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. First the program should ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period. Use Methods to solve the problem.

Here is the code i have wrote out so far. The bit i am stuck on is how do i get the outter loop to work for the number of years the user has entered.My coding is posted below

 static void Main(string[] args)
`        {`
            int Numyears = 0;

            double Rainfall = 0;
            double avrRainfall = 0.0;

            string[] Month = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

            Console.WriteLine("please enter number of years");
            Numyears = int.Parse(Console.ReadLine());

            while(Numyears < 1)

                Console.WriteLine("Minimum number of years is 1 Please re-enter");
                Numyears = int.Parse(Console.ReadLine());


            for (int i = 0; i <= Numyears; Numyears++)

                for (int j = 0; j < Month.Length; j++)
                    Console.WriteLine(" Enter inches of rainfall for {0}", Month [j]);


1 Year
Discussion Span
Last Post by JamesCherrill

You could introduce a method to input an integer.
Like this:

/// <summary>
        /// Read an integer from the console
        /// </summary>
        /// <param name="prompt">descriptive message</param>
        /// <returns>an integer</returns>
        public int readInt(string prompt)
            string line = Console.ReadLine();
            int quantity;
            if (int.TryParse(line, out quantity) == false)
                quantity = 0;
            return quantity;

In your month loop you have to add the inches of rainfall in your rainfall variable, but you also got to know the number of months to calculate the average. Because you know the number of years, this should be easy.

Votes + Comments
i am confused now ,how and where would i enter this in my coding.,does this solve the problem i have with the number of years entered by the user

I was just helping YOU to solve YOUR problem. If you can't appreciate that, so be it.
We don't deliver full fledged 24 carat homework solutions round here.


Line 23
You are incrementing the final value Numyears when you should be incrementing the loop variable i

Votes + Comments
what should the vaule of loop years be if i dont know what the user is going to enter.
Great eyesight!

what should the vaule of loop years be if i dont know what the user is going to enter

I don't understand that question!

You have obtained and validated a value for Numyears from the user (lines 11-20).
Now all you need to on line 23 is to loop with i going from 1 to Numyears. You almost have that right a the moment, except that you are changing Numyears when you should be changing iin the loop.

Votes + Comments
How do i loop i going from 1 to Numyears. Sorry about the confusion ,i have been looking at this for hours.Thanks for your help

Come on now, loop i from 1 to N? That's the simplest, most common type of loop known to man. You have one on line 27 to use as an example.

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.