(a) Ask the user to enter 5 integer values between 1 and 50. Create a simple bar graph using the character of your choice.
Hint: You may want to try to use a loop within a loop (a nested loop). Don't forget to indent properly! (2A)
For example: If the user were to enter 3, 1, 6, 2, 5, the output might look like:
***
*
******
**
*****
Extension:
(d) [challenge] Make the bar graph horizontal (2T)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[5] ;
int max = 0;
for (int i = 0; i < 5; i++)
{
int num = Convert.ToInt32(Console.ReadLine());
arr[i] = num;
if (arr[i] > max)
max = arr[i];
}
Console.WriteLine("Vertical");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < arr[i]; j++)
{
Console.Write('*');
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Horizontal");
Console.WriteLine();
int rowCounter = 0;
for (int j = 0; j < max; j++)
{
for (int i = 0; i < 5; i++)
{
if(arr[i] >= ( max - rowCounter))
Console.Write('*');
else
Console.Write(' ');
Console.Write(' ');
}
Console.WriteLine();
rowCounter++;
}
Console.ReadKey();
}
}
}
Comments
Leave a comment