Super
Super

Reputation: 43

C# How to display vertical Histogram?

I am very new to C# and I am trying to create a console application to create a vertical and horizontal histogram, made of stars. So I am asking the user for 8 numbers between 1-10 and printing their results onto the screen as a histogram. I need help with displaying the histogram vertically, I've done the horizontal one and can't figure out how to make it vertical. Here's what I've got so far: Would appreciate any help greatly. Thank you in advance. I'd like it to look something like this:

*
*  *
*  *  *
*  *  *  *
*  *  *  *  *(Height of row depends on numbers user enters.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise_3A
{
    class Program
    {
        static void Main(string[] args)
        {
            clsMainMenu MainMenu = new clsMainMenu();
            ConsoleKeyInfo ConsoleKeyPressed;

            do
            {
                MainMenu.DisplayMenu();
                ConsoleKeyPressed = Console.ReadKey(false);
                Console.WriteLine();
                switch (ConsoleKeyPressed.KeyChar.ToString())
                {
                    case "1":
                        clsHistogram Histogram = new clsHistogram();
                        Histogram.CreateHorizontalHistogram();
                        break;
                    case "2":
                        clsHistogram HistogramV = new clsHistogram();
                        HistogramV.CreateVerticalHistogram();
                        break;
                }
            } while (ConsoleKeyPressed.Key != ConsoleKey.Escape);

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise_3A
{
    class clsMainMenu
    {
        public void DisplayMenu()
        {
            Console.WriteLine("1. Create a Horizontal Histogram.");
            Console.WriteLine("2. Create a Vertical Histogram.");
            Console.WriteLine("Press Esc to exit the Program.");
            Console.WriteLine("..................................");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise_3A
{
    class clsHistogram
    {
        string strNumberChosen = "";

        public void CreateHorizontalHistogram()
        {
            Console.WriteLine("Please enter a number between 1 and 10:");

            int[] intHistogramArray = new int[8];

            for (int intCounter = 0; intCounter < 8; intCounter++)
            {
                Console.WriteLine("Enter number " + (intCounter + 1) + " :");
                strNumberChosen = Console.ReadLine(); // Need Data Validation Here.             
            } // Populating Array.

            Console.WriteLine("Your Histogram looks like this: ");
            for (int intcounter = 0; intcounter < 8; intcounter++)
            {
                int intStarPlot = intHistogramArray[intcounter];
                while (intStarPlot > 0)
                {
                    Console.Write(" *");
                    intStarPlot -= 1;
                }
                Console.WriteLine();
            } // Display a Horizontal Array.
        }


        public void CreateVerticalHistogram()
        {
            Console.WriteLine("Please enter a number between 1 and 10:");

            int[] intHistogramArray = new int[8];

            for (int intCounter = 0; intCounter < 8; intCounter++)
            {
                Console.WriteLine("Enter number " + (intCounter + 1) + " :");
                strNumberChosen = Console.ReadLine(); // Need Data Validation Here.
            } // Populating Array.

            Console.WriteLine("Your Histogram looks like this: ");
            for (int intcounter = 0; intcounter < 8; intcounter++)
            {
                int intStarPlot = intHistogramArray[intcounter];
                while (intStarPlot > 0)
                {
                    Console.Write(" * \n");
                    intStarPlot -= 1;
                }
                Console.WriteLine();
            } // Display a Vertical Array.
        }
    }
}

Upvotes: 0

Views: 987

Answers (2)

Barns
Barns

Reputation: 4848

Here is a little test program that might get you started on creating the Vertical Histogram. Notice I combined the last solution I provided for the Horizontal Histogram and made the code more universally applicable:

private static readonly char star = '*';
private static readonly uint minValue = 1;
private static readonly int maxValue = 10;

static void Main(string[] args)
{
     var list = GetHistorgramData();
     CreateHorizontalHistogram(list);
     CreateVerticalHistogram(list);
}

private static void CreateHorizontalHistogram(List<int> list)
{
    Console.WriteLine("Your Horizontal Histogram looks like this: ");
    //foreach(var value in list)
    //{
    //    Console.WriteLine(string.Empty.PadRight(value, star));
    //}

    //Console.WriteLine("Or like this with LINQ");
    list.ForEach(n => Console.WriteLine(string.Empty.PadRight(n, star)));
}



private static void CreateVerticalHistogram(List<int> list)
{
    Console.WriteLine("Your Vertical Histogram looks like this: ");
    for(int i = 0; i < maxValue + 1; i++)
    {
        var displayLine = string.Empty;
        foreach(int x in list)
        {
            displayLine += ((x + i) - maxValue) > 0 ? star.ToString() : " ";
        }
        Console.WriteLine(displayLine);
    }
}



private static List<int> GetHistorgramData()
{
    var limits = "a number between " + minValue + " and " + maxValue + ": ";
    Console.WriteLine("Please enter " + limits);

    var list = new List<int>();

    do
    {
        var message = string.Empty;
        bool isNumber = false;
        bool isRightSize = false;
        int output;

        do
        {
            var input = Console.ReadLine();      
            isNumber = int.TryParse(input, out output);
            if(isNumber)
            {
                isRightSize = minValue <= output && output <= maxValue;
                message = isRightSize ? "That will do: " : "Try again - value is not " + limits + output;
            }
            else
            {
                message = "Try again - " + input + " is not a Number";
            }
            Console.WriteLine(message);
        }while(!isNumber || !isRightSize);

        list.Add(output);
        Console.WriteLine("Entered number at position" + list.Count + " : " + output);
    }while(list.Count < 8);

    return list;
}


Vertical Results:

    *    
   **    
   **    
  ****   
  ****   
 ******  
 ******* 
********
********

for input:

Please enter a number between 1 and 10: 
2
Entered number at position1 : 2
4
Entered number at position2 : 4
6
Entered number at position3 : 6
8
Entered number at position4 : 8
9
Entered number at position5 : 9
6
Entered number at position6 : 6
4
Entered number at position7 : 4
3
Entered number at position8 : 3



NOTE:
I suggest you use the method GetHistorgramData() for both Vertical and Horizontal.
You can decide whether you wish to use LINQ for the Horizontal Histogram or the foreach loop version.
I think I could have made LINQ version for the Vertical Histogram, but I felt that might look confusing.

You might want to restyle the Histogram a bit, but keep in mind the width of a space " " is different than that of the star "*".

Please let me know if you have any questions.

Upvotes: 1

Luuk
Luuk

Reputation: 14929

  int currentValue = 1;
            bool allDone = false;
            Console.WriteLine("Your Histogram looks like this: ");

            while (!(allDone))
            {
                int x = 0;
                for (int intcounter = 0; intcounter < 8; intcounter++)
                {
                    if (intHistogramArray[intcounter] >= currentValue)
                    {
                        Console.Write(" * ");
                    }
                    else
                    {
                        Console.Write("   ");
                        x = x + 1;
                    }
                }
                if (x>=8) { allDone = true; }
                currentValue = currentValue + 1;
                Console.WriteLine();
            }

output:

Your Histogram looks like this:
 *  *  *  *  *  *  *  *
    *  *  *  *  *  *  *
       *  *  *  *  *  *
          *  *  *  *  *
             *  *  *  *
                *  *  *
                   *  *
                      *

If you want to have them bottom-aligned, you have to make some slight modigfications, this is just to give you an idea on how to start.

Upvotes: 1

Related Questions