# For Loop Task Please Help ### Question

• Hi!

I am trying to create 100 random numbers ranging from 11 to 20 then count the frequencies of each number with a horizontal bar chart and a vertical bar chart for the frequencies. This is how far I got, but something is not right. Please help me where I went wrong!

```static void Main(string[] args)
{ // create 100 random numbers ranging 11-20. Count the frequency of each generated number.
Random rnd = new Random();
int max = 0;
int i,j,x;
int[] f = new int;
for ( i=0; i<100; i++)
{
x = rnd.Next(11, 21); //ranging from 11 to 20, so put 21
Console.Write(x + " ");
f[x - 11]++; //count is always added by 1. subtract 11 because the difference is always 11. g
}
Console.Write("\n Frequencies: ");
for (i = 0; i < f.Length; i++)
{
Console.Write(f[i]+" ");
}
//print a horizontal bar chart
Console.WriteLine("\n FRQ Table:");
for (i=0; i<f.Length;i++)
{
Console.WriteLine("    {0,2}   {1,2}", i+11, f[i]);
}
//print a vertical bar chart for  the frequencies
//find max frequency
foreach (int y in f)
{
if (y > max) max = y;
}
Console.WriteLine("vertical bar Chart");
for (i = max; i >= 1; i-- ) // i-- because max frequency will go dwon from 6 to 1
{
for (j = 0; j < f.Length; j++)
{
if (f[j]<i) Console.Write("   ");
else Console.Write(" * ");
}
Console.WriteLine();
}
for (i = 11; i <= 20; i++ )
{
Console.Write("  " + i);
}
Console.ReadKey();``` And how should I approach the horizontal bar chart? I'm really lost...

Thank you!

Monday, April 17, 2017 10:42 PM

### All replies

• Greetings Kimmie.

What exactly do you think is wrong? Apart from the bars not lining up with the numbers, it looks right to me.

Monday, April 17, 2017 11:18 PM
• Hi KimmieKim,

Thank you for posting here.

If you want to get the horizontal bar chart, please try the following code.

``` static void Main(string[] args)
{
// create 100 random numbers ranging 11-20. Count the frequency of each generated number.
Random rnd = new Random();
int max = 0;
int i, j, x;
int[] f = new int;
for (i = 0; i < 100; i++)
{
x = rnd.Next(11, 21); //ranging from 11 to 20, so put 21
Console.Write(x + " ");
f[x - 11]++; //count is always added by 1. subtract 11 because the difference is always 11. g
}
Console.Write("\n Frequencies: ");
for (i = 0; i < f.Length; i++)
{
Console.Write(f[i] + " ");
}
//print a horizontal bar chart
Console.WriteLine("\n FRQ Table:");
for (i = 0; i < f.Length; i++)
{
Console.WriteLine("  {0}   {1}", i + 11, f[i]);
}
//print a vertical bar chart for  the frequencies
//find max frequency
foreach (int y in f)
{
if (y > max)
max = y;
}
Console.WriteLine("vertical bar Chart");

for (i = max; i >= 1; i--) // i-- because max frequency will go dwon from 6 to 1
{
for (j = 0; j < f.Length; j++)
{
if (f[j] < i)
Console.Write("   ");
else
Console.Write(" * ");
}
Console.WriteLine();
}
for (i = 11; i <= 20; i++)
{
Console.Write(" " + i);
}
Console.WriteLine(Environment.NewLine);

string[] str = new string;
int s = 0;
for (i = 11; i <= 20; i++)
{
str[s] = i.ToString();
s++;
}

for (j = 0; j < f.Length; j++)
{
while (f[j] >= 0)
{
str[j] += " * ";
f[j]--;
}
}

foreach (var item in str)
{
Console.WriteLine(item);
}
Console.ReadKey();

}``` I hope this would be helpful.

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Proposed as answer by Friday, April 28, 2017 8:29 AM
Tuesday, April 18, 2017 3:19 AM