|
 |
|
 |
10-14-2006, 12:58 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
Printing a Pyramid of num's
how can a program be constructed to display a pyramid of numbers using a for loops in c++,
it Ask the user to enter a digit(0-9) and display the pyramid with limit up to that number( as shown in output).and after the output is shown , user presses a key to exit.
OUTPUT :
if 5,entered :
Code:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Press any key to quit
Last edited by redhead; 10-14-2006 at 01:18 AM.
|
|
|
10-14-2006, 01:04 AM
|
#2 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
a similar code
here is a code , which i made how it can be improved to print number instead of Asteriaks as shown in the above Output ,
Code:
int size;
while (cin >> size) {
for (int row=1; row<=size; row++) {
for (int col=0; col<row; col++) {
cout << "*";
}
cout << endl; // end the line.
}
}
output:
*
**
***
****
*****
******
Last edited by redhead; 10-14-2006 at 01:19 AM.
|
|
|
10-14-2006, 01:17 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Exchange with And in the future please wrap [ code] and [/code] tags around your sourcecode, it makes it alot more readable.
|
|
|
10-14-2006, 01:27 AM
|
#4 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
thankyou,
thanks redhead,
now i want to improve it by limiting the user to take only digits from 0 - 9
also After the output is shown , there is a repsone taken from user
Press any key to exit
please help me with this.
|
|
|
10-14-2006, 01:55 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
For a starter, here is a C implementation of it
Code:
#include <stdio.h>
int main(void)
{
size_t count=0, i, j;
/* ask the user for numbers to print */
printf("How high should the pyramid go ?: ");
fflush(stdout);
fscanf(stdin, "%d", &count);
for(i=1; i <= count; ++i)
{
/* calculate privius spaces */
for(j=(count/2+i); j <= count*2; ++j)
printf(" ");
/* show actual number */
for(j=0; j < i; ++j)
printf(" %d", i);
printf("\n");
}
return 0;
}
Notice it will only go upto 9, since the number 10 will span far more than what the single space, in order to align the numbers correct, will be able to correct in the pyramid formation.
Now try an make a similar one in ANSI C++ and for educational purpus, try to make one which would handle numbers larger than 10.
I'm on my way out to play some soccer, so for now this will have todo, but when I return, I will give further advices.
|
|
|
10-14-2006, 07:26 AM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Quote:
|
now i want to improve it by limiting the user to take only digits from 0 - 9
|
Code:
while(cin >> size)
{
if(size < 0 || size > 9)
{
cout << "Sorry your given number isn't within acceted limits (0-9)" << endl;
continue;
}
...
Quote:
After the output is shown , there is a repsone taken from user
Press any key to exit
|
hmm... shouldn't this run untill the user explicit exit, or should it just be a single run ??
To accommodate a request running untill the user explicit exits, you would take percausions like
Code:
cout << "Please enter a number between 0 and 9, use -1 to quit: ";
while(cin >> size && size >= 0)
...
To accommodate the user unly beeing asked once, and then exit uppon further user interaction, just eliminate your while clause, and simply read from the user, check for valid number, and then print your pyramid.
Then query the user "press anythign to quit" and simply just let the program terminate uppon next linefeed.
|
|
|
10-15-2006, 06:13 AM
|
#7 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
Thanks for your support ,red head
Quote:
|
Then query the user "press anythink to quit" and simply just let the program terminate uppon next linefeed.
|
I want to display the Praymid and then shut my programme , as user presses any key
now how can i query user , is it just simply
Code:
cout << "Press any Key to Exit";
..... ????????????????
or any other way around ?? , iam a
C++ newbie , doesn't know much , like learn and conquer it.
so please do me a favour !!
Thanks again ,
|
|
|
10-15-2006, 10:06 AM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Read the system (PAUSE) article.
But the easiest way would be something like:
Code:
int main()
{
...
char ch;
...
cout << "Press enter to exit";
cin >> ch;
return 0;
}
|
|
|
10-15-2006, 08:26 PM
|
#9 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 19
|
i have done it finally , for programming , i learn now that , try - try try again !! 
|
|
|
10-20-2006, 03:39 PM
|
#10 (permalink)
|
|
Registered User
Join Date: Oct 2006
Posts: 1
|
Quote:
|
Originally Posted by js2006
here is a code , which i made how it can be improved to print number instead of Asteriaks as shown in the above Output ,
Code:
int size;
while (cin >> size) {
for (int row=1; row<=size; row++) {
for (int col=0; col<row; col++) {
cout << "*";
}
cout << endl; // end the line.
}
}
output:
*
**
***
****
*****
******
|
sorry could u write the full code of this starting form #include<iostream.h>
i cant get this to work....i am a newbie.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 08:34 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|