3 Replies - 47950 Views - Last Post: 04 April 2008 - 07:44 AM Rate Topic: -----

#1 r12345t  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-October 07

Printing a triangle of stars

Posted 12 October 2007 - 10:55 PM

View Postjayman9, on 19 Sep, 2007 - 11:13 PM, said:

Post the code that you have completed in your attempt to fulfill your assignment. We will not do your work for you.


PLEASE HELP!! same type of question programmed in C++.
I want to make a triangle of stars when the user inputs a number. that number inputted should be the length of the base and the height.
For example if user inputs 4:
*
**
***
****

my program when compiled (user enters 4) looks like:

*
**

1/2 of the rows it should be!! i stared at it for hours and just cant figure it out. help would be greatly appreciated.

my code is:
the restrictions i set on the input must be: positive even value between 0 and 21
__________________________________

#include <iostream>
using namespace std;

int main ()
{
	
	int input =0;
	do
	{
		cout<< "Input a number of rows on your triangle"<<endl;
		cout<< "Enter -1 to exit"<<endl;
		cin>>input;
		
	}while(input<0 || input>21 || input%2 != 0);
	
	int column=1;
	int row = 1;
	
	for (row = 1; row<=input; row++)
	{
		for (column= 1; column<=input; column++)
		{
			if(column<=row)
			cout<< "*";
			else
			cout<< " ";
			column++;
			
		}
		column=1;
		row++;
		cout<<endl;
	}
	   
system ("pause");

return 0;

}



i have a feeling its something small but i cant figure it out. please help as quick as possible :) thanks in advance

1) Always use code blocks
2) Always post in the correct forum!

I'm moving this to the C and C++ Forum

This post has been edited by PsychoCoder: 13 October 2007 - 05:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Printing a triangle of stars

#2 csmanoj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 06-August 07

Re: Printing a triangle of stars

Posted 13 October 2007 - 07:52 AM

You're already incrementing the row variable in the 'for' statement. Then you are again incrementing it inside the loop. So the row variable is incremented twice every iteration. That's why you get only half the number of lines. Remove one of the iterations. Same is the case with columns.

This post has been edited by csmanoj: 13 October 2007 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 GDS  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 04-April 08

Re: Printing a triangle of stars

Posted 04 April 2008 - 07:35 AM

Hi
i did some sample code to print the stars in diffrent ways in jave
may be it will be use full check this out.

Thanks
GDS

/**
 * 
 */

/**
 * @author Sashidharrao.GDS
 * 
 */
public class Traingles {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n = 10;
		Traingles obj = new Traingles();
		obj.printTriangleOfStars(n);
		obj.printTriangleOfOddNumberStars(n);
		obj.printTriangleOfCenteredOddNumberStars(n);
		obj.printTriangleOfCenteredOddNumberStarsAndReverse(n);
	}

	public void printTriangleOfStars(int n) {

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= i; j++)
				System.out.print("*");
			System.out.print("\n");
		}
	}

	public void printTriangleOfOddNumberStars(int n) {

		for (int i = 1, j = 0; i <= n; i++, j++) {
			for (int k = 1; k <= (i + j); k++)
				System.out.print("*");
			System.out.print("\n");
		}
	}

	public void printTriangleOfCenteredOddNumberStars(int rowsize) {

		int spaces = rowsize / 2, currentsize = 1, counter, count;
		for (counter = 0; counter != (rowsize + 1) / 2; counter++) {
			for (count = spaces; count != 0; count--)
				System.out.print(" ");
			spaces--;
			for (count = currentsize; count != 0; count--)
				System.out.print("*");
			currentsize += 2;
			System.out.print("\n");
		}
	}

	void printTriangleOfCenteredOddNumberStarsAndReverse(int rowsize) {
		int spaces = rowsize / 2, currentsize = 1, counter, count;
		for (counter = 0; counter != (rowsize + 1) / 2; counter++) {
			for (count = spaces; count != 0; count--)
				System.out.print(" ");
			spaces--;
			for (count = currentsize; count != 0; count--)
				System.out.print("*");
			currentsize += 2;
			System.out.print("\n");
		}
		currentsize = rowsize - 1;
		spaces += 2;
		for (counter = rowsize / 2; counter != 0; counter--) {
			for (count = 0; count != spaces; count++)
				System.out.print(" ");
			spaces++;
			for (count = currentsize; count != 0; count--)
				System.out.print("*");
			currentsize -= 2;
			System.out.print("\n");
		}
	}

}


*
**
***
****
*****
******
*******
********
*********
**********
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
.....*
....***
...*****
..*******
.*********
.....*
....***
...*****
..*******
.*********
..*********
...*******
....*****
.....***
......*


*mod edit: added code tags: :code:
*mod edit: formatted your triangles.
Was This Post Helpful? 1
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Printing a triangle of stars

Posted 04 April 2008 - 07:44 AM

This topic is from October....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1