C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Using 2 for loops Posted by ssk1974 on 31 Oct 2006 at 2:55 PM
how do I write a code like

*
**
***
****

and so on? I am beginning to learn C++.


Report
Re: Using 2 for loops Posted by bluj91 on 31 Oct 2006 at 6:39 PM
: how do I write a code like
:
: *
: **
: ***
: ****
:
: and so on? I am beginning to learn C++.
:
:
:

In C++

#include <cstdio>

using namespace std;

int main(void)
{
for (int i = 0; i < 4; ++ i)
{
for (int j = 0; j < i; ++ i)
putchar('*');
puts("\n\r");
}
return 0;
}

in C:


#include <stdio.h>

int main(void)
{
int i, j;
for (i = 0; i < 4; ++ i)
{
for (j = 0; j < i; ++ i)
putchar('*');
puts("\n\r");
}
return 0;
}

Basically, the only real difference between C and C++ in that in C you cannot declare varible within for loops.
Report
Re: Using 2 for loops Posted by Lundin on 1 Nov 2006 at 1:34 AM
: : how do I write a code like
: :
: : *
: : **
: : ***
: : ****
: :
: : and so on? I am beginning to learn C++.
: :
: :
: :
:
: In C++
:
: #include <cstdio>
:
: using namespace std;
:
: int main(void)
: {
: for (int i = 0; i < 4; ++ i)
: {
: for (int j = 0; j < i; ++ i)
: putchar('*');
: puts("\n\r");
: }
: return 0;
: }
:
: in C:
:
:
: #include <stdio.h>
:
: int main(void)
: {
: int i, j;
: for (i = 0; i < 4; ++ i)
: {
: for (j = 0; j < i; ++ i)
: putchar('*');
: puts("\n\r");
: }
: return 0;
: }
:
: Basically, the only real difference between C and C++ in that in C you cannot declare varible within for loops.
:


Please, stop doing other people's homework, we really don't need to encourage them to get more than 10 posts like this per week. Try to give him a point in the right direction, but please don't provide the whole solution without any effort made from the OP's side.
Report
Re: Using 2 for loops Posted by bluj91 on 1 Nov 2006 at 8:02 PM
: : : how do I write a code like
: : :
: : : *
: : : **
: : : ***
: : : ****
: : :
: : : and so on? I am beginning to learn C++.
: : :
: : :
: : :
: :
: : In C++
: :
: : #include <cstdio>
: :
: : using namespace std;
: :
: : int main(void)
: : {
: : for (int i = 0; i < 4; ++ i)
: : {
: : for (int j = 0; j < i; ++ i)
: : putchar('*');
: : puts("\n\r");
: : }
: : return 0;
: : }
: :
: : in C:
: :
: :
: : #include <stdio.h>
: :
: : int main(void)
: : {
: : int i, j;
: : for (i = 0; i < 4; ++ i)
: : {
: : for (j = 0; j < i; ++ i)
: : putchar('*');
: : puts("\n\r");
: : }
: : return 0;
: : }
: :
: : Basically, the only real difference between C and C++ in that in C you cannot declare varible within for loops.
: :
:
:
: Please, stop doing other people's homework, we really don't need to encourage them to get more than 10 posts like this per week. Try to give him a point in the right direction, but please don't provide the whole solution without any effort made from the OP's side.
:

Sometimes knowing the solution to a given problem can help one in understanding the process of devising the solution. However, fair enough I shall change the nature of my replys to such problems in the future, unless they give me bribes.
Report
Re: Using 2 for loops Posted by HK_MP5KPDW on 2 Nov 2006 at 5:50 AM
: how do I write a code like
:
: *
: **
: ***
: ****
:
: and so on? I am beginning to learn C++.
:
:

You don't even need a nested loop to accomplish this in C++, a single loop can be used in conjunction with the string constructor to create a sequence of '*' characters of the appropriate length.
Report
Re: Using 2 for loops Posted by Kush6 on 2 Nov 2006 at 9:57 AM

: You don't even need a nested loop to accomplish this in C++, a single loop can be used in conjunction with the string constructor to create a sequence of '*' characters of the appropriate length.
:
Simple as it may seem to us, to a beginner, strings are somewhat more complicated than loops.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.