Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-14-2006, 12:58 AM   #1 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
Exclamation 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.
js2006 is offline   Reply With Quote
Old 10-14-2006, 01:04 AM   #2 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
Exclamation 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.
js2006 is offline   Reply With Quote
Old 10-14-2006, 01:17 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Exchange
Code:
cout << "*";
with
Code:
cout << row;
And in the future please wrap [ code] and [/code] tags around your sourcecode, it makes it alot more readable.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-14-2006, 01:27 AM   #4 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
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.
js2006 is offline   Reply With Quote
Old 10-14-2006, 01:55 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-14-2006, 07:26 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-15-2006, 06:13 AM   #7 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
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 ,
js2006 is offline   Reply With Quote
Old 10-15-2006, 10:06 AM   #8 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-15-2006, 08:26 PM   #9 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
i have done it finally , for programming , i learn now that , try - try try again !!
js2006 is offline   Reply With Quote
Old 10-20-2006, 03:39 PM   #10 (permalink)
h.yusufzai
Registered User
 
Join Date: Oct 2006
Posts: 1
h.yusufzai is on a distinguished road
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.
h.yusufzai is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Platform Independent Printing Solutions for C++ laalitseth Standard C, C++ 3 04-15-2005 12:14 AM
Printing freesoft_2000 Java 0 11-27-2004 11:19 AM
Problem printing, please help me laurence10 Java 0 03-11-2003 04:26 PM
new php nums sde PHP 1 05-09-2002 10:31 PM


All times are GMT -8. The time now is 08:34 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting