Click here to Skip to main content
14,870,596 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hi to every body
i have a question about nested for loop

how to print pyramids
*
***
*****
***
*
plz any one help me i shall be very thankful wtng
Posted
Updated 9-Jan-21 3:37am

This looks like homework.

It would be best if you did your own homework. It is given to you so that you will learn something, think about what have been taught. Read your text books and give it a try.

Once you have code and run into problems you can always come back here with a more specific question and the community will do its best to help you. Don't forget to post only the relevant code bits that pose the problem as a code dump is not usually helpful at all in getting someone to help/assist you.
   
Comments
[no name] 25-Oct-11 13:02pm
   
My 5! Completely agree.
There are loads of examples on the internet: a simple google would have found them. Google: how to print pyramids[^]

Please try to look at google before asking basic questions. :)
   
This is completely Home Work Question. If your Tutor has given this as an assignment to you, then it must has some purpose.

Many Programming Biginners gets these type of assignments in their early Programming learning days. If you try to solve it by applying your logic then not only it will improve your logical thinking but also you shall start Loving it.

Believe me it's so interesting if you do it by your self.
   
This is very simple.
What we want is a mathematical function which will do the following
0 -> 1
1 -> 3
2 -> 5
3 -> 3
4 -> 1


If we break this down we get the equation y = 5 - abs(x - 2) * 2

So, our inner for loop needs to implement that function, while our outer for loop goes from 0-4 (inclusive)

C++
#include <stdio.h>

int abs(int n) {
	return n > 0 ? n : -n;
}

int main() {
	for (int i = 0; i < 5; ++i) {
		int max = 5 - abs(i - 2) * 2; //Our equation
		for (int j = 0; j < max; ++j) {
			putchar('*');
		}
		putchar('\n'); //Print the new line
	}
	return 0;
}
   
v3
Comments
[no name] 25-Oct-11 13:17pm
   
I woudn't vote, but certainly it's not a good idea to provide ready made solution to OP, who probably even did not tried to solve.
Just out of spite ;P, here's a version that doesn't require nested loops, and no length function either:
C++
void pyramid(unsigned short height) {
   char* line = new char[height+1];   // allocate output string
   char* p = line+height;             // position at end
   *p = 0;                            // set end of string marker
   while (p != line) {                // iterate to start of string ...
      *--p = '*';                     // ... and fill while doing so
      puts(p);                        // print the filled-in part
   }
   while (*++p) {                     // iterate back to end of string
      puts(p);                        // print to end of string
   }
   delete [] line;                    // release string memory
}
   
Comments
Andrew Brock 25-Oct-11 12:27pm
   
The question has 1, 3, 5, 3, 1 stars, not incrementing/decrementing by 1
Stefan_Lang 25-Oct-11 12:29pm
   
Ouch, missed that.

Oh well, he can fix that as a homework ;)
[no name] 25-Oct-11 13:16pm
   
I woudn't vote, but certainly it's not a good idea to provide ready made solution to OP, who probably even did not tried to solve it. But good part is, OP has something still remaining to solve. :)
Stefan_Lang 26-Oct-11 3:48am
   
Agreed, and in fact I hesitated posting this at all. But since at the time there already was a full solution, and because I did deliberately ignore the apparent requirement (using nested for loops), I thought it wouldn't hurt.
muhammd awais 9-Nov-11 10:50am
   
HI brother raiskazi tnx for ur suggestion, i am new in c++ and i am facing problems in c++ specialy functions plz suggest any site that will help me to overcome my problems in c++.
waiting for your kind reply.
muhammd awais 9-Nov-11 10:46am
   
hi brother raiskazi i have problem in c++ specialy in functions.plz suggest any site which give me alot of help to overcome my problems in c++.
wtng for ur kind reply
Thnx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900