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 11-27-2005, 07:31 PM   #1 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
Help!!!!!!!

How would i write this program?

//print_triangle prints a number of stars like a right triangle

void print_triangle(int step, int height);


HINT
-Printing a triangle with a step of 2 and a height of 5 would loop like:
**
****
******
********
**********
This is accomplished by writing a nested for loop.
josh2two is offline   Reply With Quote
Old 11-28-2005, 12:23 AM   #2 (permalink)
josh2two
Registered User
 
Join Date: Sep 2005
Posts: 22
josh2two is on a distinguished road
please

anyone?
josh2two is offline   Reply With Quote
Old 11-28-2005, 02:41 AM   #3 (permalink)
ashok rajagopal
Registered User
 
ashok rajagopal's Avatar
 
Join Date: Sep 2005
Posts: 20
ashok rajagopal is on a distinguished road
for(int i =0;i <=10; i+=2)
{
for (int j=0; j<=10;j+=2)
{
printf("*");
}
printf("\n");
}
ashok rajagopal is offline   Reply With Quote
Old 11-28-2005, 06:22 AM   #4 (permalink)
ashok rajagopal
Registered User
 
ashok rajagopal's Avatar
 
Join Date: Sep 2005
Posts: 20
ashok rajagopal is on a distinguished road
put step instead of 2 in the outer loop, and height instead of 2 in the second...

I guess i happened to read your question wrongly... that construct is for step 2 and height 2
ashok rajagopal is offline   Reply With Quote
Old 11-28-2005, 06:30 AM   #5 (permalink)
ashok rajagopal
Registered User
 
ashok rajagopal's Avatar
 
Join Date: Sep 2005
Posts: 20
ashok rajagopal is on a distinguished road
This is the code...
<code>
for(int i =0;i <=height; i+=step)
{
for (int j=0; j<=height;j+=step)
{
printf("*");
}
printf("\n");
}
<code>
ashok rajagopal is offline   Reply With Quote
Old 11-28-2005, 07:20 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
you would need to do it differently, the inner loop will allways print (max) times *, thus you need to check for i in that ie:
Code:
for(i=0; i < height; i+=step)
{
    for(j=0; j < i; j++;)
        printf("*");
    printf("\n");
}
__________________
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 11-28-2005, 07:46 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Or for a C++ version:
Code:
for(i=0; i < height; i+=step)
{
    std::cout.fill('*');
    std::cout.width(i+1);
    std::cout << std::endl;
}
__________________
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
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



All times are GMT -8. The time now is 04:00 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