Forming an inverted triangle with asterisks

nested for loop prt2 asterisk! :(

Page 1 of 1

10 Replies - 29534 Views - Last Post: 25 June 2008 - 08:18 PM Rate Topic: -----

#1 jencalma   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

Forming an inverted triangle with asterisks

Posted 24 June 2008 - 03:55 AM

the output should be::

* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
*

n is where you input the number
for (a=2;a<=a+1;a++,n--)
{ for (b=n;b>0;b--?)
{gotoxy (b,a); cout<<"*";
}
}
getch();
}

* i have a little problem i don't know how to fix this and i really have a problem regarding using the nested for loop.. i know that you can help thank you :)
* the asterisk are not aligned by one another they descending forming a triangle upside down

This post has been edited by jayman9: 24 June 2008 - 08:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Forming an inverted triangle with asterisks

#2 AndyH1963   User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 04:21 AM

How about this:

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
   for (int n = 10; n >= 0; n-=2)
  {
	 if ( n == 0 ) n = 1;
	 cout << string(n, '*') << endl;
   }
}


Was This Post Helpful? 0
  • +
  • -

#3 jencalma   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 04:48 AM

View PostAndyH1963, on 24 Jun, 2008 - 04:21 AM, said:

How about this:

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
   for (int n = 10; n >= 0; n-=2)
  {
	 if ( n == 0 ) n = 1;
	 cout << string(n, '*') << endl;
   }
}



where not yet using some string command,, how will i code this program without using some string commands? and where only on the basic of c++ that's why! thank you :) i appreciate a lot of your help

This post has been edited by jencalma: 24 June 2008 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 mensahero   User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 680
  • Joined: 26-May 08

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 04:55 AM

what triangle do you want? upside down or the normal triangle?

I've found this. Maybe you could try it.

#include<conio.h>

int main()
{

int col, row, inputs, count, a, b, c;


clrscr();


retry:
gotoxy(36,1);
printf("TRIANGLE");
gotoxy(30,3);

printf("Enter number of Rows: ");
scanf("%d",&inputs);

if(inputs > 13){
gotoxy(10,5);
printf("Please enter A number less than 13. Press Enter to Continue..");
getch();
clrscr();
goto retry;
}

row = 5 + inputs;
count = 1;
col = 42-(row*2);

c = inputs*2;
a = 1;

do{

	for(a; a <= inputs; a++){

	gotoxy(col,row);

	for(b = 1; b < c; b++){
	   printf("  *");
	   delay (10000);
	}

	col = col + 3;
	c = c - 2;
	row--;
	}

	count++;
}while(count <= inputs);

gotoxy(21, inputs + 10);
printf("Thank you. Press Any Key To Continue....");
getch();

}



If that work then cool g00gle is my bestfriend. Thats a normal triangle by the way not the upside down one. The delay is just to show how the triangle is being made, you can thrash it if you want.

This post has been edited by mensahero: 24 June 2008 - 04:57 AM

Was This Post Helpful? 0
  • +
  • -

#5 AndyH1963   User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 05:03 AM

[/quote]

where not yet using some string command,, how will i code this program without using some string commands? and where only on the basic of c++ that's why! thank you :) i appreciate a lot of your help
[/quote]

OK, if you are not using string yet:

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
   for (int n = 10; n >= 0; n-=2)
   {
	  if (n == 0) n = 1;
		
	  for (int m = 0; m < n; ++m)
		 cout << '*';

	  cout << endl;
   }
}


Was This Post Helpful? 0
  • +
  • -

#6 jencalma   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 07:44 AM

View PostAndyH1963, on 24 Jun, 2008 - 05:03 AM, said:



where not yet using some string command,, how will i code this program without using some string commands? and where only on the basic of c++ that's why! thank you :) i appreciate a lot of your help
[/quote]

OK, if you are not using string yet:

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
   for (int n = 10; n >= 0; n-=2)
   {
	  if (n == 0) n = 1;
		
	  for (int m = 0; m < n; ++m)
		 cout << '*';

	  cout << endl;
   }
}


[/quote]


what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. :) thank you so mch..

This post has been edited by jencalma: 24 June 2008 - 07:47 AM

Was This Post Helpful? 0
  • +
  • -

#7 AndyH1963   User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 08:09 AM

View Postjencalma, on 24 Jun, 2008 - 07:44 AM, said:

what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. :) thank you so mch..


If you want to center the triangle:

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
   const int START = 12;

   for (int n = START; n > 0; n-=2)
   {		
	  /* output leading space characters, to 
		  center the triangle						  */
	  for (int x = 0; x < (START - n/2); ++x)
		 cout << ' ';
	  /* output the asterisks for this line	   */
	  for (int m = 0; m < n; ++m)
		 cout << '*';

	  cout << endl;
   }
}


Was This Post Helpful? 0
  • +
  • -

#8 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 08:28 AM

Topic renamed to be more descriptive of the problem.
Was This Post Helpful? 0
  • +
  • -

#9 jencalma   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

Re: Forming an inverted triangle with asterisks

Posted 24 June 2008 - 02:49 PM

View PostAndyH1963, on 24 Jun, 2008 - 08:09 AM, said:

View Postjencalma, on 24 Jun, 2008 - 07:44 AM, said:

what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. :) thank you so mch..


If you want to center the triangle:

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
   const int START = 12;

   for (int n = START; n > 0; n-=2)
   {		
	  /* output leading space characters, to 
		  center the triangle						  */
	  for (int x = 0; x < (START - n/2); ++x)
		 cout << ' ';
	  /* output the asterisks for this line	   */
	  for (int m = 0; m < n; ++m)
		 cout << '*';

	  cout << endl;
   }
}




the program needs you to press enter before the asterisk will show. what i want is that when i run the program the star will appear already on the screen. thank you for the help. :)
Was This Post Helpful? 0
  • +
  • -

#10 AndyH1963   User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: Forming an inverted triangle with asterisks

Posted 25 June 2008 - 02:41 AM

View Postjencalma, on 24 Jun, 2008 - 09:49 PM, said:

View PostAndyH1963, on 24 Jun, 2008 - 08:09 AM, said:

View Postjencalma, on 24 Jun, 2008 - 07:44 AM, said:

what i mean is that the triangle should not be aligned on the right side,, it has to descending..is that possible that the triangle are not aligned on neither of the side? Use nested for loops (a total of four (4) for loops). but thank you for your help,, again sorry for the disturbance.. :) thank you so mch..


If you want to center the triangle:

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
   const int START = 12;

   for (int n = START; n > 0; n-=2)
   {		
	  /* output leading space characters, to 
		  center the triangle						  */
	  for (int x = 0; x < (START - n/2); ++x)
		 cout << ' ';
	  /* output the asterisks for this line	   */
	  for (int m = 0; m < n; ++m)
		 cout << '*';

	  cout << endl;
   }
}




the program needs you to press enter before the asterisk will show. what i want is that when i run the program the star will appear already on the screen. thank you for the help. :)


I don't understand your question? If you need the program to do somthing, then you need to write the code ... Have a go and if you have a problem post the description of the problem and the code to go with it.
Was This Post Helpful? 0
  • +
  • -

#11 em0napz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 08

Re: Forming an inverted triangle with asterisks

Posted 25 June 2008 - 08:18 PM

Try this code:

#include <iostream.h>
#include <conio.h>

void  main(void)
{
	int n;
	char answer;

	clrscr();
  do {
	do {
	cout<<"Enter an integer--> ";
	cin>>n;
	 }while(n<=1);

	for (int j=1;j<=n;j++)
	{
		for (int i=n;i>=j;i--)
		 {
	   	cout<<'*';
		 }
	   cout<<endl;
	 }
	 cout<<"\nProcess Another? Y/N ";
	 cin>>answer;
	 clrscr();

	}while (answer=='y' || answer=='Y');
	getch();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1