• 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
https://www.neowin.net/forum/topic/1236209-c-printed-arrow-program/
Share on other sites

3 answers to this question

Recommended Posts

  • 0
  Quote

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
  • 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
  • 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 
              *
             ***
            *****
           *******
          *********
             ***
             ***
             ***
             ***
             ***
             ***
             ***
             ***
             ***
            *   *
          *       *
        *           *
This topic is now closed to further replies.
  • Posts

    • TDP of this CPU is 60 watts higher than Ryzen 7600. At s usage rate of four hours per day, at a cost of twelve cents per KWh, the Intel cost $10.51 more per year to use. I don't see a real advantage to Intel here.
    • Lmao. Cries about not playing those games not installed and yet don't ever want to touch them.
    • If I want to merge folder trees that have a similar structure, Beyond Compare is always my first choice. It's not free but it's awesome. If I want to just scan a whole drive/folder and find duplicates that are taking up space, I like Czkawka.
    • Claude Code gets throttled as Anthropic rolls out fresh usage caps by David Uzondu Claude Code, the AI-in-terminal utility developed by Anthropic and launched back in February, is getting updated usage limits following weeks of user complaints about being abruptly cut off. Many developers on the "$200/month Max plan" found their access blocked after just a few requests, with no explanation from the company. In a recent thread posted to X, the AI lab explained that it has seen "unprecedented demand since launch," pointing to some of its heaviest users who were running the tool continuously in the background 24/7, with one person reportedly consuming tens of thousands of dollars in model usage on a single $200 subscription. Anthropic also claimed that some users were violating its usage policy by sharing and reselling accounts, which impacts system capacity for everyone. These factors all led the company to announce new weekly limits that will be added on top of the existing five-hour caps, effective August 28. Max plan subscribers will have the option to buy additional usage at standard API rates if they hit their cap. Here's what the new weekly limits look like: Pro Plan ($20/month): An estimated 40 to 80 hours of usage with the Sonnet 4 model. Max Plan ($100/month): An estimated 140 to 280 hours with Sonnet 4 and 15 to 35 hours with the top-tier Opus 4 model. Max Plan ($200/month): An estimated 240 to 480 hours with Sonnet 4 and 24 to 40 hours with Opus 4. Per TechCrunch, the company provided these hour-based estimates, noting that the actual numbers may vary based on the size of a project's codebase. What's interesting is how this new structure compares to the old marketing. Anthropic previously advertised its $200 Max plan as offering 20 times more usage than the Pro plan. Based on these new hourly estimates, that multiple is now closer to six. It is possible the 20x figure still applies when measured in tokens or raw compute, but, according to TechCrunch, the company has not clarified that point.
    • I don't give a rat's f### what Trumpette, the Putin puppet likes!
  • Recent Achievements

    • First Post
      Gladiattore earned a badge
      First Post
    • Reacting Well
      Gladiattore earned a badge
      Reacting Well
    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      645
    2. 2
      ATLien_0
      260
    3. 3
      Xenon
      165
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      107
  • Tell a friend

    Love Neowin? Tell a friend!