Equilateral triangle with arrays

closed account (9G3v5Di1)
Write a program that will display an equilateral triangle with a height depending on the user. The minimum height is 1, the maximum height is 10. Use an array to display the specific character on the specific row. The array will be: { 0 := “A”, 1:= “B”, 2 := “C”, 3 := “D”, 4 := “E”, 5 := “F”, 6 := “G”, 7 := “H”, 8 := “I”, 9 := “J” }. You are limited to four (4) variables only (including the array).



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
  #include <limits>
#include <cstring>
#include <float.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <istream>
#include "_pause.h"

using namespace std;

int main() {
	int n;

	cout << "Enter height of the triangle:";
	cin >> n;

	char x[n] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};

	for(int y=0; y <= n; y++){
		for(int s=n-y; s>0; s--){
			cout << " ";
		}
		for(int x=1; x <= y; x++){
			cout << x[n] << " ";
		}
		cout << endl;
	}

	std::cin.get();
	_pause();
	return 0;
}


Theres an idea going through my mind but I don't know what/where to start. I'm so confused. I do not know how to organize my thoughts. My mind is in complete confusion.. please teach me to construct this one.

What I understand about this is you
1) need first to know the height that the user will enter
2) declare a variable that will store arrays
3) i do not know what's next
Last edited on
goldwinbryanr wrote:
I don't know what/where to start

I'm confused. Didn't you write the code you have posted?


First of all remove all the junk includes at the start. The only one you need is
#include <iostream>
and maybe
#include <string>
Then please get rid of the unneeded
1
2
	std::cin.get();
	_pause();

so that we can run it in c++ shell.


goldwinbryanr wrote:
1) need first to know the height that the user will enter

But surely you have already done this with
1
2
3
int n;
	cout << "Enter height of the triangle:";
	cin >> n;



goldwinbryanr wrote:
2) declare a variable that will store arrays

But surely you have tried to do this with
char x[n] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
Note that this is illegal in standard C++. Both modern C and modern Fortran allow you declare the array size at run time, but it is a VLA (variable-length array) and not legitimate in C++. It also wouldn't match the length of the initialiser list unless n was 10, so both C and Fortran would baulk at it. Given your specification this could simply be written
char x[10] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};


goldwinbryanr wrote:
3) i do not know what's next

That's not consistent with the code that you have given. Where did it come from?
for(int y=0; y <= n; y++){
This would give n+1 rows. You need n, so
for(int y=0; y < n; y++){


The next loop gives n-y spaces. It's OK, but takes your variable count to 5. You could actually achieve the same with
cout << string( n-y, ' ' );
so reducing the number of variables.


The next loop is supposed to write the character x[y] the number y+1 times. So fix:
- the loop index (you can't re-use x; you need another variable name)
- the number of times the loop runs;
- the array element being output here.


Then it works.


You will learn more if you write your own code. This appears to have been taken, with minor modification, from somewhere else.
closed account (9G3v5Di1)
Okay so I wrote everything there, the for loops came from my previous activity which is to create a right-angled triangle with the height at the right corner.

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
#include <string>
#include <iostream>
#include "_pause.h"

using namespace std;

int main() {
	int n;

	cout << "Enter height of the triangle:";
	cin >> n;

	char x[10] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};

	for(int y=0; y < n; y++){
		for(int s=n-y; s>0; s--){
			cout << " ";
		}
		for(int x[y]; x <= 10; y++){
			cout << x[n] << " ";
		}
		cout << endl;
	}

	return 0;
}


okay I didn't understand what you've said about the last loop.. I really am sorry.. please help
1
2
3
		for(int x[y]; x <= 10; y++){
			cout << x[n] << " ";
		}

Makes no sense.


int x[y];
Your loop variable clearly can't be called x[y]. Call it i or something.


x <= 10
You don't want it to run 10 times. Reread my post.


y++
The loop variable isn't called y either.


cout << x[n] << " ";
This would output the same thing every time (the nth element of the array x[]). You want the y element.
Last edited on
closed account (9G3v5Di1)
after experimenting.. I got it

1
2
3
for(int i=0; i <= y; i++){
			cout << x[y] << " ";
		}


so does this mean that the index here

cout << x[y] << " ";
is the counting number?

Another question, for the first loops, does the variable increments before proceeding to the next loop? Or does it increment after the 1st iteration?
Last edited on
No, i is your counting number for this inner loop.

You shouldn't have to "experiment": think it through. Overall, you might want to consider renaming some variables to reflect what they actually stand for.

[Edit for extra question]
The variable increments at the end of each loop. The structure of the for-loop is roughly
for ( initialisation; decision (to run the loop or not); what is done after each loop )
Last edited on
closed account (9G3v5Di1)
okay so I really don't get it at all...

x[y], why y in the index?


Enter a number: 5

     A
    B B
   C C C
  D D D D
 E E E E E 


but when x[i],


Enter a number: 5

     A
    A B
   A B C
  A B C D
 A B C D E


first of all, if y is the index then the value of y as declared will be as it is in the index?
second, why does x[i] differ from x[y] if both has the same starting value.. I'm so confused
Last edited on
The index y happens to be the row number (counting from 0).
The index i is how many letters you are across that row (again counting from 0).

As I understand it, you want the whole row to have the same character. This is x[y] in your notation. Therefore, for all the i values (i.e. the whole of a row) if you want to output the same character then this will be x[y].

If you chose to output x[i], then as you went across a row (i.e. changed the value of i) you would get the different characters x[0]='A', x[1]='B', x[2] = 'C' etc. Which is what you see.

The order of your indexing matters, because you are obliged (by the screen-writing facilities) to go downward, one whole row at a time. Thus, your outer loop gives the row, and your inner loop gives the distance across it.
Last edited on
closed account (9G3v5Di1)
okay in short the y is for the row, and the i is for the column. thank you so much!
closed account (9G3v5Di1)
why can't I create a new topic?
Topic archived. No new replies allowed.