• 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

    • Yeah, sure. Everyone just convert their PC with all their data and apps to Linux today. Most people do not know what Linux is, wouldn't know which of the dozen or 2 versions would be the best fit for them nor have any idea how to install it. I have no problem with Linux being the underlying operating system for commercial devices sold and maintained by organizations. Microsoft needs to get their act together, listen to their general user's complaints and release a productive, user friendly W12. All the good things under the hood but an easy to use interface.
    • Bitdefender Antivirus Free 27.0.52.261 by Razvan Serea Bitdefender Antivirus Free is a free antivirus software especially designed to protect your Windows PC. Quick to install and light on computer resources, it is good for gaming, image and video editing, and resource-intensive applications. Once you have installed Bitdefender Antivirus Free, your device is protected against all kinds of threats (such as malware, spyware, ransomware, exploits, botnets and trojans) and internet threats (such as hackers, phishing and spam). The app uses the Photon technology to enhance the speed and performance of the threat scanning process. It works by learning the usage patterns of your system apps to know what and when to scan, thus minimizing the impact on system performance. Many free antivirus solutions are as intrusive and resource-heavy as the viruses they’re supposed to stop. Only Bitdefender Antivirus Free is designed from the ground up to keep you safe without disrupting your workflow and focus. Free antivirus protection that stops even the fastest-evolving attacks Runs silently in the background and stays out of your way Impossibly light on CPU (will not slow down your computer) Live customer support included (unlike other free antivirus software) Bitdefender Antivirus 27.0.52.261 changelog: Improved the uninstall process for a smoother experience Removed SecurePass cards, recommendations, and quick actions from the interface Fixed errors related to Bitdefender processes Operating systems: Windows 7 with Service Pack 1, Windows 8.1, Windows 10 and Windows 11 Download: Bitdefender Antivirus Free 27.0.52.261 | 18.0 MB (Freeware) View: Bitdefender Antivirus Free Homepage | Release Notes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yeah, I'e said it myself couple of comments prior. Doesn't solve a problem that I can't play my Xbox games on a these PCs.
    • I've seen people/companies/governments try this move several times before and they still end up going back to Windows. Maybe this time they will have more success. Will be interesting.
  • Recent Achievements

    • First Post
      NeoToad777 earned a badge
      First Post
    • Week One Done
      JoeV earned a badge
      Week One Done
    • One Month Later
      VAT Services in UAE earned a badge
      One Month Later
    • First Post
      LsDmT earned a badge
      First Post
    • Week One Done
      evershinefacilityservice earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      566
    2. 2
      ATLien_0
      249
    3. 3
      +Edouard
      162
    4. 4
      +FloatingFatMan
      151
    5. 5
      Michael Scrip
      113
  • Tell a friend

    Love Neowin? Tell a friend!