Hi this is my first post and I need help on my first program on making some shapes in c++ using for statement and using *. Its an simple for statement program but I got nothing. Im trying to making an acute triangle using *. I got the triangle down but its coming out as an right triangle and I dont really know how to make it as as acute. This is what i have now

_______________________________________________________________
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    for (int i=0; i < 10; i++)
    {
        for (int j=0; j < i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
}
_______________________________________________________________

thanks inadvance

I assume by acute you mean equilateral. It's terribly difficult to get all of the acute trianglesto print accurately using command line output. Instead of one nested loop, try two. The first nested loop should handle spaces and the second should handle asterisks:

for i = 0 to N do
  for j = 0 to spaces do
    print ' '
  for j = 0 to stars do
    print ' '
  print '\n'

Then you only need to work out the initial values for N, spaces, and stars, as well as how to modify spaces and stars to get the output like you want it.

for i = 0 to N do
  for j = 0 to spaces do
    print ' '
  for j = 0 to stars do
    print ' '
  print '\n'

Thanks for replying narue. Im just starting to learn about c++. Im trying to try to understand what you reply. I just went out and bought myself a book. Man, I have to say the book is thick. So are you saying that I should add this after the code I wrote or should i rewrite it. I dont understand the to part for example (for i = 0 to N do) Im going to have to declare the N, spaces, stars, right?

Before saying anything else, I want to be sure that you know the code I posted is not C++. ;) It's pseudocode, meant to express an algorithmic concept and be easily translatable into any imperative programming language.

>Im going to have to declare the N, spaces, stars, right?
Yep. N is the height of the triange, so if N is 5, the triangle should look like this:

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

spaces is the number of whitespace characters printed before the line of asterisks, and stars is the number of asterisks to print. In the above example, spaces starts at 4 and counts down, stars starts at 1 and counts up by two.

for i = 0 to N do    means  for (i = N)
{
  for j = 0 to spaces do    means for (j = spaces)
     {
    print ' ' means  cout << ' '; 
     }
  for j = 0 to stars do means  for(j = stars)
    {
    print ' ' means  cout << "*";
    }
    print '\n' means  cout << "\n";

Narue Im sorry if Im annoying you but can you give me a little more hint. I think (think) I manage to translate those pseudocode. Sorry if it looks like a mess.

Here's the pseudocode translated to C++. All you need to do is figure out what initial values to give spaces and fills, and what changes to make with each iteration. That's most of the problem anyway.

int n = 5;
int spaces = // You need to figure out
int fills = // You need to figure out

for (int i = 0; i < n; i++) {
  for (int j = 0; j < spaces; j++)
    cout<<' ';
  for (int j = 0; j < fills; j++)
    cout<<'*';
  spaces = // You need to figure out
  fills = // You need to figure out
}

Hey Narue
I still could do it the way you been trying to help me with but I did mange to get the pyramid to form. I even got it to for a diamond. I wan to thank you what you did up til now

#include <iostream>
#include <iomanip>
using namespace std;


int spaces = 40;
int size;
int main()
{
cout << "How many long would you like (odd number please)?" << endl;
cin >> size;

for (int i = 1; i < size; i=i+2)
  {
      cout << setw(spaces);
	for (int j = 0 ; j < i; j++)
	   cout << "*";
	      cout << endl;
		--spaces;
   }	
}

this is my diamond one

#include <iostream>
#include <iomanip>
using namespace std;


int spaces = 40;

int main()
{

for (int i = 1; i < 20; i=i+2)
 {
   cout << setw(spaces);
    for (int j = 0 ; j < i; j++)
     cout << "*";
       cout << endl;
	--spaces;
	}	
	  for (int i = 1; i < 20; i=i+2)
	{
		cout << setw(spaces+2);
		 for (int j = 18 ; j > i; j--)
		  cout << "*";
		   cout << endl;
		    ++spaces;
        }
}

Well, okay. If you wanted to do it the fun way, why didn't you say so? Nested loops are so 1995:

#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  int n = 5;

  for ( int i = 0; i < n; i++ ) {
    cout<< setfill ( ' ' ) << setw ( n - i - 1 ) <<"";
    cout<< setfill ( '*' ) << setw ( i * 2 + 1 ) <<""<<endl;
  }
}

Wow. That seems so simple yet it so complicated to me. In 1995, i dont think i even touch a computer back then.

So many of you have solved the problem of printing a triangle so elegantly and simply .... here's the way i would do it ......

#include<iostream.h>
#include<stdio.h>
//This program will attemp to print a triangle of asterisks
main()
{
void space(int);
void star(int);
int n,i,j,k,x,y,spaces,stars;
cout<<"Input the number of rows of * the tringle should have"<<endl;
cin>>n;
cout<<"This is the triangle with n rows"<<endl;
spaces=40;
for(i=0;i<n;i++)
{
//spaces=-(i-n);
space(spaces);
spaces=spaces-1;
stars=i+1;
star(stars);
cout<<endl;
}
}
void space(int x)
{
int j;
for(j=0;j<x;j++){
cout<<" ";}
}


void star(int y)
{
int k;
for(k=0;k<y;k++){
cout<<"*"<<" ";}
}

As far as the diamond goes ..... this is how i would have attempted it .... its not the most efficient way .... but I have a way of comlicating even the simplest of things ... :D

here it is !!!

#include<iostream.h>
#include<stdio.h>
//This program will attemp to print a diamond of asterisks
main()
{
void space(int);
void star(int);
void revspace(int);
void revstar(int);
int n,i,j,k,x,y,spaces,stars;
cout<<"Input the number of rows of * the tringle should have"<<endl;
cin>>n;
cout<<"This is the triangle with n rows"<<endl;
spaces=40;
for(i=0;i<n;i++)
{
//spaces=-(i-n);
space(spaces);
spaces=spaces-1;
stars=i+1;
star(stars);
cout<<endl;
}


spaces=spaces+2;
for(i=n-1;i>0;i--)
{
//spaces=-(i-n);
revspace(spaces);
spaces=spaces + 1;
stars=i;
revstar(stars);
cout<<endl;
}


}
//Functions for top half of the diamond
void space(int x)
{
int j;
for(j=0;j<x;j++){
cout<<" ";}
}


void star(int y)
{
int k;
for(k=0;k<y;k++){
cout<<"*"<<" ";}
}


//Functions for bottom half of the diamond
void revspace(int x)
{
int j;
for(j=0;j<x;j++){
cout<<" ";}
}



void revstar(int y)
{
int k;
for(k=0;k<y;k++){
cout<<"*"<<" ";}
}

>sorry i forgot to put comments in my code ..... guess i was of no help at all, huh ?
No, you forgot to put code tags around your code. Comments aren't needed if the code is self documenting, and code with no indention is far from self documenting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.