ADB
ADB

Reputation: 65

Trouble printing out results of averaging an array

using System;

namespace RainfallHW {
    class Program {
        static void Main(string[] args) {
            string [] months;
            double[] rain;

            months = new string [12] {  "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
            rain = new double[12];

            for (int i = 0; i < rain.Length; i++) {
                Console.Write("Rainfall in {0}: ", months[i]);
                rain[i] = double.Parse(Console.ReadLine());

                while (rain[i] < 0) {
                    Console.Write("Rainfall in {0}: ", months[i]);
                    rain[i] = double.Parse(Console.ReadLine());

                    double avg;
                    double sum = 0;
                    for (int x = 0; x < rain.Length; x++) {
                        sum = sum + rain[i];
                    }
                    avg = sum / 12;
                    Console.WriteLine("");
                    Console.WriteLine("Average Month Rain: ", avg);
                }
            }
        }
    }
}

I am trying to take in the rainfall amount for each month and average it out. I cannot get it to print out my result for the average. I am relatively new to arrays and loops and I am just not too sure where I am going wrong here.

I am currently getting no output whatsoever. The program closes as soon as the inputs for each month is entered.

Upvotes: 2

Views: 485

Answers (3)

levelonehuman
levelonehuman

Reputation: 1505

EDIT: Please consider my edit to the code below. The updated while loop should make sure an actual number is being supplied. With the existing while loop, you will throw an exception if something other than a number is input.

The issue is the code inside your while loop. It looks like maybe this was intended to be a "hey, you didn't put in a valid number, try again." That's fine, but the rest of your code is also inside this loop.

Instead:

using System;

namespace RainfallHW
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] months;
            double[] rain;
            double avg;
            double sum = 0;
            double value;
            string input;
            bool isValid = false;


            months = new string[12] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
            rain = new double[12];

            for (int i = 0; i < rain.Length; i++)
            {
                Console.Write("Rainfall in {0}: ", months[i]);

                while (!isValid)
                {
                    input = Console.ReadLine();
                    //TryParse returns true if input is a number
                    //Then check for > 0
                    if (double.TryParse(input, out value) && double.Parse(input) > 0)
                    {
                        rain[i] = double.Parse(input);
                        sum += rain[i]; //update the sum here instead of its own loop
                        isValid = true;
                    }
                    else 
                    {
                        Console.Write("Rainfall in {0}: ", months[i]);
                    }
                }
            }
            avg = sum / 12;
            Console.WriteLine("");
            Console.WriteLine("Average Month Rain: {0}", avg);
        }
    }
}

This is what I got from what I think you're trying to do. If that's not the case, please let me know and I can take another look.

The way this will run now is

Get Input for month Check for invalid input Loop through final month Loop through rain array to compute average *annual* rainfall

Upvotes: 1

ideafixxxer
ideafixxxer

Reputation: 474

You obviously messed up with curly braces. After avg-calculating block is moved to its right place, it starts working:

string [] months;
double[] rain;

months = new string [12] {  "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
rain = new double[12];

for (int i = 0; i < rain.Length; i++) {
    Console.Write("Rainfall in {0}: ", months[i]);
    rain[i] = double.Parse(Console.ReadLine());

    while (rain[i] < 0) {
        Console.Write("Rainfall in {0}: ", months[i]);
        rain[i] = double.Parse(Console.ReadLine());
    }
}
double avg;
double sum = 0;
for (int i = 0; i < rain.Length; i++) {
    sum = sum + rain[i];
}
avg = sum / 12;
Console.WriteLine("");
Console.WriteLine("Average Month Rain: " + avg);

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

You are pretty close: the code for adding up the rainfall and dividing by the number of months has to happen after the reading loop, not inside it.

Currently you do not print anything because the loop for correcting negative input encloses the code that prints the average.

You can do additions to compute sum as you go. Here is an outline of what you can do:

  • Make a for loop that goes through the 12 month
  • Ask for the rainfall number
  • While the value entered is negative, continue asking for input
  • When a non-negative value is entered, add the value to a running total
  • Once the reading loop has finished, print the running total divided by 12.

Upvotes: 1

Related Questions