Print X for value of Array

Viewed 111

I'm trying to print "x" for the value within the array for example the integer 32 would print 32 x's but I'm not sure how to go about doing so.

Any help or pointers on what to do would be great have looked around but can't seem to find anything that helps me without over complicating it.

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

namespace Histogram
{
    class Program
    {

        static void Main(string[] args)
        {

            string output = "";
            int[] x;
            x = new int[18];

            int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };

            const int ARRAY_SIZE = 18;
            int[] z;

            z = new int[ARRAY_SIZE];

            for (int i = 0; i < z.Length; i++)
                z[i] = 2 * i;              

            Console.WriteLine("Element\t \tValue\t \tHistogram\t\t\n");
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                output += i + "\t\t" + y[i] + "\t\t" + y[i] + "\t\t" + "\n";                

            }
            Console.WriteLine(output);
            Console.ReadKey();

        }
    }
}
4 Answers

What you are looking for is already built into String class. It has a constructor to create a string of repeating chars for any length. No need for String Builder or any extra loops, that would be over complicated.

 static void Main(string[] args)
 {
      string output = "";
      const int ARRAY_SIZE = 18;
      int[] x = new int[ARRAY_SIZE];
      int[] z = new int[ARRAY_SIZE];
      int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };

      for (int i = 0; i < z.Length; i++)
            z[i] = 2 * i;

      Console.WriteLine("Element\t \tValue\t \tHistogram\t\t\n");
      for (int i = 0; i < ARRAY_SIZE; i++)
      {
           string bar = new string('X', y[i]);
           output += i + "\t\t" + y[i] + "\t\t" + bar + "\t\t" + "\n";
      }
      Console.WriteLine(output);
      Console.ReadKey();
}

I was not entirely sure if I was following what you wanted to do but this piece of code may be something like what you were doing with your loops.

Used StringBuilder because it "doesn't create a new object in the memory but dynamically expands memory to accommodate the modified string"


    using System;
    using System.Text;
    
    public class Program
    {
        public static void Main()
        {
            string output = "";
            StringBuilder sb = new StringBuilder("x");
            int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };
            //used to determine how many elements in the y array to go through
            const int ARRAY_SIZE = 5;
            int[] z;
            z = new int[ARRAY_SIZE];
            
            for(int i=0; i< z.Length; i++){
                if(sb.Length != y[i])
                    sb.Clear();
                    for(int j=0; j < y[i]; j++)
                    sb.Append("x");
                    
            output += i + "\t\t" + y[i] + "\t\t" + sb + "\t\t" + "\n";
            //clear the string builder var for next set of x's
            sb.Clear();
            }
            Console.WriteLine(output);
        }
    }

The easiest thing would probably be to put the cout statement into 2 for loops. The first one for the length of the Array you want to print and the second one for the Number that is at that place.

As an example (array is the name of your array and ARRAY_SIZE is the length):

for (int x = 0; x < ARRAY_SIZE; x++) {
   for (int y = 0; y <= array[x]; y++) {
       output += x + " ";
   }
   output += "\n";
}

I would use the PadRight method as @TaW suggested. I don't see any reason why you stored your result on the output variable, then printed it.

Please find my solution below(removed unnecessary codes) where I am printing immediately without a need to store anything in the memory:

    static void Main(string[] args)
    {
        const char paddingChar = 'X';
        const string tabs = "\t\t";
        int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37, 17, 56, 99, 34, 75, 36, 12, 8, 100, 77 };

        Console.WriteLine($"Element{tabs}Value{tabs}Histogram");
        for (int i = 0; i < y.Length; i++)
        {
            Console.WriteLine($"{i}{tabs}{y[i]}{tabs}{"".PadRight(y[i], paddingChar)}");
        }

        Console.ReadKey();
    }
Related