1.11M Members

C++ triangle using nested for loops

 
0
 

Hey everyone,

I am new to this site, basically my problem is this: The user inputs an integer, and then the user inputs a character, the program is supposed to make a triangle out of the character, with the max width of the user inputed integer.

for example;

user inputs integer 5
user inputs character *

output:
*
**
***
****
*****
****
***
**
*

the following code that I have written only takes me about half way there and gets me to:
*
**
***
****
*****

#include "StdAfx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
     int num;
	 int rows;
	 char x;
     do
	 {
		 cout << "Enter an integer" << endl;
		 cin >> num;
		 cout << "Enter symbol" << endl;
		 cin >> x;
	 
		for(rows=0;rows<num;rows++)
		 {
		 for (int axis=0; axis<=rows;axis++)
		 {
            cout<<x;
            }
            for (int len=num;len>rows+1;len--)
            cout<<" ";
            cout<<endl;
            }
            cin.ignore();
            cin.ignore();
            } while(cin);
	return 0;
}

Any help/response is much appreciated, thanks!

 
0
 

the following code that I have written only takes me about half way there

Excellent. Now do the same thing again, but this time reverse your starting and ending points, and count down instead of up.

 
0
 

Hi, thank you for the response, I tried doing what I thought you said to do and I just ended up in an infinite loop. Could you explain in a little more detail about reversing my starting and ending points? Thank you.

 
0
 

Hi, thank you for the response, I tried doing what I thought you said to do and I just ended up in an infinite loop. Could you explain in a little more detail about reversing my starting and ending points? Thank you.

the reverse part is simple,
your original code was something like this
for(row=0;row<num;row++)
{ for(axis=0;axis<=row;axis++)
{ cout<<x; }
endl;
}
just change the row and axis to its max value and decrease through the loop
row = num, row-- through each loop, axis = num, >0, axis--
something like that will make another reverse triangle

hmm, one question, why do you need the spaces?
usually that appears when the question asking you to build a triangle like this
...x
..xxx
xxxxx
..xxx
...x
you wont need that in this one i think

 
0
 

@bigwhiteegg
-Thank you for your comment, I tried what you said but I couldn't crack it. cheers.
I am still having some difficulties,I was however, able to re-format my code. This made it a little more easier to follow.

#include "StdAfx.h"
#include <iostream>
using namespace std;

int main()
{
	int numb;
	int numb2;
	int numb3;
	int numb4;
	int input;
	char x;

	do
	{
		cout << "Enter an integer"<< endl;
		cin >> input;
		if(input == 0)
		{
			cout << "Thank you come again" << endl;
			break;
		}
		else
		{
			cout << "Enter a character" << endl;
			cin >> x;
			numb = input;
			numb2 = input;
			for(input = 0; input < numb; input++)
			{
				for(numb2 = 0; numb2 <= input; numb2++)
						cout << x;
						cout << endl;
					for(numb3 = numb-1; numb3 > 0; numb3--)
					{
						for(numb4 = numb +1; numb4 <= numb3; numb4++)
						cout << x;
						
					}
	
				}
			
		}
	}while(cin);
	return 0;
}

Again any help is appreciated, thanks!

 
0
 

@bigwhiteegg

Thank you for you help, I tried you suggestion but I was not able to do the bottom half. I only ended up in the same place. I was however able to reformat my code to make it easier to follow. Again it basically outputted the same material as before.

This is an example run with my current code:


Please enter an integer.
5
Please enter a character.
+

+
++
+++
++++
+++++
--------------------------
This is what the program is supposed to return:

please enter an integer.
5
please enter a character
+

+
++
+++
++++
+++++
++++
+++
++
+
------------

I can get the top, but not the bottom.

Reformatted code:

#include "StdAfx.h"
#include <iostream>
using namespace std;

int main()
{
	int numb;
	int numb2;
	int numb3;
	int numb4;
	int input;
	char x;

	do
	{
		cout << "Enter an integer"<< endl;
		cin >> input;
		if(input == 0)
		{
			cout << "Thank you come again" << endl;
			break;
		}
		else
		{
			cout << "Enter a character" << endl;
			cin >> x;
			numb = input;
			numb2 = input;
			for(input = 0; input < numb; input++)
			{
				for(numb2 = 0; numb2 <= input; numb2++)
						cout << x;
						cout << endl;
					for(numb3 = numb-1; numb3 > 0; numb3--)
					{
						for(numb4 = numb +1; numb4 <= numb3; numb4++)
						cout << x;
						
					}
	
				}
			
		}
	}while(cin);
	return 0;
}

I have attached to c++ file (.cpp) for anyone who would like it.
Again any help is appreciated. Thank you!

Attachments Source1.cpp (0.72KB)
 
0
 

@bigwhiteegg

Thank you for you help, I tried you suggestion but I was not able to do the bottom half. I only ended up in the same place. I was however able to reformat my code to

what you did in the beginning was correct
now it totally doesn't make sense
what you should do is have two separate nested for loop, not inside it
one build the top
one build the bottom

 
0
 

Hello infiniteloop56,
Use separate nested for loops for top and bottom patterns. Have one nested for loop for the top pattern and have separate nested for loop for the bottom pattern.
The one below is for bottom pattern...

for(c = 0; c < numb; c++)
{
  for(d = numb-1; d > c; d--)
   {
     cout << x; 
   }
}

In your code the for loop in line 36 will not function . numb4 will always be greater than numb3. Check the condition in that for loop and accordingly increment or decrement.

 
-1
 
#include <iostream>                          
#include <conio>
#include <process>
void main(){
int s;
cout<<"Enter Your Choice :"<<endl;
cout<<" Select 1 to draw a triangle"<<endl;
cout<<"Select 2 to Exit"<<endl;
cin>>s;
switch(s){
case 1:
{char c;
int num;
cout<<"Enter Integer Num:";
cin>>num;
cout<<endl;
cout<<"Enter symbol:";
cin>>c;
cout<<endl;
for(int j=0;j<=num;j++)
{for(int i=0;i<=num;i++ )
if(i<=j)
  cout<<c;

    cout<<endl;

   }
   for(int x=0;x<=num-1;x++)
   {for(int y=0;y<=num-1;y++)
   if(x<=y)
   cout<<c;
   cout<<endl;}}

  getch();  } }
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: