Home > front end >  How do I output monthly rainfall as "*" using arrays C#?
How do I output monthly rainfall as "*" using arrays C#?

Time:10-19

I have created a program which allows a user to enter the rainfall over the course of a year. The program is then meant to output a chart as well as summarized data such as average, maximum, minimum and total rainfall.

While all of my outputs based upon data works, the actual chart which is meant to display the numbers inputted with asterisks isn't working.

I can't quite understand where I have gone wrong. I have linked an example output of the "*" table as well as my actual code below. Any help would be much appreciated. I am a beginner to C# by the way. Example output

        int[] monthrainfall = new int[12];
        int i;
        double average;
        int total = 0;
        int max = 0;
        int min = Int32.MaxValue;
        string bar = "";

        //title
        Console.Write("\n\nRainfall Data:\n");
        Console.Write("**************\n");

        //asks user to input names
        Console.Write("Enter Rainfall for the year:\n");

        for (i = 0; i < 12; i  )
        {
            Console.Write("Enter rainfall for month {0}: ", i   1);
            monthrainfall[i] = Convert.ToInt32(Console.ReadLine());
        }

        //outputs rainfall results
        Console.WriteLine("\nRainfall Chart");
        Console.WriteLine("**************");

        //outputs results for each month
        for (i = 0; i < 12; i  )
        {
            Console.Write("Month {0}: ", i   1);
            bar = bar   "*";
            Console.WriteLine("{0} ", bar[i]);
        }

        Console.WriteLine("\nSummary data");
        Console.WriteLine("**************");

        //calculates the maximum rainfall
        for (int index2 = 0; index2 < 12; index2  ) //inline variable declaration
        {
            if (monthrainfall[index2] > max) max = monthrainfall[index2];
        }
        Console.WriteLine("The maximum rainfall was: "   max);

        //calculates the minimum rainfall

        for (int index3 = 0; index3 < 12; index3  ) //inline variable declaration
        {
            if (monthrainfall[index3] < min) min = monthrainfall[index3];
        }
        Console.WriteLine("The minimum rainfall was: "   min);

        //calculates average rainfall
        for (int index = 0; index < 12; index  )
        {
            total = total   monthrainfall[index];
        }
        average = (total) / 12;

        Console.WriteLine("The average rainfall was: "   average);
        Console.WriteLine("The total rainfall was: "   total);
        Console.ReadKey();

CodePudding user response:

    for (i = 0; i < 12; i  )
    {   //loop every month
        Console.Write("Month {0}: ", i   1);
bar = "";
for(int stars = 0; stars < monthrainfall[i]; stars  )
{
    bar = bar   "*";
}
        Console.WriteLine("{0} ", bar[i]);
    }

but as @Flydog57 said, use the string constructor instead of the additional for-loop

CodePudding user response:

All of the explanation is going to take place in this part of code

//outputs results for each month
for (i = 0; i < 12; i  )
{
    Console.Write("Month {0}: ", i   1);
    bar = bar   "*";
    Console.WriteLine("{0} ", bar[i]);
}

Part 1: on bar = bar "*"; you are adding only one * for each iteration of the loop. However, what you want is to add the number based on what have been entered in monthrainfall.

As @Flydog57 suggested, the modification would be bar = new string('*', monthrainfall[i]);


Part 2: on Console.WriteLine("{0} ", bar[i]); you are only printing one * as you have selected the element bar[i] of the string bar.

The modification would be Console.WriteLine("{0} ", bar);


  • Related