Graph Printing

Question :
Draw a bar chart of 5 values entered by the user, where the y-axis is along the rows and the x-axis is along the columns of the screen. Y-axis must be equal to the maximum number entered...
I entered number 6, 2, 10, 8, 9
As you see y-axis is being at maximum...
10 *
09 * *
08 * * *
07 * * *
06 * * * *
05 * * * *
04 * * * *
03 * * * *
02 * * * * *
01 * * * * *
1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main(){
        int ymax,a,b,c,d,e;
        cin>>a>>b>>c>>d>>e;
        for(int y = 10; y >= 1; --y){
           cout<<y<<"\t";
           for(int x = 1; x<= 5 ; ++x){
               cout<<" *  ";
           }
           cout<<endl;
        }
        for(int x = 1; x<= 5 ; ++x){
            cout<<"\t "<<x<<" ";
        }
}


Can you code it for me to help to some extent just need...
I just started to lean c++ a week back...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main(){
        int ymax,a,b,c,d,e;
        cin>>a>>b>>c>>d>>e;
		string sa(a,'*'); //c++ is becoming more about letting the language
		string sb(b,'*'); //do simple things for you.  this constructor fills in
		string sc(c,'*'); //the string for you, N times based off first parameter. 
		string sd(d,'*'); //when you find yourself doing loops to edit strings
		string se(e,'*');  //there is very likely a better answer. 
                string el = "\n"; //this would be better if you knew functions... 
                                         //as repeating code over and over is not clean. 
		cout << sa<<el<< sb<<el<< sc<<el<< sd<<el<< se<<el;
		
}
Last edited on
@ninjz98

jonnin's example couts the values horizontally, whereas, you seem to want the values displayed vertically. I added to your program with an array to keep better track of your inputted values and added in a checking for values over 10 or under 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int values[5], x, y;
	cout << "Enter 5 integers values for the graph.. (0 thru 10 only, please.)" << endl; 
	for(x=0;x<5;x++)
	{
		do
		{
			cout << "Enter value number " << x+1 << " >> ";
			cin>>values[x];
		}while (values[x] > 10 || values[x] < 0);
	}

	for(y = 10; y > 0; y--)
	{
		if(y<10)
			cout << " "; // Add space if y less than 10 to keep column even
		cout << y << " ";
		for(x = 0; x < 5 ; x++)
		{
			if(values[x] == y)
			{
				cout<<" * ";
				values[x]--; // Reduce the value to keep in line with y loop
			}
			else
				cout<<"   ";

		}
		cout << endl;
	}
	cout << "   ";
	for(int x = 1; x < 6 ; x++){
		cout<<" " << x << " ";
	}
	cout << endl << endl;
}
Hello ninjz98,

Could you provide exactly what the output should look like, so no one has to guess at what you are trying to achieve.

I feel that my idea of what the output should look like is much different than what you have said so far.

What you have posted so far defines int ymax,a,b,c,d,e;, but none of these values are used in your program.

jonnin has a good idea, but not sure if you can use it, but it is the simplest way to achieve what you want.

Andy
Hello ninjz98,

With a little input from whitenite1 example I did manage to achieve this for output.

Enter value number 1 >> 6
Enter value number 2 >> 2
Enter value number 3 >> 10
Enter value number 4 >> 8
Enter value number 5 >> 9

10  *   *   *   *   *   *   *   *   *   *
 9  *   *   *   *   *   *   *   *   *
 8  *   *   *   *   *   *   *   *
 7
 6  *   *   *   *   *   *
 5
 4
 3
 2  *   *
 1
    1   2   3   4   5   6   7   8   9  10



Not sure if that is what you are looking for.

Andy
@Handy Andy

Could you provide exactly what the output should look like


ninjz98 does show what the output looks like, in his op. He shows 5 columns with numbers 1 to 10, though they are not in the order of his shown inputs.
@whitenite1,

As I said my idea is totally different from OP's first post. To me printing five " * " for every number is wrong for a bar graph.

Why enter numbers for "a" - "e" if you are not going to use them and then why print the graph that way. It does not make any sense to me.

it also says:
Y-axis must be equal to the maximum number entered...
To me that means that 10 will have 10 " * " and 2 will have 2 " * ". That is why the confusion.

Andy
Yeah, a fun challenge!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Draw a graph!
// Guaranteed to get you either an 'A' or an 'F'.
//
// Save this file as UTF-8. Then compile with one of:
//
//   cl /EHsc /W4 /Ox /std:c++17 /utf-8 a.cpp
//   clang++ -Wall -pedantic-errors -O3 -std=c++17 a.cpp -o a
//   g++ -Wall -pedantic-errors -O3 -std=c++17 a.cpp
//
// Takes a single, optional argument specifying the maximum number
// of lines (terminated by newline) to output. Defaults to 25.
//
// Takes the values from standard input, as many as you give it,
// assuming the following:
//
//   ∀x ∈ [0, 1000)
//   9 + 4|xs| < console_width
//

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

// The isatty() function will tell us whether standard input is piped or not.
#ifdef _WIN32
  #include <windows.h>
  #include <io.h>
  #define isatty _isatty
#else
  #include <unistd.h>
#endif

int main( int argc, char** argv )
{
  // Windows needs a little help...
  #ifdef _WIN32
  SetConsoleOutputCP( CP_UTF8 );
  #endif

  // Maximum screen height
  int max_lines = (argc == 2) ? std::stoi( argv[1] ) : 25;

  // Get the values to be graphed from standard input
  std::vector <double> xs;
  {
    if (isatty( 0 )) std::cout << "xs? ";  // Ask the user, if necessary
    double x;
    while (std::cin >> x)
      xs.emplace_back( x );
  }

  // Calculate the vertical dimensions of the graph,
  // fitting it to the available space as necessary.
  double domain  = *std::max_element( xs.begin(), xs.end() );
  double divisor = (domain < (max_lines - 3)) ? 1           : domain / (max_lines - 3);
  int    nlines  = (domain < (max_lines - 3)) ? (int)domain :          (max_lines - 3);

  // Output Glyphs (this is the UTF-8 required part)
  const char* none_bar   = "────";
  const char* half_bar   = "─▄▄▄";
  const char* full_bar   = "─███";
  const char* x_axis     = "─▀▀▀";
  const char* x_axis_0   = "────";
  const char* x_axis_cap = "─ ";
  const char* y_axis     = "├";
  const char* line_cap   = "─ ";
  const char* origin     = "└";
  const char* digits[] = { "₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉" };

  // Draw the graph

  // (draw everything above the X-axis)
  std::cout << std::fixed << "\n";
  for (int n = 0; n < nlines; n++)
  {
    double y2 = (nlines - n) * divisor;
    double y1 = y2 - divisor / 4;
    std::cout << std::setw( 6 ) << std::setprecision( 2 ) << y2 << " " << y_axis;
    for (auto x : xs)
    {
      if      (x > y2) std::cout << full_bar;
      else if (x > y1) std::cout << half_bar;
      else             std::cout << none_bar;
    }
    std::cout << line_cap << "\n";
  }

  // (draw the X-axis)
  std::cout << "  0.00 " << origin;
  for (auto x : xs)
  {
    if (x > divisor / 4) std::cout << x_axis;
    else                 std::cout << x_axis_0;
  }
  std::cout << x_axis_cap << "\n";

  // (draw labels under the X-axis)
  std::cout << std::string( 8, ' ' );
  for (int n = 0; n < xs.size(); n++)
  {
    auto s = std::to_string( n );
    std::string d;
    for (auto& c : s) d += digits[ c - '0' ];
    std::cout << std::setw( 3 ) << "x" << d;
  }
  std::cout << "\n";
}

And an example of the pretty output:
$ a 12
xs? 24 18 7 15.3 15 2 12 3.56 5 .

 24.00 ├─▄▄▄───────────────────────────────── 
 21.33 ├─███───────────────────────────────── 
 18.67 ├─███─▄▄▄───────────────────────────── 
 16.00 ├─███─███───────────────────────────── 
 13.33 ├─███─███─────███─███───────────────── 
 10.67 ├─███─███─────███─███─────███───────── 
  8.00 ├─███─███─────███─███─────███───────── 
  5.33 ├─███─███─███─███─███─────███─────▄▄▄─ 
  2.67 ├─███─███─███─███─███─────███─███─███─ 
  0.00 └─▀▀▀─▀▀▀─▀▀▀─▀▀▀─▀▀▀─▀▀▀─▀▀▀─▀▀▀─▀▀▀─ 
          x₀  x₁  x₂  x₃  x₄  x₅  x₆  x₇  x₈

Take that, lusers!

BTW, I do not recommend you turn this in as your assignment — as that is cheating.
I do recommend you enjoy playing with it and learn something.

    I  HAZ
  
    /' _
  (°.o /
   |  `.
   || ( `.
   cc C_,)~~  
   
    OUTPUT 
Topic archived. No new replies allowed.