Answer to Question #306367 in C# for thanh tran

Question #306367

(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)


1
Expert's answer
2022-03-05T05:21:15-0500
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();
        }
 
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS