• 1

C++ Printed arrow program


Question

Hi Guys,

 

I need some help. I'm new to C++ and I've been set a tiny project to help me understand and implement nested for loops.

All this project is supposed to do is print out an arrow using the values given for the Arrow Length, Arrow Head Width, Arrow Tail Width and the character to output in.

 

I have managed to get the arrow head going, mostly with luck...

Now it's just down to the tail. I cannot for the life of me figure out how to centre the tail. I know I'm missing the for loop for the spaces to push it in, but that's the part which I'm struggling with. Probably very basic, but my head is blank right now.

 

This solution will eventually take user input to change the arrow dimensions, so it needs to be relative.

#include <iostream>
using namespace std;

void main()
{
	int arrowHeadWidth = 7;
	int arrowLength = 9;
	int arrowTailWidth = 3;

	char outputChar = '*';

	//Arrow Head
	//loop to keep running until the height has been met
	for (int height = 1; height <= arrowHeadWidth; height += 2)
	{
		// loop to output the spaces
		for (int spaces = arrowHeadWidth; spaces > height; spaces -= 2)
		{
			cout << " ";
		}
		// loop to output the character for the body of the arrow
		for (int stars = 0; stars < height; stars++)
		{
			cout << outputChar;
		}
		cout << endl;
	}

	int arrowTailLength = arrowLength - (arrowHeadWidth / 2) - 1;

	//Arrow Tail
	for (int r = 0; r < arrowTailLength; r++)
	{
		for (int c = 0; c < arrowTailWidth; c++)
		{
			cout << 'outputChar';
		}
		cout << endl;
	}
}
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I cannot for the life of me figure out how to centre the tail. I know I'm missing the for loop for the spaces to push it in, but that's the part which I'm struggling with. Probably very basic, but my head is blank right now.

What is it about that for loop adding spaces that's bugging you? You only need to figure out the upper bound, i.e. the number of spaces to output in order to center the arrow. It's a very simple mathematical problem. You can figure it out on paper by drawing a grid and doing some examples with arrows of different widths.

 

Figuring things out "by luck" won't get you very far in programming. I know you're still struggling with the syntax but try to separate the logic of the problem - which you can figure out on paper regardless of the language - and the actual implementation including all syntactic details.

  • Like 2
Link to comment
Share on other sites

  • 0

All I needed was a piece of paper and a pencil. It turns out it really was easy.

 

Sometimes it's easier to properly visualise the problem. Thanks Andre

  • Like 2
Link to comment
Share on other sites

  • 0

It's messy, but it works provided HEAD_WIDTH and SHAFT_WIDTH have opposite parities. I'm sure there's probably a more structured way of doing it of course. Using something like NCURSES or platform specific terminal functions might yield a better solution.

#include <stdio.h>

const int HEAD_WIDTH 	= 10;
const int SHAFT_LENGTH	= 9;
const int SHAFT_WIDTH	= 3;
const int TAIL_WIDTH	= 6;
const int PADDING	= 4; 

#define SYMBOL		'*'

void
align ( int x ) {

	while ( x-- )
		putchar ( ' ' );	
}

void
draw_head ( int width ) {
	
	int i, j;
	int x = width + PADDING;

	for ( i = 1; i < width; i += 2 ) {
			
		align ( x - i / 2 );
	
		for ( j = 0; j < i; j++ )
			putchar ( SYMBOL );
		
		putchar ( '\n' );
	}
}

void
draw_shaft ( int length, int width ) {

	int i;
	int x = ( HEAD_WIDTH + PADDING ) - width / 2;

	while ( length-- ) {

		align ( x );
		
		for ( i = 0; i < width; i++ ) 
			putchar ( SYMBOL );		
		
		putchar ( '\n' );
	}
}

void
draw_tail ( int width ) {

	int i;
	int x = ( HEAD_WIDTH + PADDING ) - SHAFT_WIDTH / 2;
	
	for ( i = 1; i < width; i += 2 ) {
	
		align ( x - i );
		putchar ( SYMBOL );

		if ( 1 == i ) {
			align ( SHAFT_WIDTH );
		} else { 
			align ( SHAFT_WIDTH + ( ( i - 1 ) * 2 ) );	
		}

		putchar ( SYMBOL );
		putchar ( '\n' );
	}
}

void
main ( int argc, char **argv ) {

	draw_head  ( HEAD_WIDTH );
	draw_shaft ( SHAFT_LENGTH, SHAFT_WIDTH ); 
	draw_tail  ( TAIL_WIDTH );
}
$  ./arrow 
              *
             ***
            *****
           *******
          *********
             ***
             ***
             ***
             ***
             ***
             ***
             ***
             ***
             ***
            *   *
          *       *
        *           *
Link to comment
Share on other sites

This topic is now closed to further replies.