1,105,340 Community Members

C++ triangle using nested for loops

Member Avatar
infiniteloop56
Newbie Poster
10 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
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.

Member Avatar
infiniteloop56
Newbie Poster
10 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
bigwhiteegg
Light Poster
41 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
infiniteloop56
Newbie Poster
10 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

Member Avatar
infiniteloop56
Newbie Poster
10 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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)
Member Avatar
bigwhiteegg
Light Poster
41 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Arbus
Practically a Master Poster
615 posts since Dec 2010
Reputation Points: 25 [?]
Q&As Helped to Solve: 31 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
goldzero
Newbie Poster
5 posts since Sep 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
-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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: