Printing characters from array values

How would I get this to print out?

Sorry, the ouput isn't showing up like it should!
The amount of "=" in each column should equal the corresponding value in the array.

=
= =
= = = = =
= = = = = = = = = = =
= = = = = = = = = = = = = = = = =
-------------------------------------

I know I have to use for loops but I can't figure out the correct way to nest them. Also I have to use the array
map[LENGTH] = {2,1,3,1,1,2,3,5,3,2,1,0,2,1,2,4,1,2};

The numbers in the array correspond to the amount of equal signs in each column.
I can get the bottom line to print out but the others won't match up with it. Any help is greatly appreciated!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
using namespace std;

int main()
{
	int map[18] = { 2, 1, 3, 1, 1, 2, 3, 5, 3, 2, 1, 0, 2, 1, 2, 4, 1, 2 };

	// move through the array obtaining the number of = required.
	for (int down = 0; down < 18; down++)	// 18 is map array size
	{
		// grab the number and perform a loop as per the value
		// drawing the required =
		for (int across = 0; across < map[down]; across++)
			cout << "=";
		// loop done, do a new line
		cout << endl;
	}
}



EDIT: If your not sure what's going on in the code give me a shout :)
Last edited on
Do you mean that {2,1,3,0,1,2} should look like:
  =
= =  =
=== ==

i.e. that a number in the array corresponds to the height of a "peak" in that column?

If so, then the outer loop iterates over rows and the inner loop iterates current row. Each position (row,col) prints '=' or ' ' depending on whether peak in map[col] reaches to that row.
@Softrix This is what I was looking for but i need to turn it horizontal. Any ideas?

@keskiverto Yes that is what I mean. Can you explain further?

Thanks!

instead of the cout<<endl; do a cout<<" ";

Is that what you mean?

It should be like

2=, 1=, 3=, 1=, 1=, 2=, ... etc.

the number is how many equal signs should be stacked on each other and the comma is a space.

So I just need to take your first comment and turn it 90 degrees.

Sorry if I'm making this harder to understand lol.

Well the following code works, there's probably a better way but being on the beers with the music up loud doesn't quite help ;-)

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

#include <iostream>
#include <Windows.h>
using namespace std;

void setPos(int x, int y);

int main()
{
	int map[18] = { 2, 1, 3, 1, 1, 2, 3, 5, 3, 2, 1, 0, 2, 1, 2, 4, 1, 2 };

	int rows = 5;	// set to highest available number in array!

	for (int i = 0; i < 18; i++)
	{
		for (int j = 0; j < map[i]; j++)
		{
			setPos(i, rows - j);
			cout << "=";
		}
	}

}

// function to set curson position in console
void setPos(int x, int y)
{
	COORD xyPos;
	xyPos.X = x;
	xyPos.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xyPos);
}


I will probably come up with something better tomorrow if you need it lol


EDIT: took out the IF statement that wasn't needed.
Last edited on
This worked thank you!
Please, no OS-specific strangeness.

They are not needed:
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
#include <iostream>

int main()
{
  const int Count = 6;
  const int map[Count] = { 2, 1, 3, 0, 1, 4 };

  const int Rows = // something

  for ( int down = 0; down < Rows; ++down )
    {
      for ( int across = 0; across < Count; ++across )
        {
          const int peak = map[across];
          if ( /*down and peak something*/ )
            {
              std::cout << '=';
            }
          else
            {
              std::cout << ' ';
            }
        }
      std::cout << '\n';
    }
  return 0;
}
Last edited on

If your going to criticise keskiverto, at least have the decency to post some complete working code for the guy and to prove me wrong; from where I am sitting its not going to work :)







Last edited on
Your welcome tylergarner :)
1. Windows.h does not belong to C++ Standard. It is better to use standard-compliant, portable examples on this Forum.

2. Do you propose that tylergarner cannot solve two (IMHO) rather simple /*somethings*/? He did ask "explain further", not all the way.

1. I never said Windows.h belonged to the C++ Standard. I also agree that it is better to use standard compliant and portable code and this was one of the main reasons I made it clear to tylergarner when I posted that I would look for a more improved method tomorrow (now today).

Well the following code works, there's probably a better way but being on the beers with the music up loud doesn't quite help ;-)


I will probably come up with something better tomorrow if you need it lol


2. Now your being silly, you have cleverly avoided my request. You post "this is not needed" with code that is incomplete and doesn't work even after the thread was marked as resolved - backup that claim and prove your point to me, which was the whole purpose of my last post.

Now according to your code all tylergarner needs to do is pop in a value for Rows, fill in the blank for his IF statement and everything will be ok.. i look forward to that.

Provide that, prove me wrong and then I will respectfully apologise like an adult should.


The original goal looked like a histogram plot. That does not require C++ at all. Spreadsheets (oocalc, MS Excel), plotting programs (gnuplot, Origin, SigmaPlot), statistics (R, MatLab), etc can do such plot in a heartbeat. OP wrote that he "has to use" loops and array. C++ offers std::array, std::vector and algorithms. A post on the Beginner Forum has thus two usual reasons: (1) the user wants to learn, and (2) the user does not know the alternatives. The "has to" points to the first. IMHO, focusing attention to smaller subproblems helps the learning more than handing out whole solutions.


Rows. How many rows? Probable answer is: "enough for the tallest peak". So how tall is the tallest peak? Easy, look from the static input data. However, if one does that, then one could just write out the plot as string literals (like I did in earlier comment, by hand) and it would not work if input data changes. So, do the "pop in" with algorithm. The maximum value of an array. That is a FAQ in this Forum. I would use std::max_element (its reference documentation has an example on how to), but a for loop can be used to achieve the same. Your pick.


Condition We want an expression that yields true or false appropriately. Relational operator?
Tallest peak == Rows, and should produce true on every row.
0 == peak should produce false on every row.
How about:
1
2
3
Rows relop (down + peak)
// where relop is one of
// != < <= == >= > 

Considering what we want to see, <= looks to be close enough.
Rows <= (down + peak)

Are we explicit enough now?


[Edit] Oh my: Another poster has a curiously similar homework: http://www2.cs.uh.edu/~acl/cs1410/Assignment/prog6.pdf
Last edited on

Never doubted you for a second, well done :)

My whole point of my post was down to your comment; I accept that the post wasn't perfect and that fact I made clear when I posted it. However, it is also sometimes beneficial to give example code so that they can experiment with and learn from.

I agree with a lot of your post though and as promised I apologise - actually I also apologise again because of the way I worded posts - lesson number 1, don't post or attempt to write code while intoxicated ;-)

Anyway that's not an excuse, I should know better - take care.


Topic archived. No new replies allowed.