I just hit a brick wall. I'm writing a code that will ask a user the height of a pyramid an output the corresponding pyramid with * characters.
I got that part working but now I need to turn it into a half pyramid like so:

*
**
***
****
*****
assuming its a height of 5.
So far I have been unable to solve this problem, any pointers or suggestions?
This is my code so far:

#include <stdio.h>

int main()
{
	int row, column, user_input;
	int x = 0;
	printf("Enter a height\n");
	scanf("%d", &user_input);
	for(row = 1; row <= user_input; row++)
	{
		for (column = 1; column <= user_input * 2 - 1; column++)
		if (column <= user_input + x && column >= user_input - x)
			printf("*");
		else
			printf(" ");
			printf("\n");
			x++;
	}if (user_input < 1)
			printf("Height must be at least one\n");
}

Thanks in advance.

Recommended Answers

All 9 Replies

Hint, how many stars are in each row.
Row 1 ___
Row 2 ___
See any pattern you can use?

How can it be hard?

Loop I from 1 to N
    Loop J from 1 to I
        Print *
    Print \n

I know it may not be hard but perhaps I should have mentioned I'm a beginner user.
Anyway, I realize it involves slashing rows by half but I could not come up with a suitable solution to solve his problem :S

wait, wait...

your code prints a full pyramid.

3=   *
    ***
   *****

which is way more complicated than printing a half pyramid.

3= *
   **
   ***

man, how did you figure out a full pyramid but not be able to get the much simpler half pyramid?

read what walt said. that's the answer, it doesnt get any simpler.

commented: Not only was it the harder problem, it was also a hard way to solve the hard problem! +19

I know it may not be hard but perhaps I should have mentioned I'm a beginner user.

I actually appreciated that you didn't say it. It's excruciatingly obvious if you post a simple program like this on a help board. I wish more people would keep mum about their noob'ility. :icon_wink:

Anyway, I realize it involves slashing rows by half but I could not come up with a suitable solution to solve his problem :S

As jephthah said, I just gave it to you... :icon_rolleyes:

#include <stdio.h>

void main()

{

    
	int i,num,j;
	printf("enter a num");
	
	do{
    scanf("%d", &num);
	}while (num<1);

	for(i=1; i<=num; i++)
	{	
		printf("\n");
		for(j=0;j<i;j++)
		printf("*");

	}
	printf("\n");
}

hope this helps :)

commented: Stop posting bad code as answers to homework problems. You are teaching people very bad practices. -2

Please ignore yila's code. It is full of problems.

yila, please refrain from posting (supposedly) working code for people. They learn nothing so you are hurting more than helping. Especially when you use bad coding practices.

Please ignore yila's code. It is full of problems.

yila, please refrain from posting (supposedly) working code for people. They learn nothing so you are hurting more than helping. Especially when you use bad coding practices.

well it's works :)
but still, i'd like you to tell me what's wrong with it. i like to learn on my errors. having said that, i've a few remarks, more like impressions about this forum.
[rant] seriously, guys, this forum is not what it is used to be.
i posted a few times in the last month and the answers you gave me (i don't count the completely useless ones like don't use void main, it's bad etc) were mostly wrong, ESPECIALLY from veteran posters with good reputation. veteran posters here never fail to write how the noob's code is badly written blah blah but very seldom (if ever) gave actual good advice on the code itself. if they actually do, they fail to find the biggest logical errors in the code, giving useless and sometimes utterly wrong (!) repair advices /like cancel essential loops/.
i know what i talking about, because i posted simultaneously here and on yahoo!answers, and in y!a of all places (!!!) i always got GOOD and not bullying advice which actually made my code work and i learned a lot.
don't get me wrong. i don't mean the bullying and the 'i know how to write code and you're a noob' attitude if there is actually some RELEVANT info coming after it. sadly it's not the case. on y!answers, you actually happen to find some guys with knowledge, but here i am utterly disappointed. [/rant]
forgive me i my remarks were to harsh. actually i got some useful hints from newbie posters here but nothing that can be compared to y!answers, which is funny because danielweb is allegedly a professional forum. that's why i almost stopped coming here, but today i cleaned my favs and clicked on your url and happened to see this post and answered. i see it wasn't a good idea, so i promise i'll stop posting here. anyho, it's a waste of time :P

commented: "well it works" is not acceptable here. -1

well it works

that's what Toyota said about their millions of recalled cars.


but still, i'd like you to tell me what's wrong with it.

here's why your code is bad. (in your defense, i will say i have seen more bad code crammed into such a short space.)

(1) void main()

(2) printing console instructions prior to a user input without flushing the output buffer either with a \newline or a fflush(stdout) call

(3) using scanf() to get user input from the console.

all of these are basic examples of bad coding that causes no end of real-world program failures. This has been explained here and elsewhere ad nausuem, and i am not going to spoonfeed you answers to questions that have been addressed hundreds of times. If you really want to know why, look it up


seriously, guys, this forum is not what it is used to be.

lol, really, in all your 9 months and 17 posts? you crusty old veteran, you.


on y!answers, you actually happen to find some guys with knowledge, but here i am utterly disappointed

well, as my grandpa always said, "Don't let the door hit you in the ass on your way out."

.

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.