I am beginner and still learning c++. So this was a question in my assignment to make a horizontal bar chart something like this,
Example:
Input:
   N1=>8 , N2=>6 , N3=>2 , N4=>10 , N5=>9
10       *
09       * *
08 *     * *
07 *     * *
06 * *   * *
05 * *   * *
04 * *   * *
03 * *   * *
02 * * * * *
01 * * * * *
   1 2 3 4 5

The columns are constant i.e 5 columns. This should be done without arrays as they are not allowed in the assignment and only using loops. It would be much appreciated if someone helps.

Recommended Answers

All 4 Replies

So the if statement can't be used? Harsh assignment there. Remember I have to take your words as they fell.

commented: We can use if statement and other basic tools +0

OK we can use the various loops and IF. Remember we have many design choices here but the goal is NOT to design well here as the assignment forbid arrays.

So we know we have 10 data lines to output so the first ten lines would be our loop.

I can't write code for you since that's your work so psuedo code will suffice.

for row from 10 to 1 
{
 output row and a space
 if n1 => row print a star and space else print two spaces.
 if n2 => row print a star and space else print two spaces.
 if n3 => row print a star and space else print two spaces.
 if n4 => row print a star and space else print two spaces.
 if n5 => row print a star.
 print the newline
} 
Print " 1 2 3 4 5"
commented: Thank you. It worked perfectly. +0

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

namespace ConsoleApp2

class Program
{

    // N1=>8 , N2=>6 , N3=>2 , N4=>10 , N5=>9

    //10       *
    //09       * *
    //08 *     * *
    //07 *     * *
    //06 * *   * *
    //05 * *   * *
    //04 * *   * *
    //03 * *   * *
    //02 * * * * *
    //01 * * * * *
    //   1 2 3 4 5



    static void Main(string[] args)
    {

        int Rows = 0;
        int columns = 0;

        int InputNumCount = 0;

        Console.Write($"Input Rows => ");
        Rows = Convert.ToInt32(Console.ReadLine());

        Console.Write($"Input Colums => ");

        columns = Convert.ToInt32(Console.ReadLine());

        Console.Write($"Input Num Count => ");

        InputNumCount = Convert.ToInt32(Console.ReadLine());

        string str = "";

        int tempInt = 0;

        for (int i = 0; i < InputNumCount; i++)
        {
            Console.Write("N" + i + "=>");
            tempInt = Convert.ToInt32(Console.ReadLine());
            if (InputNumCount - 1 == i)
            {
                str += tempInt.ToString();
            }
            else { 
                str += tempInt.ToString() + ",";

            }

        }

        string[] StrAry = str.Split(',');
        Console.WriteLine();

        for (int i = Rows; i > 0; i--)
        {
            if (i != 10)
            {
                Console.Write("0" + i + "");
            }
            else
            {
                Console.Write(i + "");
            }

            for (int j = 0; j < columns; j++)
            {
                if (StrAry.Length <= j)
                    break;

                if (Convert.ToInt32(StrAry[j]) >= i && Convert.ToInt32(StrAry[j]) != 0)
                    Console.Write("  " + "*");
                else { 
                    Console.Write("   ");
                }
            }

            if (i  == 1) {

                Console.WriteLine();
                Console.Write("    ");

                for (int i1 = 1; i1 <= columns; i1++)
                {
                    Console.Write(i1 + "  ");

                }
            }

            Console.WriteLine();
        }

        Console.ReadLine();

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.