473,503 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incremental pattern with nested loops

1 New Member
Hello. I am in a beginner C++ programming class. This is my first class I have ever done any programming in my whole life. I'll also mention that though it's a college freshman class, there are many parts to the programs that we have to write that I just do not understand. The current project that I am working on involves printing 4 different patterns to the screen(like the ones I have provided below)
1st
*
**
***
****
*****

2nd
*********
********
*******
******
*****
****
***
**
*

3rd: like 1 but w/ setw(9)
4th: like 2 but w/ setw(9)

I can get the 2nd one to work with the following program:
Expand|Select|Wrap|Line Numbers
  1. int(main)
  2. {   
  3.     int   i, j, k;
  4.  
  5.        for(i=1; i<=20; i++)
  6.  
  7.        {
  8.  
  9.             cout<<"\n";
  10.  
  11.               for(j=i; j<=20; j++)
  12.       {       
  13.             cout<< '*';
  14.       }
  15.    }
  16.        cout << endl;
but I am not sure how to go about getting the others to work, for what I have tried has given me infinite asterisks. Below is what I have tried.
Expand|Select|Wrap|Line Numbers
  1. for(k=i; k<=20; k--)
  2.     {
  3.         cout << " ";
  4.         cout << setw(9) << fixed << '*';
  5.     }
Any help, hints, or suggestions would be GREATLY appreciated.

Thanks,
Amber
Oct 6 '07 #1
1 1589
scruggsy
147 New Member
for(k=i; k<=20; k--)
Hi, Amber.
Your problem is in the above line of code.
Each time through the loop, you're subtracting 1 from k.
Your loop won't terminate as long as k <= 20.
k will never be greater than 20 if you subtract from it each time through.
Fix either your loop termination condition or your loop update.
Hope this helps.
Oct 6 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

25
12664
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
1778
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
46
9868
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
5152
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
2831
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
3107
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
4
1569
by: dhinakar_ve | last post by:
Hi All, I am writing a function to generate the strings based on a pattern. For example A will generate A1, A2 and A3. If the pattern is A then it will generate the strings A11, A12, A21,...
13
2680
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
7234
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.