Coding Assignment, stuck!

Hey all, trouble with a school assignment, i need to create an arrow in the console using user inputs and nested loops. My idea is that after every column the draw char increase by two until it reaches headWidth going out from the center.

1
2
3
4
  *
 ***
*****
+2 stars until it reaches headWidth starting from center

Bit unsure about how to code this though.. this is what ive got so far
1
2
3
4
5
6
7
8
9
10
11
12
13

	for (int r = 0; r < length; r++)
	{
		for (int c = 0; c < headWidth; c++)
		{
			if (r == headWidth / 2) && (r == //UNSURE*)
				cout << drawChar;
			else
				cout << ' ';
		}
		cout << endl;
	}
	


Your help would be much appreciated!
Last edited on
An easy way to solve a problem like this is to write out a few examples.

Example 1: arrow with 3 rows (I'll number then 0-2):
Row 0: 2 spaces, 1 star
Row 1: 1 space, 3 stars
Row 2: 0 spaces: 5 stars.

Example 2: arrow with 4 rows:
Row 0: 3 spaces, 1 star
Row 1: 2 spaces, 3 stars
Row 2: 1 space, 5 stars
Row 3: 0 spaces, 7 stars

Now that you see the pattern, you can make a formula for it:

Arrow with N rows:
Row 0: N-1 spaces, 1 star
Row 1: N-2 spaces, 3 stars
...
Row N-1: 0 spaces: 2*N-1 stars

And even more generally:
Arrow with N rows:
Row R: N-R-1 spaces, 2R+1 stars

You should be able to translate this directly into code.
Lifesaver. Going to try write this up and see how it goes. Thanks very much!
Ok this is where i'm at
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<iostream>
#include<conio.h>
#include<limits>

using namespace std;

// Global Variables
int length, headWidth, tailWidth;
char drawChar;
int repeatMeasurements;


int printArrow()
{
	// Variables limited to this function
	int headLength, tailLength;
	
	headLength = length / 2 + 1;
	tailLength = length - headLength;
	

	//This will create the ArrowHead
// This will create the top section of the arrow , 1 Draw char top row followed by +2 each following row
	for (int height = 1; height <= headWidth; height += 2) 
	{
		//Going from bottom up minusing 2 spaces as it goes
		for (int spaces = headWidth; spaces > height; spaces -= 2) 
		{
			//output spaces
			cout << " "; 
		}
		for (int spaces = 0; spaces < height; spaces++)
		{
			cout << drawChar; //Prints Symbol
		}
		cout << endl;
	}

	// This will create the tail of the arrow

	//Arrow Tail
		for (int r = 0; r < tailLength; r++) 
		{
		for (int c = 0; c < tailWidth; c++)
		{
			// Prints spaces untill the columns is equal to tailwidth
			if (r > headWidth / 2 + 1 || c >= length / tailLength || c <= headLength)
			{
				cout << drawChar; //print symbol
			}
			else
			{
				cout << ' '; //Print space
			}

		}
		cout << endl;
		}
//function must return value
	return 0;
}

int RepeatArrow()
{
	return repeatMeasurements;
}


//Starting Menu Function
int userchoice()
{
	cout << "Please choose an option:\n";
	cout << "\t1. Create an arrrow: \n ";
	cout << "\t2. Repeat the last measurements? \n ";
	cout << "\t3. Exit\n";

	int input;
	cin >> input;
	if (input == 1 || input == 2 || input == 3)
	{
		cout << "Please continue.\n";
	}
	else
	{
		cin.clear();
		cout << "Error: Please select another option\n";
	}

	return input;
}

int main()
{

	bool KeepRunning = true;
	while (KeepRunning)
	{
		//Runs the Menu Function
		int selection = userchoice();
		switch (selection)
		{
		case 1:


			cout << "Please enter the height:\n";

			cin >> length;

			cout << "Please enter the headWidth:\n";

			cin >> headWidth;

			cout << "Please enter the tailWidth:\n";

			cin >> tailWidth;

			cout << "Please enter the character you would like to render the arrow with:\n";

			cin >> drawChar;

			printArrow();

			break;
		case 2:
			RepeatArrow();
			break;
		case 3:
			KeepRunning = false;
			break;
		}
	}
	return 0;
}


Seems to be working except for one thing, the tail is not correct.. any ideas whats wrong with it? Just prints it to the left . So the arrow is still messed up, also i need to give an option for repeating the inputs, not sure how to do that either!

Also im being graded on my IPO and psuedo Code..

Gregory Cornes
Student Number : 14036637



Task A Arrowhead
Input Processing Output
Length /2+1 Headlength
Length -headLength Taillength


Psuedo Code
1. Ask user for inputs (headWidth, tailWidth, length, drawchar)
2. Read user inputs
3. Calculate Head Length = length / 2 + 1
4. Calculate Tail Length = length - headLength
5. Print Arrowhead
Output draw char for long as height less than or equal to headWidth starting with 1 increasing by 2.
Output Spaces from bottom of arrow head subtracting - 2 each row
6. Print ArrowTail
Create Rows = taillength
Create Colums = tailWidth


Task B Menu Systems and Repeats
Psuedo Code
1. Ask user for selection
2. If Option 1, Go to Task A
3. If Option 2, Repeat Inputs from task A
4. If Option 3, Exit the Program





Last edited on
Topic archived. No new replies allowed.