• 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

    • Oh, what if the AI skips over some "editor's" personally cherished lie in an article? The Horror!
    • eagerly awaiting a version that runs on android tablets
    • I grew up with the Beach Boys. Even saw them in concert in the 70's. Brian suffered from mental issues all his life. May you find peace wherever you are.
    • Wikipedia suffers backlash from human editors over AI summaries, prompting feature pause by David Uzondu Wikipedia editors have pushed back against plans from the Wikimedia Foundation to test AI-generated article summaries, powered by Aya, the open-weight AI model from Cohere. The non-profit has now paused the project. The decision came after a swift and overwhelmingly negative reaction from its community. As first reported by 404Media, the plan involved a two-week, opt-in trial on the mobile version of Wikipedia. But the volunteer editors who build the encyclopedia met the idea with immediate and fierce opposition. The project's discussion page became a torrent of rejection. It included simple comments like "Yuck" and blunt declarations like "strongest possible oppose" and "Absolutely not." One editor argued that a test would cause "immediate and irreversible harm to our readers and to our reputation as a decently trustworthy and serious source." They noted that Wikipedia has built its name on being sober and reliable, not flashy. Another feared it would destroy the site's collaborative model. They argued that while the "collective mass" of human editors "evens out into a beautiful corpus," the AI would install "one singular editor with known reliability and NPOV [neutral point-of-view] issues" at the very top of an article. That same editor also noted the following: For context, this is what AI-generated summaries on the platform was supposed to look like: Image: 404Media It is not hard to see why they are so protective. The editors' fears are grounded in recent and very public failures of AI features from tech giants. For example, Google's AI overviews recently hit 1.5 billion monthly users. The feature became a laughingstock for telling people to put glue on their pizza and that a dog had played in the NBA. This is the kind of humiliating error Wikipedia's community is desperate to avoid, as it would undermine two-plus decades of careful work. We also saw the potential for reputational damage back in January. That was when Apple's AI feature falsely generated a notification claiming that Luigi Mangione had died by suicide. The man was actually alive and in custody. On the site's technical discussion page earlier today, Marshall Miller (MMiller), a Senior Director at the Wikimedia Foundation, posted an update acknowledging the feedback. He admitted, "It's clear we could have done a better job introducing this idea," and confirmed the experiment was paused. The Foundation says the goal was to explore accessibility for different readers. While this specific test is off the table, the organization still wants to use new technologies. Miller ended with a promise: "We do not have any plans for bringing a summary feature to the wikis without editor involvement." A WMF spokesperson also told 404Media that though the feature has been paused, the foundation is still interested in AI-generated summaries. The spokesperson insisted the goal was to eventually build moderation systems where "humans remain central" and called this kind of backlash feedback part of what makes Wikipedia a "truly collaborative platform."
    • I see, yeah that makes sense. I have been in situations where I barely did not crush badly on the road due to other driver starting to change lanes into another car - freaked out last second and avoided it by crashing into the side of the bridge instead. i got away because I quickly changed lanes 2 times in a couple of second and unlike that idiot I did not lose control big part of this was my car was good 😊 (audi a7) vs the old van the crashed driver was driving would AI be able to react and quickly change lanes twice both time barely avoiding collision … I don’t know my car systems pumped the breaks and tried to warn me with a beep and vibration but if I slammed the breaks the car behind me would hit me then again I have BMW driver training and a good car - so I have no idea how robot taxi would react i am not sure extreme fast lane changes would be programmed in - it is dangerous as hell unless you are FULLY aware, and have done it before but it is a general risk to do it especially in the conditions with bad weather and when you are not driving a sports car with 4 wheel drive and very good control
  • Recent Achievements

    • Collaborator
      CHUNWEI earned a badge
      Collaborator
    • Apprentice
      Cole Multipass went up a rank
      Apprentice
    • Posting Machine
      David Uzondu earned a badge
      Posting Machine
    • One Month Later
      Stokenking earned a badge
      One Month Later
    • One Month Later
      Kevin Jones earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      537
    2. 2
      ATLien_0
      266
    3. 3
      +Edouard
      193
    4. 4
      +FloatingFatMan
      181
    5. 5
      snowy owl
      135
  • Tell a friend

    Love Neowin? Tell a friend!